diff --git a/itests/alltags.java b/itests/alltags.java new file mode 100644 index 000000000..ff583ff16 --- /dev/null +++ b/itests/alltags.java @@ -0,0 +1,28 @@ + +//REPOS dummy=http://dummy +//DEPS dummy.org:dummy:1.2.3 + +//SOURCES Two.java +//SOURCES nested/*.java + +//FILES res/resource.properties renamed.properties=res/resource.properties +//FILES META-INF/application.properties=res/resource.properties + +//COMPILE_OPTIONS --enable-preview --verbose +//RUNTIME_OPTIONS --add-opens java.base/java.net=ALL-UNNAMED +//NATIVE_OPTIONS -O1 -d + +//JAVA 11+ +//MAIN mainclass +//MODULE mymodule +//DESCRIPTION some description +//GAV example.org:alltags:1.2.3 +//CDS +//PREVIEW +//MANIFEST one=1 two=2 three=3 +//JAVAAGENT + +public class alltags { + public static void main(String... args) { + } +} diff --git a/itests/jbang-catalog.json b/itests/jbang-catalog.json index e81ad7e3e..8e9a5d011 100644 --- a/itests/jbang-catalog.json +++ b/itests/jbang-catalog.json @@ -19,6 +19,87 @@ }, "resource": { "script-ref": "res/resource.java" + }, + "alltags": { + "script-ref": "alltags.java", + "description": "twodesc", + "arguments": ["2"], + "runtime-options": ["--two"], + "sources": ["helloworld.java"], + "files": ["res/test.properties"], + "dependencies": ["twodep"], + "repositories": ["tworepo"], + "classpaths": ["twocp"], + "properties": {"two":"2"}, + "java": "twojava", + "main": "twomain", + "module": "twomodule", + "compile-options": ["--ctwo"], + "native-image": true, + "native-options": ["--ntwo"], + "jfr": "twojfr", + "debug": {"twod":"2"}, + "cds": true, + "interactive": true, + "enable-preview": true, + "enable-assertions": true, + "enable-system-assertions": true, + "manifest-options": {"twom":"2"}, + "java-agents": [{"agent-ref":"twojag","options":"twoopts"}] + }, + "hellojar": { + "script-ref": "hellojar.jar", + "description": "twodesc", + "arguments": ["2"], + "runtime-options": ["--two"], + "sources": ["helloworld.java"], + "files": ["res/test.properties"], + "dependencies": ["twodep"], + "repositories": ["tworepo"], + "classpaths": ["twocp"], + "properties": {"two":"2"}, + "java": "twojava", + "main": "twomain", + "module": "twomodule", + "compile-options": ["--ctwo"], + "native-image": true, + "native-options": ["--ntwo"], + "jfr": "twojfr", + "debug": {"twod":"2"}, + "cds": true, + "interactive": true, + "enable-preview": true, + "enable-assertions": true, + "enable-system-assertions": true, + "manifest-options": {"twom":"2"}, + "java-agents": [{"agent-ref":"twojag","options":"twoopts"}] + }, + "pgmgav": { + "script-ref": "org.eclipse.jgit:org.eclipse.jgit.pgm:5.9.0.202009080501-r", + "description": "twodesc", + "arguments": ["2"], + "runtime-options": ["--two"], + "sources": ["helloworld.java"], + "files": ["res/test.properties"], + "dependencies": ["info.picocli:picocli:4.6.3"], + "repositories": ["mavencentral", "tworepo"], + "classpaths": ["twocp"], + "properties": {"two":"2"}, + "java": "twojava", + "main": "twomain", + "module": "twomodule", + "compile-options": ["--ctwo"], + "native-image": true, + "native-options": ["--ntwo"], + "jfr": "twojfr", + "debug": {"twod":"2"}, + "cds": true, + "interactive": true, + "enable-preview": true, + "enable-assertions": true, + "enable-system-assertions": true, + "manifest-options": {"twom":"2"}, + "java-agents": [{"agent-ref":"twojag","options":"twoopts"}] } }, "templates": {}, diff --git a/src/main/java/dev/jbang/catalog/Alias.java b/src/main/java/dev/jbang/catalog/Alias.java index 83f3c4714..63164b851 100644 --- a/src/main/java/dev/jbang/catalog/Alias.java +++ b/src/main/java/dev/jbang/catalog/Alias.java @@ -6,8 +6,11 @@ import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.function.Function; +import javax.annotation.Nonnull; + import com.google.gson.annotations.SerializedName; import dev.jbang.cli.ExitException; @@ -56,17 +59,30 @@ public class Alias extends CatalogItem { public static class JavaAgent { @SerializedName(value = "agent-ref") + @Nonnull public final String agentRef; + @Nonnull public final String options; - private JavaAgent() { - this(null, null); - } - - public JavaAgent(String agentRef, String options) { + public JavaAgent(@Nonnull String agentRef, @Nonnull String options) { this.agentRef = agentRef; this.options = options; } + + @Override + public boolean equals(Object o) { + if (this == o) + return true; + if (o == null || getClass() != o.getClass()) + return false; + JavaAgent javaAgent = (JavaAgent) o; + return agentRef.equals(javaAgent.agentRef) && options.equals(javaAgent.options); + } + + @Override + public int hashCode() { + return Objects.hash(agentRef, options); + } } public Alias() { diff --git a/src/main/java/dev/jbang/catalog/CatalogItem.java b/src/main/java/dev/jbang/catalog/CatalogItem.java index 5f3b970c4..d57e0f09c 100644 --- a/src/main/java/dev/jbang/catalog/CatalogItem.java +++ b/src/main/java/dev/jbang/catalog/CatalogItem.java @@ -1,5 +1,7 @@ package dev.jbang.catalog; +import java.nio.file.Paths; + import dev.jbang.util.Util; abstract class CatalogItem { @@ -16,7 +18,12 @@ public CatalogItem(Catalog catalog) { public String resolve(String scriptRef) { String ref = scriptRef; if (!Util.isAbsoluteRef(ref)) { - ref = catalog.getScriptBase() + "/" + ref; + String base = catalog.getScriptBase(); + if (Util.isRemoteRef(base) || !Util.isValidPath(base)) { + ref = base + "/" + ref; + } else { + ref = Paths.get(base).resolve(ref).toString(); + } } return ref; } diff --git a/src/main/java/dev/jbang/cli/Alias.java b/src/main/java/dev/jbang/cli/Alias.java index 8c5134f95..abd7e4e4d 100644 --- a/src/main/java/dev/jbang/cli/Alias.java +++ b/src/main/java/dev/jbang/cli/Alias.java @@ -85,7 +85,7 @@ class AliasAdd extends BaseAliasCommand { String name; @CommandLine.Option(names = { "--enable-preview" }, description = "Activate Java preview features") - boolean enablePreviewRequested; + Boolean enablePreviewRequested; @CommandLine.Parameters(paramLabel = "params", index = "1..*", arity = "0..*", description = "Parameters to pass on to the script") List userParams; diff --git a/src/main/java/dev/jbang/cli/BaseBuildCommand.java b/src/main/java/dev/jbang/cli/BaseBuildCommand.java index c0c613876..8aaafa53f 100644 --- a/src/main/java/dev/jbang/cli/BaseBuildCommand.java +++ b/src/main/java/dev/jbang/cli/BaseBuildCommand.java @@ -31,7 +31,7 @@ public abstract class BaseBuildCommand extends BaseCommand { Path buildDir; @CommandLine.Option(names = { "--enable-preview" }, description = "Activate Java preview features") - boolean enablePreviewRequested; + Boolean enablePreviewRequested; PrintStream out = new PrintStream(new FileOutputStream(FileDescriptor.out)); diff --git a/src/main/java/dev/jbang/dependencies/ArtifactResolver.java b/src/main/java/dev/jbang/dependencies/ArtifactResolver.java index 865afede8..298b2d9ae 100644 --- a/src/main/java/dev/jbang/dependencies/ArtifactResolver.java +++ b/src/main/java/dev/jbang/dependencies/ArtifactResolver.java @@ -12,6 +12,7 @@ import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.Properties; import java.util.Set; import java.util.stream.Collectors; @@ -382,8 +383,10 @@ private Settings newEffectiveSettings() { DefaultSettingsBuilderFactory factory = new DefaultSettingsBuilderFactory(); DefaultSettingsBuilder builder = factory.newInstance(); + Properties props = new Properties(); + props.putAll(System.getProperties()); SettingsBuildingRequest settingsBuilderRequest = new DefaultSettingsBuildingRequest(); - settingsBuilderRequest.setSystemProperties(System.getProperties()); + settingsBuilderRequest.setSystemProperties(props); if (withUserSettings) { // find the settings diff --git a/src/main/java/dev/jbang/source/ProjectBuilder.java b/src/main/java/dev/jbang/source/ProjectBuilder.java index cc70b294a..feefa34c5 100644 --- a/src/main/java/dev/jbang/source/ProjectBuilder.java +++ b/src/main/java/dev/jbang/source/ProjectBuilder.java @@ -53,7 +53,7 @@ public class ProjectBuilder { private File catalogFile; private Boolean nativeImage; private String javaVersion; - private boolean enablePreview; + private Boolean enablePreview; // Cached values private Properties contextProperties; @@ -172,7 +172,7 @@ public ProjectBuilder nativeImage(Boolean nativeImage) { return this; } - public ProjectBuilder enablePreview(boolean enablePreviewRequested) { + public ProjectBuilder enablePreview(Boolean enablePreviewRequested) { this.enablePreview = enablePreviewRequested; return this; } @@ -278,7 +278,7 @@ private Project createJarProject(ResourceRef resourceRef) { && DependencyUtil.looksLikeAGav(resourceRef.getOriginalResource())) { prj.getMainSourceSet().addDependency(resourceRef.getOriginalResource()); } - return importJarMetadata(updateProject(prj)); + return updateProject(importJarMetadata(prj, moduleName != null && moduleName.isEmpty())); } private Project createJbangProject(ResourceRef resourceRef) { @@ -353,12 +353,12 @@ public Project build(Source src) { return updateProject(updateProjectMain(src, prj, getResourceResolver())); } - private Project importJarMetadata(Project prj) { + private Project importJarMetadata(Project prj, boolean importModuleName) { Path jar = prj.getResourceRef().getFile(); if (jar != null && Files.exists(jar)) { try (JarFile jf = new JarFile(jar.toFile())) { String moduleName = ModuleUtil.getModuleName(jar); - if (moduleName != null && "".equals(prj.getModuleName().orElse(null))) { + if (moduleName != null && importModuleName) { // We only import the module name if the project's module // name was set to an empty string, which basically means // "we want module support, but we don't know the name". @@ -410,6 +410,7 @@ private Project updateProject(Project prj) { updateAllSources(prj, replaceAllProps(additionalSources)); ss.addResources(allToFileRef(replaceAllProps(additionalResources))); ss.addCompileOptions(compileOptions); + ss.addNativeOptions(nativeOptions); prj.putProperties(properties); prj.getManifestAttributes().putAll(manifestOptions); if (moduleName != null) { @@ -426,7 +427,9 @@ private Project updateProject(Project prj) { if (nativeImage != null) { prj.setNativeImage(nativeImage); } - prj.setEnablePreviewRequested(enablePreview); + if (enablePreview != null) { + prj.setEnablePreviewRequested(enablePreview); + } return prj; } @@ -589,6 +592,9 @@ private void updateFromAlias(Alias alias) { if (manifestOptions.isEmpty()) { manifestOptions(alias.manifestOptions); } + if (enablePreview == null) { + enablePreview(alias.enablePreview); + } } public static boolean isAlias(ResourceRef resourceRef) { diff --git a/src/main/java/dev/jbang/source/RefTarget.java b/src/main/java/dev/jbang/source/RefTarget.java index 572021b3a..1c9d93872 100644 --- a/src/main/java/dev/jbang/source/RefTarget.java +++ b/src/main/java/dev/jbang/source/RefTarget.java @@ -6,6 +6,10 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardCopyOption; +import java.util.Objects; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; import dev.jbang.cli.ExitException; import dev.jbang.util.Util; @@ -16,19 +20,23 @@ * //FILES target=source directives in scripts and templates. */ public class RefTarget { + @Nonnull protected final ResourceRef source; + @Nullable protected final Path target; - protected RefTarget(ResourceRef source, Path target) { + protected RefTarget(@Nonnull ResourceRef source, @Nullable Path target) { assert (source != null); this.source = source; this.target = target; } + @Nonnull public ResourceRef getSource() { return source; } + @Nullable public Path getTarget() { return target; } @@ -52,6 +60,29 @@ public void copy(Path destroot) { } } + @Override + public boolean equals(Object o) { + if (this == o) + return true; + if (o == null || getClass() != o.getClass()) + return false; + RefTarget refTarget = (RefTarget) o; + return source.equals(refTarget.source) && Objects.equals(target, refTarget.target); + } + + @Override + public int hashCode() { + return Objects.hash(source, target); + } + + @Override + public String toString() { + return "RefTarget{" + + "source=" + source + + ", target=" + target + + '}'; + } + public static RefTarget create(String ref, Path dest, ResourceResolver siblingResolver) { return new RefTarget(siblingResolver.resolve(ref), dest); } diff --git a/src/main/java/dev/jbang/source/TagReader.java b/src/main/java/dev/jbang/source/TagReader.java index f0b853556..90e544b7a 100644 --- a/src/main/java/dev/jbang/source/TagReader.java +++ b/src/main/java/dev/jbang/source/TagReader.java @@ -392,6 +392,13 @@ public static RefTarget toFileRef(String fileReference, ResourceResolver sibling try { ResourceRef ref = siblingResolver.resolve(src); + if (ref == null) { + throw new ExitException(EXIT_INVALID_INPUT, + String.format("Could not find '%s' when resolving '%s' in %s", + src, + fileReference, + siblingResolver.description())); + } if (dest != null && dest.endsWith("/")) { p = p.resolve(ref.getFile().getFileName()); } diff --git a/src/main/java/dev/jbang/source/buildsteps/IntegrationBuildStep.java b/src/main/java/dev/jbang/source/buildsteps/IntegrationBuildStep.java index 23a7d18ba..3bee1f5bf 100644 --- a/src/main/java/dev/jbang/source/buildsteps/IntegrationBuildStep.java +++ b/src/main/java/dev/jbang/source/buildsteps/IntegrationBuildStep.java @@ -27,23 +27,28 @@ public IntegrationBuildStep(BuildContext ctx) { public IntegrationResult build() throws IOException { // todo: setting properties to avoid loosing properties in integration call. Project project = ctx.getProject(); - Properties old = System.getProperties(); - Properties temp = new Properties(System.getProperties()); + Properties oldProps = System.getProperties(); + Properties tempProps = new Properties(); + tempProps.putAll(oldProps); + System.setProperties(tempProps); for (Map.Entry entry : project.getProperties().entrySet()) { System.setProperty(entry.getKey(), entry.getValue()); } - IntegrationResult integrationResult = IntegrationManager.runIntegrations(ctx); - System.setProperties(old); + try { + IntegrationResult integrationResult = IntegrationManager.runIntegrations(ctx); - if (project.getMainClass() == null) { // if non-null user forced set main - if (integrationResult.mainClass != null) { - project.setMainClass(integrationResult.mainClass); + if (project.getMainClass() == null) { // if non-null user forced set main + if (integrationResult.mainClass != null) { + project.setMainClass(integrationResult.mainClass); + } } + if (integrationResult.javaArgs != null && !integrationResult.javaArgs.isEmpty()) { + // Add integration options to the java options + project.addRuntimeOptions(integrationResult.javaArgs); + } + return integrationResult; + } finally { + System.setProperties(oldProps); } - if (integrationResult.javaArgs != null && !integrationResult.javaArgs.isEmpty()) { - // Add integration options to the java options - project.addRuntimeOptions(integrationResult.javaArgs); - } - return integrationResult; } } diff --git a/src/test/java/dev/jbang/cli/TestAlias.java b/src/test/java/dev/jbang/cli/TestAlias.java index 95baa634d..38bab7ad6 100644 --- a/src/test/java/dev/jbang/cli/TestAlias.java +++ b/src/test/java/dev/jbang/cli/TestAlias.java @@ -35,18 +35,33 @@ public class TestAlias extends BaseTest { " \"script-ref\": \"one\",\n" + " \"description\": \"twodesc\",\n" + " \"arguments\": [\"2\"],\n" + - " \"java-options\": [\"--two\"],\n" + - " \"compile-options\": [\"--ctwo\"],\n" + - " \"native-options\": [\"--ntwo\"],\n" + + " \"runtime-options\": [\"--two\"],\n" + + " \"sources\": [\"twosrc\"],\n" + + " \"files\": [\"twofiles\"],\n" + " \"dependencies\": [\"twodep\"],\n" + " \"repositories\": [\"tworepo\"],\n" + " \"classpaths\": [\"twocp\"],\n" + - " \"properties\": {\"two\":\"2\"}\n" + + " \"properties\": {\"two\":\"2\"},\n" + + " \"java\": \"twojava\",\n" + + " \"main\": \"twomain\",\n" + + " \"module\": \"twomodule\",\n" + + " \"compile-options\": [\"--ctwo\"],\n" + + " \"native-image\": true,\n" + + " \"native-options\": [\"--ntwo\"],\n" + + " \"jfr\": \"twojfr\",\n" + + " \"debug\": {\"twod\":\"2\"},\n" + + " \"cds\": true,\n" + + " \"interactive\": true,\n" + + " \"enable-preview\": true,\n" + + " \"enable-assertions\": true,\n" + + " \"enable-system-assertions\": true,\n" + + " \"manifest-options\": {\"twom\":\"2\"},\n" + + " \"java-agents\": [{\"agent-ref\":\"twojag\",\"options\":\"twoopts\"}]\n" + " },\n" + " \"three\": {\n" + " \"script-ref\": \"http://dummy\",\n" + " \"arguments\": [\"3\"],\n" + - " \"java-options\": [\"--three\"],\n" + + " \"runtime-options\": [\"--three\"],\n" + " \"compile-options\": [\"--cthree\"],\n" + " \"native-image\": true,\n" + " \"native-options\": [\"--nthree\"],\n" + @@ -56,14 +71,28 @@ public class TestAlias extends BaseTest { " \"script-ref\": \"three\",\n" + " \"description\": \"fourdesc\",\n" + " \"arguments\": [\"4\"],\n" + - " \"java-options\": [\"--four\"],\n" + - " \"compile-options\": [\"--cfour\"],\n" + - " \"native-image\": false,\n" + - " \"native-options\": [\"--nfour\"],\n" + + " \"runtime-options\": [\"--four\"],\n" + + " \"sources\": [\"foursrc\"],\n" + + " \"files\": [\"fourfiles\"],\n" + " \"dependencies\": [\"fourdep\"],\n" + " \"repositories\": [\"fourrepo\"],\n" + " \"classpaths\": [\"fourcp\"],\n" + - " \"properties\": {\"four\":\"4\"}\n" + + " \"properties\": {\"four\":\"4\"},\n" + + " \"java\": \"fourjava\",\n" + + " \"main\": \"fourmain\",\n" + + " \"module\": \"fourmodule\",\n" + + " \"compile-options\": [\"--cfour\"],\n" + + " \"native-image\": false,\n" + + " \"native-options\": [\"--nfour\"],\n" + + " \"jfr\": \"fourjfr\",\n" + + " \"debug\": {\"fourd\":\"4\"},\n" + + " \"cds\": false,\n" + + " \"interactive\": false,\n" + + " \"enable-preview\": false,\n" + + " \"enable-assertions\": false,\n" + + " \"enable-system-assertions\": false,\n" + + " \"manifest-options\": {\"fourm\":\"4\"},\n" + + " \"java-agents\": [{\"agent-ref\":\"fourjag\",\"options\":\"fouropts\"}]\n" + " },\n" + " \"five\": {\n" + " \"script-ref\": \"three\"\n" + @@ -145,7 +174,6 @@ void testAddWithDefaultCatalogFile2() throws IOException { assertThat(alias.classpaths, contains("cps")); assertThat(alias.properties, aMapWithSize(1)); assertThat(alias.properties, hasEntry("prop", "val")); - assertThat(alias.arguments, iterableWithSize(3)); assertThat(alias.mainClass, is("mainclass")); assertThat(alias.compileOptions, iterableWithSize(1)); assertThat(alias.compileOptions, contains("copts")); @@ -365,6 +393,10 @@ void testGetAliasTwo() throws IOException { assertThat(alias.arguments, contains("2")); assertThat(alias.runtimeOptions, iterableWithSize(1)); assertThat(alias.runtimeOptions, contains("--two")); + assertThat(alias.sources, iterableWithSize(1)); + assertThat(alias.sources, contains("twosrc")); + assertThat(alias.resources, iterableWithSize(1)); + assertThat(alias.resources, contains("twofiles")); assertThat(alias.dependencies, iterableWithSize(1)); assertThat(alias.dependencies, contains("twodep")); assertThat(alias.repositories, iterableWithSize(1)); @@ -373,11 +405,26 @@ void testGetAliasTwo() throws IOException { assertThat(alias.classpaths, contains("twocp")); assertThat(alias.properties, aMapWithSize(1)); assertThat(alias.properties, hasEntry("two", "2")); + assertThat(alias.javaVersion, equalTo("twojava")); + assertThat(alias.mainClass, equalTo("twomain")); + assertThat(alias.moduleName, equalTo("twomodule")); assertThat(alias.compileOptions, iterableWithSize(1)); assertThat(alias.compileOptions, contains("--ctwo")); - assertThat(alias.nativeImage, nullValue()); + assertThat(alias.nativeImage, is(Boolean.TRUE)); assertThat(alias.nativeOptions, iterableWithSize(1)); assertThat(alias.nativeOptions, contains("--ntwo")); + assertThat(alias.jfr, equalTo("twojfr")); + assertThat(alias.debug, aMapWithSize(1)); + assertThat(alias.debug, hasEntry("twod", "2")); + assertThat(alias.cds, is(Boolean.TRUE)); + assertThat(alias.interactive, is(Boolean.TRUE)); + assertThat(alias.enablePreview, is(Boolean.TRUE)); + assertThat(alias.enableAssertions, is(Boolean.TRUE)); + assertThat(alias.enableSystemAssertions, is(Boolean.TRUE)); + assertThat(alias.manifestOptions, aMapWithSize(1)); + assertThat(alias.manifestOptions, hasEntry("twom", "2")); + assertThat(alias.javaAgents, iterableWithSize(1)); + assertThat(alias.javaAgents, contains(new Alias.JavaAgent("twojag", "twoopts"))); } @Test @@ -391,6 +438,10 @@ void testGetAliasFour() throws IOException { assertThat(alias.arguments, contains("4")); assertThat(alias.runtimeOptions, iterableWithSize(1)); assertThat(alias.runtimeOptions, contains("--four")); + assertThat(alias.sources, iterableWithSize(1)); + assertThat(alias.sources, contains("foursrc")); + assertThat(alias.resources, iterableWithSize(1)); + assertThat(alias.resources, contains("fourfiles")); assertThat(alias.dependencies, iterableWithSize(1)); assertThat(alias.dependencies, contains("fourdep")); assertThat(alias.repositories, iterableWithSize(1)); @@ -399,11 +450,26 @@ void testGetAliasFour() throws IOException { assertThat(alias.classpaths, contains("fourcp")); assertThat(alias.properties, aMapWithSize(1)); assertThat(alias.properties, hasEntry("four", "4")); + assertThat(alias.javaVersion, equalTo("fourjava")); + assertThat(alias.mainClass, equalTo("fourmain")); + assertThat(alias.moduleName, equalTo("fourmodule")); assertThat(alias.compileOptions, iterableWithSize(1)); assertThat(alias.compileOptions, contains("--cfour")); assertThat(alias.nativeImage, is(Boolean.FALSE)); assertThat(alias.nativeOptions, iterableWithSize(1)); assertThat(alias.nativeOptions, contains("--nfour")); + assertThat(alias.jfr, equalTo("fourjfr")); + assertThat(alias.debug, aMapWithSize(1)); + assertThat(alias.debug, hasEntry("fourd", "4")); + assertThat(alias.cds, is(Boolean.FALSE)); + assertThat(alias.interactive, is(Boolean.FALSE)); + assertThat(alias.enablePreview, is(Boolean.FALSE)); + assertThat(alias.enableAssertions, is(Boolean.FALSE)); + assertThat(alias.enableSystemAssertions, is(Boolean.FALSE)); + assertThat(alias.manifestOptions, aMapWithSize(1)); + assertThat(alias.manifestOptions, hasEntry("fourm", "4")); + assertThat(alias.javaAgents, iterableWithSize(1)); + assertThat(alias.javaAgents, contains(new Alias.JavaAgent("fourjag", "fouropts"))); } @Test diff --git a/src/test/java/dev/jbang/source/TestProjectBuilder.java b/src/test/java/dev/jbang/source/TestProjectBuilder.java index 4f2cdb736..64bef3e27 100644 --- a/src/test/java/dev/jbang/source/TestProjectBuilder.java +++ b/src/test/java/dev/jbang/source/TestProjectBuilder.java @@ -1,17 +1,56 @@ package dev.jbang.source; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.*; import static org.junit.jupiter.api.Assertions.assertThrows; +import java.io.IOException; import java.nio.file.Path; +import java.nio.file.Paths; import java.util.Arrays; import org.junit.jupiter.api.Test; import dev.jbang.BaseTest; import dev.jbang.cli.ExitException; +import dev.jbang.dependencies.MavenRepo; +import dev.jbang.source.resolvers.AliasResourceResolver; +import dev.jbang.util.Util; public class TestProjectBuilder extends BaseTest { + static final String aliases = "{\n" + + " \"aliases\": {\n" + + " \"test\": {\n" + + " \"script-ref\": \"alltags.java\",\n" + + " \"description\": \"twodesc\",\n" + + " \"arguments\": [\"2\"],\n" + + " \"runtime-options\": [\"--two\"],\n" + + " \"sources\": [\"twosrc\"],\n" + + " \"files\": [\"twofiles\"],\n" + + " \"dependencies\": [\"twodep\"],\n" + + " \"repositories\": [\"tworepo\"],\n" + + " \"classpaths\": [\"twocp\"],\n" + + " \"properties\": {\"two\":\"2\"},\n" + + " \"java\": \"twojava\",\n" + + " \"main\": \"twomain\",\n" + + " \"module\": \"twomodule\",\n" + + " \"compile-options\": [\"--ctwo\"],\n" + + " \"native-image\": true,\n" + + " \"native-options\": [\"--ntwo\"],\n" + + " \"jfr\": \"twojfr\",\n" + + " \"debug\": {\"twod\":\"2\"},\n" + + " \"cds\": true,\n" + + " \"interactive\": true,\n" + + " \"enable-preview\": true,\n" + + " \"enable-assertions\": true,\n" + + " \"enable-system-assertions\": true,\n" + + " \"manifest-options\": {\"twom\":\"2\"},\n" + + " \"java-agents\": [{\"agent-ref\":\"twojag\",\"options\":\"twoopts\"}]\n" + + " }\n" + + " }\n" + + "}"; + @Test void testDuplicateAnonRepos() { ProjectBuilder pb = Project.builder(); @@ -44,4 +83,250 @@ void testReposSameIdDifferentUrl() { BuildContext.forProject(prj).resolveClassPath(); }); } + + @Test + void testSourceTags() { + ProjectBuilder pb = Project.builder(); + Path src = examplesTestFolder.resolve("alltags.java"); + Project prj = pb.build(src); + assertThat(prj.getDescription().get(), equalTo("some description")); + assertThat(prj.getRuntimeOptions(), iterableWithSize(2)); + assertThat(prj.getRuntimeOptions(), contains("--add-opens", "java.base/java.net=ALL-UNNAMED")); + assertThat(prj.getMainSourceSet().getSources(), iterableWithSize(6)); + assertThat(prj.getMainSourceSet().getSources(), containsInAnyOrder( + ResourceRef.forFile(src), + ResourceRef.forFile(examplesTestFolder.resolve("Two.java")), + ResourceRef.forFile(examplesTestFolder.resolve("gh_fetch_release_assets.java")), + ResourceRef.forFile(examplesTestFolder.resolve("gh_release_stats.java")), + ResourceRef.forFile(examplesTestFolder.resolve("nested/NestedTwo.java")), + ResourceRef.forFile(examplesTestFolder.resolve("nested/NestedOne.java")))); + assertThat(prj.getMainSourceSet().getResources(), iterableWithSize(3)); + assertThat(prj.getMainSourceSet().getResources(), containsInAnyOrder( + RefTarget.create(ResourceRef.forFile(examplesTestFolder.resolve("res/resource.properties")), null), + RefTarget.create(ResourceRef.forFile(examplesTestFolder.resolve("res/resource.properties")), + Paths.get("renamed.properties")), + RefTarget.create(ResourceRef.forFile(examplesTestFolder.resolve("res/resource.properties")), + Paths.get("META-INF/application.properties")))); + assertThat(prj.getMainSourceSet().getDependencies(), iterableWithSize(5)); + assertThat(prj.getMainSourceSet().getDependencies(), contains( + "dummy.org:dummy:1.2.3", + "info.picocli:picocli:4.6.3", + "org.kohsuke:github-api:1.116", + "info.picocli:picocli:4.6.3", + "org.kohsuke:github-api:1.116")); + assertThat(prj.getRepositories(), iterableWithSize(1)); + assertThat(prj.getRepositories(), contains(new MavenRepo("dummy", "http://dummy"))); + assertThat(prj.getMainSourceSet().getClassPaths(), iterableWithSize(0)); + assertThat(prj.getProperties(), anEmptyMap()); + assertThat(prj.getJavaVersion(), equalTo("11+")); + assertThat(prj.getMainClass(), equalTo("mainclass")); + assertThat(prj.getModuleName().get(), equalTo("mymodule")); + assertThat(prj.getMainSourceSet().getCompileOptions(), iterableWithSize(3)); + assertThat(prj.getMainSourceSet().getCompileOptions(), contains("-g", "--enable-preview", "--verbose")); + assertThat(prj.isNativeImage(), is(Boolean.FALSE)); + assertThat(prj.getMainSourceSet().getNativeOptions(), iterableWithSize(2)); + assertThat(prj.getMainSourceSet().getNativeOptions(), contains("-O1", "-d")); + assertThat(prj.enableCDS(), is(Boolean.TRUE)); + assertThat(prj.enablePreview(), is(Boolean.TRUE)); + assertThat(prj.getManifestAttributes(), aMapWithSize(3)); + assertThat(prj.getManifestAttributes(), hasEntry("one", "1")); + assertThat(prj.getManifestAttributes(), hasEntry("two", "2")); + assertThat(prj.getManifestAttributes(), hasEntry("three", "3")); + } + + @Test + void testJar() { + ProjectBuilder pb = Project.builder(); + Path src = examplesTestFolder.resolve("hellojar.jar"); + Project prj = pb.build(src); + assertThat(prj.getDescription().isPresent(), equalTo(Boolean.FALSE)); + assertThat(prj.getRuntimeOptions(), iterableWithSize(0)); + assertThat(prj.getMainSourceSet().getSources(), iterableWithSize(0)); + assertThat(prj.getMainSourceSet().getResources(), iterableWithSize(0)); + assertThat(prj.getMainSourceSet().getDependencies(), iterableWithSize(0)); + assertThat(prj.getRepositories(), iterableWithSize(0)); + assertThat(prj.getMainSourceSet().getClassPaths(), iterableWithSize(0)); + assertThat(prj.getProperties(), anEmptyMap()); + assertThat(prj.getJavaVersion(), equalTo("8+")); + assertThat(prj.getMainClass(), equalTo("helloworld")); + assertThat(prj.getModuleName().isPresent(), equalTo(Boolean.FALSE)); + assertThat(prj.getMainSourceSet().getCompileOptions(), iterableWithSize(0)); + assertThat(prj.isNativeImage(), is(Boolean.FALSE)); + assertThat(prj.getMainSourceSet().getNativeOptions(), iterableWithSize(0)); + assertThat(prj.enableCDS(), is(Boolean.FALSE)); + assertThat(prj.enablePreview(), is(Boolean.FALSE)); + assertThat(prj.getManifestAttributes(), aMapWithSize(0)); + } + + @Test + void testGAV() { + ProjectBuilder pb = Project.builder(); + String gav = "org.eclipse.jgit:org.eclipse.jgit.pgm:5.9.0.202009080501-r"; + Project prj = pb.build(gav); + assertThat(prj.getDescription().isPresent(), equalTo(Boolean.FALSE)); + assertThat(prj.getRuntimeOptions(), iterableWithSize(0)); + assertThat(prj.getMainSourceSet().getSources(), iterableWithSize(0)); + assertThat(prj.getMainSourceSet().getResources(), iterableWithSize(0)); + assertThat(prj.getMainSourceSet().getDependencies(), iterableWithSize(1)); + assertThat(prj.getMainSourceSet().getDependencies(), contains(gav)); + assertThat(prj.getRepositories(), iterableWithSize(0)); + assertThat(prj.getMainSourceSet().getClassPaths(), iterableWithSize(0)); + assertThat(prj.getProperties(), anEmptyMap()); + assertThat(prj.getJavaVersion(), nullValue()); + assertThat(prj.getMainClass(), equalTo("org.eclipse.jgit.pgm.Main")); + assertThat(prj.getModuleName().isPresent(), equalTo(Boolean.FALSE)); + assertThat(prj.getMainSourceSet().getCompileOptions(), iterableWithSize(0)); + assertThat(prj.isNativeImage(), is(Boolean.FALSE)); + assertThat(prj.getMainSourceSet().getNativeOptions(), iterableWithSize(0)); + assertThat(prj.enableCDS(), is(Boolean.FALSE)); + assertThat(prj.enablePreview(), is(Boolean.FALSE)); + assertThat(prj.getManifestAttributes(), aMapWithSize(0)); + } + + @Test + void testAliasSource() throws IOException { + Util.setCwd(examplesTestFolder); + ProjectBuilder pb = Project.builder(); + Path src = examplesTestFolder.resolve("alltags.java"); + Project prj = pb.build("alltags"); + assertThat(prj.getDescription().get(), equalTo("some description")); + assertThat(prj.getRuntimeOptions(), iterableWithSize(4)); + assertThat(prj.getRuntimeOptions(), + contains("--add-opens", "java.base/java.net=ALL-UNNAMED", "-Dfoo=bar", "-Dbar=aap noot mies")); + assertThat(prj.getMainSourceSet().getSources(), iterableWithSize(7)); + assertThat(prj.getMainSourceSet().getSources(), containsInAnyOrder( + new AliasResourceResolver.AliasedResourceRef(src.toString(), src, null), + ResourceRef.forFile(examplesTestFolder.resolve("Two.java")), + ResourceRef.forFile(examplesTestFolder.resolve("gh_fetch_release_assets.java")), + ResourceRef.forFile(examplesTestFolder.resolve("gh_release_stats.java")), + ResourceRef.forFile(examplesTestFolder.resolve("nested/NestedTwo.java")), + ResourceRef.forFile(examplesTestFolder.resolve("nested/NestedOne.java")), + ResourceRef.forNamedFile("helloworld.java", examplesTestFolder.resolve("helloworld.java")))); + assertThat(prj.getMainSourceSet().getResources(), iterableWithSize(4)); + assertThat(prj.getMainSourceSet().getResources(), containsInAnyOrder( + RefTarget.create(ResourceRef.forFile(examplesTestFolder.resolve("res/resource.properties")), null), + RefTarget.create(ResourceRef.forFile(examplesTestFolder.resolve("res/resource.properties")), + Paths.get("renamed.properties")), + RefTarget.create(ResourceRef.forFile(examplesTestFolder.resolve("res/resource.properties")), + Paths.get("META-INF/application.properties")), + RefTarget.create(ResourceRef.forNamedFile("res/test.properties", + examplesTestFolder.resolve("res/test.properties")), null))); + assertThat(prj.getMainSourceSet().getDependencies(), iterableWithSize(6)); + assertThat(prj.getMainSourceSet().getDependencies(), contains( + "dummy.org:dummy:1.2.3", + "info.picocli:picocli:4.6.3", + "org.kohsuke:github-api:1.116", + "info.picocli:picocli:4.6.3", + "org.kohsuke:github-api:1.116", + "twodep")); + assertThat(prj.getRepositories(), iterableWithSize(2)); + assertThat(prj.getRepositories(), + contains(new MavenRepo("dummy", "http://dummy"), new MavenRepo("tworepo", "tworepo"))); + assertThat(prj.getMainSourceSet().getClassPaths(), iterableWithSize(1)); + assertThat(prj.getMainSourceSet().getClassPaths(), contains("twocp")); + assertThat(prj.getProperties(), aMapWithSize(1)); + assertThat(prj.getProperties(), hasEntry("two", "2")); + assertThat(prj.getJavaVersion(), equalTo("twojava")); + assertThat(prj.getMainClass(), equalTo("mainclass")); // This is not updated from Alias here! + assertThat(prj.getModuleName().get(), equalTo("mymodule")); // This is not updated from Alias here! + assertThat(prj.getMainSourceSet().getCompileOptions(), iterableWithSize(4)); + assertThat(prj.getMainSourceSet().getCompileOptions(), + contains("-g", "--enable-preview", "--verbose", "--ctwo")); + assertThat(prj.isNativeImage(), is(Boolean.TRUE)); + assertThat(prj.getMainSourceSet().getNativeOptions(), iterableWithSize(4)); + assertThat(prj.getMainSourceSet().getNativeOptions(), contains("-O1", "-d", "-O1", "--ntwo")); + assertThat(prj.enableCDS(), is(Boolean.TRUE)); + assertThat(prj.enablePreview(), is(Boolean.TRUE)); + assertThat(prj.getManifestAttributes(), aMapWithSize(7)); + assertThat(prj.getManifestAttributes(), hasEntry("one", "1")); + assertThat(prj.getManifestAttributes(), hasEntry("two", "2")); + assertThat(prj.getManifestAttributes(), hasEntry("three", "3")); + assertThat(prj.getManifestAttributes(), hasEntry("foo", "true")); + assertThat(prj.getManifestAttributes(), hasEntry("bar", "baz")); + assertThat(prj.getManifestAttributes(), hasEntry("baz", "nada")); + assertThat(prj.getManifestAttributes(), hasEntry("twom", "2")); + } + + @Test + void testAliasJar() throws IOException { + Util.setCwd(examplesTestFolder); + ProjectBuilder pb = Project.builder(); + Project prj = pb.build("hellojar"); + assertThat(prj.getDescription().isPresent(), equalTo(Boolean.FALSE)); + assertThat(prj.getRuntimeOptions(), iterableWithSize(2)); + assertThat(prj.getRuntimeOptions(), contains("-Dfoo=bar", "-Dbar=aap noot mies")); + assertThat(prj.getMainSourceSet().getSources(), iterableWithSize(1)); + assertThat(prj.getMainSourceSet().getSources(), containsInAnyOrder( + ResourceRef.forNamedFile("helloworld.java", examplesTestFolder.resolve("helloworld.java")))); + assertThat(prj.getMainSourceSet().getResources(), iterableWithSize(1)); + assertThat(prj.getMainSourceSet().getResources(), containsInAnyOrder( + RefTarget.create(ResourceRef.forNamedFile("res/test.properties", + examplesTestFolder.resolve("res/test.properties")), null))); + assertThat(prj.getMainSourceSet().getDependencies(), iterableWithSize(1)); + assertThat(prj.getMainSourceSet().getDependencies(), contains("twodep")); + assertThat(prj.getRepositories(), iterableWithSize(1)); + assertThat(prj.getRepositories(), contains(new MavenRepo("tworepo", "tworepo"))); + assertThat(prj.getMainSourceSet().getClassPaths(), iterableWithSize(1)); + assertThat(prj.getMainSourceSet().getClassPaths(), contains("twocp")); + assertThat(prj.getProperties(), aMapWithSize(1)); + assertThat(prj.getProperties(), hasEntry("two", "2")); + assertThat(prj.getJavaVersion(), equalTo("twojava")); + assertThat(prj.getMainClass(), equalTo("helloworld")); // This is not updated from Alias here! + assertThat(prj.getModuleName().isPresent(), equalTo(Boolean.FALSE)); // This is not updated from Alias here! + assertThat(prj.getMainSourceSet().getCompileOptions(), iterableWithSize(1)); + assertThat(prj.getMainSourceSet().getCompileOptions(), contains("--ctwo")); + assertThat(prj.isNativeImage(), is(Boolean.TRUE)); + assertThat(prj.getMainSourceSet().getNativeOptions(), iterableWithSize(2)); + assertThat(prj.getMainSourceSet().getNativeOptions(), contains("-O1", "--ntwo")); + assertThat(prj.enableCDS(), is(Boolean.FALSE)); // This is not updated from Alias here! + assertThat(prj.enablePreview(), is(Boolean.TRUE)); + assertThat(prj.getManifestAttributes(), aMapWithSize(4)); + assertThat(prj.getManifestAttributes(), hasEntry("foo", "true")); + assertThat(prj.getManifestAttributes(), hasEntry("bar", "baz")); + assertThat(prj.getManifestAttributes(), hasEntry("baz", "nada")); + assertThat(prj.getManifestAttributes(), hasEntry("twom", "2")); + } + + @Test + void testAliasGAV() throws IOException { + Util.setCwd(examplesTestFolder); + ProjectBuilder pb = Project.builder(); + String gav = "org.eclipse.jgit:org.eclipse.jgit.pgm:5.9.0.202009080501-r"; + Project prj = pb.build("pgmgav"); + assertThat(prj.getDescription().isPresent(), equalTo(Boolean.FALSE)); + assertThat(prj.getRuntimeOptions(), iterableWithSize(2)); + assertThat(prj.getRuntimeOptions(), contains("-Dfoo=bar", "-Dbar=aap noot mies")); + assertThat(prj.getMainSourceSet().getSources(), iterableWithSize(1)); + assertThat(prj.getMainSourceSet().getSources(), containsInAnyOrder( + ResourceRef.forNamedFile("helloworld.java", examplesTestFolder.resolve("helloworld.java")))); + assertThat(prj.getMainSourceSet().getResources(), iterableWithSize(1)); + assertThat(prj.getMainSourceSet().getResources(), containsInAnyOrder( + RefTarget.create(ResourceRef.forNamedFile("res/test.properties", + examplesTestFolder.resolve("res/test.properties")), null))); + assertThat(prj.getMainSourceSet().getDependencies(), iterableWithSize(2)); + assertThat(prj.getMainSourceSet().getDependencies(), contains( + gav, "info.picocli:picocli:4.6.3")); + assertThat(prj.getRepositories(), iterableWithSize(2)); + assertThat(prj.getRepositories(), contains(new MavenRepo("mavencentral", "https://repo1.maven.org/maven2/"), + new MavenRepo("tworepo", "tworepo"))); + assertThat(prj.getMainSourceSet().getClassPaths(), iterableWithSize(1)); + assertThat(prj.getMainSourceSet().getClassPaths(), contains("twocp")); + assertThat(prj.getProperties(), aMapWithSize(1)); + assertThat(prj.getProperties(), hasEntry("two", "2")); + assertThat(prj.getJavaVersion(), equalTo("twojava")); + assertThat(prj.getMainClass(), equalTo("org.eclipse.jgit.pgm.Main")); // This is not updated from Alias here! + assertThat(prj.getModuleName().isPresent(), equalTo(Boolean.FALSE)); // This is not updated from Alias here! + assertThat(prj.getMainSourceSet().getCompileOptions(), iterableWithSize(1)); + assertThat(prj.getMainSourceSet().getCompileOptions(), contains("--ctwo")); + assertThat(prj.isNativeImage(), is(Boolean.TRUE)); + assertThat(prj.getMainSourceSet().getNativeOptions(), iterableWithSize(2)); + assertThat(prj.getMainSourceSet().getNativeOptions(), contains("-O1", "--ntwo")); + assertThat(prj.enableCDS(), is(Boolean.FALSE)); // This is not updated from Alias here! + assertThat(prj.enablePreview(), is(Boolean.TRUE)); + assertThat(prj.getManifestAttributes(), aMapWithSize(4)); + assertThat(prj.getManifestAttributes(), hasEntry("foo", "true")); + assertThat(prj.getManifestAttributes(), hasEntry("bar", "baz")); + assertThat(prj.getManifestAttributes(), hasEntry("baz", "nada")); + assertThat(prj.getManifestAttributes(), hasEntry("twom", "2")); + } }