-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #114 from gradle/tt/add-template-for-java-library
Setup project generation for sample declarative java library and java application projects
- Loading branch information
Showing
21 changed files
with
351 additions
and
1 deletion.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
unified-prototype/unified-plugin/plugin-common/build.gradle.kts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
unified-prototype/unified-plugin/plugin-common/output/build.gradle.dcl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
javaLibrary { | ||
javaVersion = 21 | ||
|
||
dependencies { | ||
implementation(project(":java-util")) | ||
implementation("com.google.guava:guava:32.1.3-jre") | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...-prototype/unified-plugin/plugin-common/output/src/main/java/com/example/lib/Library.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.example.lib; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
|
||
public class Library { | ||
public Iterable<String> getMessages() { | ||
// Verify that Guava is available | ||
ImmutableList.Builder<String> builder = ImmutableList.builder(); | ||
builder.add("Hello from Java " + System.getProperty("java.version")); | ||
|
||
return builder.build(); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...n/plugin-common/src/integTest/groovy/org/gradle/util/ResourceLoaderIntegrationTest.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package org.gradle.util | ||
|
||
import org.apache.commons.io.FileUtils | ||
import spock.lang.Specification | ||
|
||
class ResourceLoaderIntegrationTest extends Specification { | ||
File outputDir | ||
|
||
def setup() { | ||
outputDir = new File("build/tmp/integTest/output").tap { deleteDir() } | ||
} | ||
|
||
def "can load resource from jar file"() { | ||
given: | ||
ResourceLoader resourceLoader = new ResourceLoader() | ||
|
||
when: | ||
resourceLoader.extractResourcesFromJar("templates/java-library", outputDir) | ||
|
||
then: | ||
assertOutputIs(['build.gradle.dcl', 'src/main/java/com/example/lib/Library.java']) | ||
} | ||
|
||
private void assertOutputIs(List<String> expectedRelativePaths) { | ||
def actualPaths = FileUtils.listFiles(outputDir, null, true)*.path | ||
def expectedPaths = expectedRelativePaths.collect { "${outputDir.toPath()}/$it".toString() } | ||
assert actualPaths == expectedPaths | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...in-common/src/main/java/org/gradle/api/experimental/buildinit/StaticProjectGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package org.gradle.api.experimental.buildinit; | ||
|
||
import org.gradle.api.file.Directory; | ||
import org.gradle.buildinit.projectspecs.InitProjectConfig; | ||
import org.gradle.buildinit.projectspecs.InitProjectGenerator; | ||
import org.gradle.util.ResourceLoader; | ||
|
||
/** | ||
* An {@link InitProjectGenerator} that generates a project from a static template packaged | ||
* as resources files in the {@link #TEMPLATES_ROOT} directory. | ||
*/ | ||
@SuppressWarnings("UnstableApiUsage") | ||
public final class StaticProjectGenerator implements InitProjectGenerator { | ||
private static final String TEMPLATES_ROOT = "templates"; | ||
|
||
@Override | ||
public void generate(InitProjectConfig config, Directory projectDir) { | ||
if (!(config.getProjectSpec() instanceof StaticProjectSpec projectSpec)) { | ||
throw new IllegalArgumentException("Unknown project type: " + config.getProjectSpec().getDisplayName() + " (" + config.getProjectSpec().getClass().getName() + ")"); | ||
} | ||
|
||
String templatePath = TEMPLATES_ROOT + "/" + projectSpec.getTemplatePath(); | ||
ResourceLoader resourceLoader = new ResourceLoader(); | ||
|
||
try { | ||
resourceLoader.extractResourcesFromJar(templatePath, projectDir.getAsFile()); | ||
} catch (Exception e) { | ||
throw new RuntimeException("Error extracting resources for: '" + projectSpec.getDisplayName() + "' from: '" + templatePath + "'!", e); | ||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
.../plugin-common/src/main/java/org/gradle/api/experimental/buildinit/StaticProjectSpec.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package org.gradle.api.experimental.buildinit; | ||
|
||
import org.gradle.buildinit.projectspecs.InitProjectParameter; | ||
import org.gradle.buildinit.projectspecs.InitProjectSpec; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
/** | ||
* An {@link InitProjectSpec} that represents a project that can be generated from a static template | ||
* using the {@link StaticProjectGenerator} | ||
*/ | ||
@SuppressWarnings("UnstableApiUsage") | ||
public final class StaticProjectSpec implements InitProjectSpec { | ||
private final String templatePath; | ||
private final String displayName; | ||
|
||
public StaticProjectSpec(String templatePath, String displayName) { | ||
this.templatePath = templatePath; | ||
this.displayName = displayName; | ||
} | ||
|
||
@Override | ||
public String getDisplayName() { | ||
return displayName; | ||
} | ||
|
||
@Override | ||
public List<InitProjectParameter<?>> getParameters() { | ||
return Collections.emptyList(); | ||
} | ||
|
||
public String getTemplatePath() { | ||
return templatePath; | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
...-prototype/unified-plugin/plugin-common/src/main/java/org/gradle/util/ResourceLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package org.gradle.util; | ||
|
||
import org.apache.commons.io.FileUtils; | ||
import org.apache.commons.io.IOUtils; | ||
|
||
import java.io.File; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.JarURLConnection; | ||
import java.net.URL; | ||
import java.util.Iterator; | ||
import java.util.jar.JarEntry; | ||
import java.util.jar.JarFile; | ||
|
||
/** | ||
* Static util class containing methods for loading resources from the classpath. | ||
*/ | ||
public final class ResourceLoader { | ||
/** | ||
* Recursively extracts the contents of a directory in a jar file on the classpath to a specified directory. | ||
* | ||
* @param relativePath path to the source directory within the jar file | ||
* @param destDir target directory to extract the contents to | ||
* @throws IOException if an I/O error occurs | ||
*/ | ||
public void extractResourcesFromJar(String relativePath, File destDir) throws IOException { | ||
URL jarDirURL = ResourceLoader.class.getClassLoader().getResource(relativePath); | ||
if (jarDirURL == null) { | ||
throw new IllegalArgumentException("Directory: '" + relativePath + "' not found on classpath."); | ||
} | ||
JarFile jarFile = ((JarURLConnection) jarDirURL.openConnection()).getJarFile(); | ||
System.out.println("Using URL: " + jarDirURL); | ||
|
||
Iterator<JarEntry> iterator = jarFile.entries().asIterator(); | ||
while (iterator.hasNext()) { | ||
JarEntry entry = iterator.next(); | ||
String entryName = entry.getName(); | ||
|
||
if (entryName.startsWith(relativePath)) { | ||
String entrySuffix = entryName.substring(relativePath.length()); | ||
System.out.println("Entry: " + entry + " name: " + entryName + " suffix: " + entrySuffix); | ||
File destFile = new File(destDir, entrySuffix); | ||
System.out.println("Dest file: " + destFile); | ||
|
||
if (entry.isDirectory()) { | ||
System.out.println("Is directory"); | ||
FileUtils.forceMkdir(destFile); | ||
System.out.println("Created directory"); | ||
} else { | ||
System.out.println("Is file"); | ||
try (InputStream is = jarFile.getInputStream(entry); | ||
FileOutputStream fos = new FileOutputStream(destFile)) { | ||
IOUtils.copy(is, fos); | ||
} | ||
System.out.println("Copied file"); | ||
} | ||
} | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...pe/unified-plugin/plugin-common/src/test/groovy/org/gradle/util/ResourceLoaderTest.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package org.gradle.util | ||
|
||
import org.apache.commons.io.FileUtils | ||
|
||
import spock.lang.Specification | ||
|
||
class ResourceLoaderTest extends Specification { | ||
def "can load resource from jar file"() { | ||
given: | ||
File output = new File("output").tap { mkdirs() } | ||
ResourceLoader resourceLoader = new ResourceLoader(this.getClass().getClassLoader()) | ||
|
||
when: | ||
File templatesDir = resourceLoader.getResource("templates/java-library") | ||
FileUtils.copyDirectory(templatesDir, output) | ||
|
||
then: | ||
FileUtils.listFiles(output, null, true)*.path == ['output/build.gradle.dcl', 'output/src/main/java/com/example/lib/Library.java'] | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...e/unified-plugin/plugin-common/src/test/resources/templates/java-library/build.gradle.dcl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
javaLibrary { | ||
javaVersion = 21 | ||
|
||
dependencies { | ||
implementation(project(":java-util")) | ||
implementation("com.google.guava:guava:32.1.3-jre") | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...mmon/src/test/resources/templates/java-library/src/main/java/com/example/lib/Library.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.example.lib; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
|
||
public class Library { | ||
public Iterable<String> getMessages() { | ||
// Verify that Guava is available | ||
ImmutableList.Builder<String> builder = ImmutableList.builder(); | ||
builder.add("Hello from Java " + System.getProperty("java.version")); | ||
|
||
return builder.build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
...ugin/plugin-jvm/src/main/java/org/gradle/api/experimental/buildinit/JVMProjectSource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package org.gradle.api.experimental.buildinit; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import org.gradle.buildinit.projectspecs.InitProjectGenerator; | ||
import org.gradle.buildinit.projectspecs.InitProjectSpec; | ||
import org.gradle.buildinit.projectspecs.InitProjectSource; | ||
|
||
/** | ||
* A {@link InitProjectSource} of project specifications for JVM projects. | ||
*/ | ||
@SuppressWarnings("UnstableApiUsage") | ||
public final class JVMProjectSource implements InitProjectSource { | ||
@Override | ||
public List<InitProjectSpec> getProjectSpecs() { | ||
return Arrays.asList( | ||
new StaticProjectSpec("java-library", "Declarative Java Library Project"), | ||
new StaticProjectSpec("java-application", "Declarative Java Application Project") | ||
); | ||
} | ||
|
||
@Override | ||
public InitProjectGenerator getProjectGenerator() { | ||
return new StaticProjectGenerator(); | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
...d-plugin/plugin-jvm/src/main/java/org/gradle/api/experimental/buildinit/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
@org.gradle.api.NonNullApi | ||
package org.gradle.api.experimental.buildinit; |
1 change: 1 addition & 0 deletions
1
.../src/main/resources/META-INF/services/org.gradle.buildinit.projectspecs.InitProjectSource
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
org.gradle.api.experimental.buildinit.JVMProjectSource |
21 changes: 21 additions & 0 deletions
21
.../unified-plugin/plugin-jvm/src/main/resources/templates/java-application/build.gradle.dcl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package templates.`java-application` | ||
|
||
javaApplication { | ||
// compile for 17 | ||
javaVersion = 17 | ||
mainClass = "com.example.App" | ||
|
||
dependencies { | ||
implementation(project(":java-util")) | ||
implementation("com.google.guava:guava:32.1.3-jre") | ||
} | ||
|
||
testing { | ||
// test on 21 | ||
testJavaVersion = 21 | ||
|
||
dependencies { | ||
implementation("org.junit.jupiter:junit-jupiter:5.10.2") | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...ugin-jvm/src/main/resources/templates/java-application/src/main/java/com/example/App.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.example; | ||
|
||
import com.example.utils.Utils; | ||
import com.google.common.collect.ImmutableList; | ||
|
||
public class App { | ||
public static void main(String[] args) { | ||
// Verify that Guava is available | ||
ImmutableList.Builder<String> builder = ImmutableList.builder(); | ||
builder.add("Hello from Java " + System.getProperty("java.version")); | ||
|
||
// Verify that the Java library is available | ||
Utils utils = new Utils(); | ||
builder.add(utils.getWelcome()); | ||
|
||
ImmutableList<String> messages = builder.build(); | ||
messages.forEach(System.out::println); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...-jvm/src/main/resources/templates/java-application/src/test/java/com/example/AppTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.example; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
public class AppTest { | ||
@Test | ||
void appHasATest() { | ||
assert true; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...type/unified-plugin/plugin-jvm/src/main/resources/templates/java-library/build.gradle.dcl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
javaLibrary { | ||
javaVersion = 21 | ||
|
||
dependencies { | ||
implementation(project(":java-util")) | ||
implementation("com.google.guava:guava:32.1.3-jre") | ||
} | ||
} |
Oops, something went wrong.