diff --git a/.gitignore b/.gitignore index fcaa865..73f11c4 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ build/ *.iml out/ +/test-project/gradle.properties diff --git a/build.gradle b/build.gradle index 5811ecd..49abbda 100644 --- a/build.gradle +++ b/build.gradle @@ -8,7 +8,7 @@ group = 'org.openjfx' version = '0.1.1-SNAPSHOT' java { - toolchain.languageVersion = JavaLanguageVersion.of(11) + toolchain.languageVersion = JavaLanguageVersion.of(8) } repositories { @@ -26,6 +26,15 @@ dependencies { tasks.named('test', Test) { useJUnitPlatform() + + def java11Home = javaToolchains + .launcherFor { languageVersion.set(JavaLanguageVersion.of(11)) } + .get() + .metadata + .installationPath + .asFile + .absolutePath + systemProperty("java11Home", java11Home) } gradlePlugin { diff --git a/src/main/java/org/openjfx/gradle/JavaFXModule.java b/src/main/java/org/openjfx/gradle/JavaFXModule.java index 00079ce..7a355f2 100644 --- a/src/main/java/org/openjfx/gradle/JavaFXModule.java +++ b/src/main/java/org/openjfx/gradle/JavaFXModule.java @@ -31,6 +31,7 @@ import org.gradle.api.GradleException; +import java.util.Arrays; import java.util.List; import java.util.Locale; import java.util.Optional; @@ -55,7 +56,7 @@ public enum JavaFXModule { private final List dependentModules; JavaFXModule(JavaFXModule...dependentModules) { - this.dependentModules = List.of(dependentModules); + this.dependentModules = Arrays.asList(dependentModules); } public static Optional fromModuleName(String moduleName) { @@ -86,13 +87,14 @@ public static Set getJavaFXModules(List moduleNames) { return moduleNames.stream() .map(JavaFXModule::fromModuleName) - .flatMap(Optional::stream) + .filter(Optional::isPresent) + .map(Optional::get) .collect(Collectors.toSet()); } public static void validateModules(List moduleNames) { - var invalidModules = moduleNames.stream() - .filter(module -> JavaFXModule.fromModuleName(module).isEmpty()) + List invalidModules = moduleNames.stream() + .filter(module -> !JavaFXModule.fromModuleName(module).isPresent()) .collect(Collectors.toList()); if (! invalidModules.isEmpty()) { diff --git a/src/main/java/org/openjfx/gradle/JavaFXOptions.java b/src/main/java/org/openjfx/gradle/JavaFXOptions.java index ed21a13..5376eac 100644 --- a/src/main/java/org/openjfx/gradle/JavaFXOptions.java +++ b/src/main/java/org/openjfx/gradle/JavaFXOptions.java @@ -43,6 +43,7 @@ import javax.inject.Inject; import java.io.File; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -177,7 +178,7 @@ public void setModules(List modules) { } public void modules(String...moduleNames) { - setModules(List.of(moduleNames)); + setModules(Arrays.asList(moduleNames)); } private void declareFXDependencies(String conf) { @@ -185,12 +186,12 @@ private void declareFXDependencies(String conf) { // This allows users to make multiple modifications to the 'configurations' list at arbitrary times during // build configuration. getConfigurationContainer().getByName(conf).withDependencies(dependencySet -> { - if (!List.of(configurations).contains(conf)) { + if (!Arrays.asList(configurations).contains(conf)) { // configuration was removed: do nothing return; } - var javaFXModules = JavaFXModule.getJavaFXModules(this.modules); + Set javaFXModules = JavaFXModule.getJavaFXModules(this.modules); if (customSDKArtifactRepository == null) { javaFXModules.stream() .sorted() @@ -203,16 +204,18 @@ private void declareFXDependencies(String conf) { // Use the list of dependencies of each module to also add direct dependencies for those. // This is needed, because there is no information about transitive dependencies in the metadata // of the modules (as there is no such metadata in the local sdk). - var javaFXModulesWithTransitives = Stream.concat( + Stream javaFXModulesWithTransitives = Stream.concat( javaFXModules.stream(), javaFXModules.stream() .flatMap(m -> m.getDependentModules().stream())) .distinct() .sorted(); - javaFXModulesWithTransitives.forEach(javaFXModule -> - dependencySet.add(getDependencies().create( - Map.of("name", javaFXModule.getModuleName())))); + javaFXModulesWithTransitives.forEach(javaFXModule -> { + Map dependencyNotation = new HashMap<>(); + dependencyNotation.put("name", javaFXModule.getModuleName()); + dependencySet.add(getDependencies().create(dependencyNotation)); + }); } }); } diff --git a/src/main/java/org/openjfx/gradle/JavaFXPlugin.java b/src/main/java/org/openjfx/gradle/JavaFXPlugin.java index 47f040c..dd3bf3a 100644 --- a/src/main/java/org/openjfx/gradle/JavaFXPlugin.java +++ b/src/main/java/org/openjfx/gradle/JavaFXPlugin.java @@ -111,9 +111,9 @@ private static void putJavaFXJarsOnModulePathForClasspathApplication(JavaExec ex FileCollection classpath = execTask.getClasspath(); execTask.setClasspath(classpath.filter(jar -> !isJavaFXJar(jar, javaFXOptions.getPlatform()))); - var modulePath = classpath.filter(jar -> isJavaFXJar(jar, javaFXOptions.getPlatform())); + FileCollection modulePath = classpath.filter(jar -> isJavaFXJar(jar, javaFXOptions.getPlatform())); - execTask.getJvmArgumentProviders().add(() -> List.of( + execTask.getJvmArgumentProviders().add(() -> Arrays.asList( "--module-path", modulePath.getAsPath(), "--add-modules", String.join(",", javaFXOptions.getModules()) )); diff --git a/src/main/java/org/openjfx/gradle/metadatarule/JavaFXComponentMetadataRule.java b/src/main/java/org/openjfx/gradle/metadatarule/JavaFXComponentMetadataRule.java index f69dc99..868b94a 100644 --- a/src/main/java/org/openjfx/gradle/metadatarule/JavaFXComponentMetadataRule.java +++ b/src/main/java/org/openjfx/gradle/metadatarule/JavaFXComponentMetadataRule.java @@ -56,7 +56,7 @@ abstract public class JavaFXComponentMetadataRule implements ComponentMetadataRu @Override public void execute(ComponentMetadataContext context) { - var details = context.getDetails(); + ComponentMetadataDetails details = context.getDetails(); for (JavaFXPlatform javaFXPlatform : JavaFXPlatform.values()) { addJavaFXPlatformVariant(javaFXPlatform, details, "Compile", "compile"); @@ -65,8 +65,8 @@ public void execute(ComponentMetadataContext context) { } private void addJavaFXPlatformVariant(JavaFXPlatform javaFXPlatform, ComponentMetadataDetails details, String nameSuffix, String baseVariant) { - var name = details.getId().getName(); - var version = details.getId().getVersion(); + String name = details.getId().getName(); + String version = details.getId().getVersion(); // Use 'maybeAddVariant'. As long as the metadata is sourced from POM, 'compile' and 'runtime' exist. // These are used as base for the additional variants so that those variants have the same dependencies. diff --git a/src/test/java/org/openjfx/gradle/JavaFXModuleTest.java b/src/test/java/org/openjfx/gradle/JavaFXModuleTest.java index 4bc58bc..f426efd 100644 --- a/src/test/java/org/openjfx/gradle/JavaFXModuleTest.java +++ b/src/test/java/org/openjfx/gradle/JavaFXModuleTest.java @@ -32,10 +32,12 @@ import org.gradle.api.GradleException; import org.junit.jupiter.api.Test; +import java.util.Arrays; import java.util.List; import java.util.Optional; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.fail; @@ -50,7 +52,7 @@ void existingModuleName() { @Test void nonExistingModuleName() { - assertTrue(JavaFXModule.fromModuleName("javafx.unknown").isEmpty()); + assertFalse(JavaFXModule.fromModuleName("javafx.unknown").isPresent()); } @Test @@ -65,14 +67,14 @@ void getArtifactName() { @Test void validateWithValidModules() { - var moduleNames = List.of(JavaFXModule.CONTROLS.getModuleName(), JavaFXModule.WEB.getModuleName()); + List moduleNames = Arrays.asList(JavaFXModule.CONTROLS.getModuleName(), JavaFXModule.WEB.getModuleName()); JavaFXModule.validateModules(moduleNames); } @Test void validateWithInvalidModules() { - var moduleNames = List.of("javafx.unknown", JavaFXModule.CONTROLS.getModuleName(), JavaFXModule.WEB.getModuleName()); + List moduleNames = Arrays.asList("javafx.unknown", JavaFXModule.CONTROLS.getModuleName(), JavaFXModule.WEB.getModuleName()); try { JavaFXModule.validateModules(moduleNames); diff --git a/src/test/java/org/openjfx/gradle/JavaFXPluginSmokeTest.java b/src/test/java/org/openjfx/gradle/JavaFXPluginSmokeTest.java index a94d96f..464cb0d 100644 --- a/src/test/java/org/openjfx/gradle/JavaFXPluginSmokeTest.java +++ b/src/test/java/org/openjfx/gradle/JavaFXPluginSmokeTest.java @@ -29,6 +29,7 @@ */ package org.openjfx.gradle; +import org.gradle.api.Project; import org.gradle.testfixtures.ProjectBuilder; import org.gradle.testkit.runner.BuildResult; import org.gradle.testkit.runner.GradleRunner; @@ -36,9 +37,13 @@ import org.junit.jupiter.api.Test; import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; import java.lang.management.ManagementFactory; import java.util.Arrays; import java.util.List; +import java.util.Properties; import java.util.stream.Collectors; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -48,7 +53,7 @@ abstract class JavaFXPluginSmokeTest { private static final String classifier; static { - var p = ProjectBuilder.builder().build(); + Project p = ProjectBuilder.builder().build(); p.getPlugins().apply(JavaFXPlugin.class); classifier = p.getExtensions().getByType(JavaFXOptions.class).getPlatform().getClassifier(); } @@ -61,60 +66,60 @@ protected String modularApplicationRuntime() { @Test void smokeTestModular() { - var result = build(":modular:run"); + BuildResult result = buildWithJava11(":modular:run"); assertEquals(TaskOutcome.SUCCESS, result.task(":modular:run").getOutcome()); - assertEquals(List.of("javafx-base-17-" + classifier + ".jar", "javafx-controls-17-" + classifier + ".jar", "javafx-graphics-17-" + classifier + ".jar"), modulePath(result).get(0)); - assertEquals(List.of("javafx-base-17-" + classifier + ".jar", "javafx-controls-17-" + classifier + ".jar", "javafx-graphics-17-" + classifier + ".jar", "modular.jar"), modulePath(result).get(1)); + assertEquals(Arrays.asList("javafx-base-17-" + classifier + ".jar", "javafx-controls-17-" + classifier + ".jar", "javafx-graphics-17-" + classifier + ".jar"), modulePath(result).get(0)); + assertEquals(Arrays.asList("javafx-base-17-" + classifier + ".jar", "javafx-controls-17-" + classifier + ".jar", "javafx-graphics-17-" + classifier + ".jar", "modular.jar"), modulePath(result).get(1)); - assertEquals(List.of(), compileClassPath(result)); - assertEquals(List.of(), runtimeClassPath(result)); + assertEquals(Arrays.asList(), compileClassPath(result)); + assertEquals(Arrays.asList(), runtimeClassPath(result)); } @Test void smokeTestModularWithModularityPlugin() { - var result = build(":modular-with-modularity-plugin:run"); + BuildResult result = buildWithJava11(":modular-with-modularity-plugin:run"); assertEquals(TaskOutcome.SUCCESS, result.task(":modular-with-modularity-plugin:run").getOutcome()); - assertEquals(List.of("javafx-base-17-" + classifier + ".jar", "javafx-controls-17-" + classifier + ".jar", "javafx-graphics-17-" + classifier + ".jar"), modulePath(result).get(0)); - assertEquals(List.of("javafx-base-17-" + classifier + ".jar", "javafx-controls-17-" + classifier + ".jar", "javafx-graphics-17-" + classifier + ".jar", modularApplicationRuntime()), modulePath(result).get(1)); + assertEquals(Arrays.asList("javafx-base-17-" + classifier + ".jar", "javafx-controls-17-" + classifier + ".jar", "javafx-graphics-17-" + classifier + ".jar"), modulePath(result).get(0)); + assertEquals(Arrays.asList("javafx-base-17-" + classifier + ".jar", "javafx-controls-17-" + classifier + ".jar", "javafx-graphics-17-" + classifier + ".jar", modularApplicationRuntime()), modulePath(result).get(1)); - assertEquals(List.of(), compileClassPath(result)); - assertEquals(List.of(), runtimeClassPath(result)); + assertEquals(Arrays.asList(), compileClassPath(result)); + assertEquals(Arrays.asList(), runtimeClassPath(result)); } @Test void smokeTestNonModular() { - var result = build(":non-modular:run"); + BuildResult result = buildWithJava11(":non-modular:run"); assertEquals(TaskOutcome.SUCCESS, result.task(":non-modular:run").getOutcome()); - assertEquals(List.of("javafx-base-17-" + classifier + ".jar", "javafx-controls-17-" + classifier + ".jar", "javafx-graphics-17-" + classifier + ".jar", "javafx-media-17-" + classifier + ".jar", "javafx-web-17-" + classifier + ".jar"), compileClassPath(result).get(0)); - assertEquals(List.of("main", "main"), runtimeClassPath(result).get(0)); - assertEquals(List.of("javafx-base-17-" + classifier + ".jar", "javafx-controls-17-" + classifier + ".jar", "javafx-graphics-17-" + classifier + ".jar", "javafx-media-17-" + classifier + ".jar", "javafx-web-17-" + classifier + ".jar"), modulePath(result).get(0)); + assertEquals(Arrays.asList("javafx-base-17-" + classifier + ".jar", "javafx-controls-17-" + classifier + ".jar", "javafx-graphics-17-" + classifier + ".jar", "javafx-media-17-" + classifier + ".jar", "javafx-web-17-" + classifier + ".jar"), compileClassPath(result).get(0)); + assertEquals(Arrays.asList("main", "main"), runtimeClassPath(result).get(0)); + assertEquals(Arrays.asList("javafx-base-17-" + classifier + ".jar", "javafx-controls-17-" + classifier + ".jar", "javafx-graphics-17-" + classifier + ".jar", "javafx-media-17-" + classifier + ".jar", "javafx-web-17-" + classifier + ".jar"), modulePath(result).get(0)); } @Test void smokeTestTransitive() { - var result = build(":transitive:run"); + BuildResult result = buildWithJava11(":transitive:run"); assertEquals(TaskOutcome.SUCCESS, result.task(":transitive:run").getOutcome()); - assertEquals(List.of("charts-17.1.41.jar", "javafx-base-17-" + classifier + ".jar", "javafx-controls-17-" + classifier + ".jar", "javafx-graphics-17-" + classifier + ".jar"), compileClassPath(result).get(0)); - assertEquals(List.of("charts-17.1.41.jar", "countries-17.0.29.jar", "heatmap-17.0.17.jar", "logback-classic-1.2.6.jar", "logback-core-1.2.6.jar", "main", "main", "slf4j-api-1.7.32.jar", "toolbox-17.0.45.jar", "toolboxfx-17.0.37.jar"), runtimeClassPath(result).get(0)); - assertEquals(List.of("javafx-base-17.0.6-" + classifier + ".jar", "javafx-controls-17.0.6-" + classifier + ".jar", "javafx-graphics-17.0.6-" + classifier + ".jar", "javafx-swing-17.0.6-" + classifier + ".jar"), modulePath(result).get(0)); + assertEquals(Arrays.asList("charts-17.1.41.jar", "javafx-base-17-" + classifier + ".jar", "javafx-controls-17-" + classifier + ".jar", "javafx-graphics-17-" + classifier + ".jar"), compileClassPath(result).get(0)); + assertEquals(Arrays.asList("charts-17.1.41.jar", "countries-17.0.29.jar", "heatmap-17.0.17.jar", "logback-classic-1.2.6.jar", "logback-core-1.2.6.jar", "main", "main", "slf4j-api-1.7.32.jar", "toolbox-17.0.45.jar", "toolboxfx-17.0.37.jar"), runtimeClassPath(result).get(0)); + assertEquals(Arrays.asList("javafx-base-17.0.6-" + classifier + ".jar", "javafx-controls-17.0.6-" + classifier + ".jar", "javafx-graphics-17.0.6-" + classifier + ".jar", "javafx-swing-17.0.6-" + classifier + ".jar"), modulePath(result).get(0)); } @Test void smokeTestLocalSdk() { - var result = build(":local-sdk:build"); // do not ':run', as it won't run on any platform + BuildResult result = buildWithJava11(":local-sdk:build"); // do not ':run', as it won't run on any platform assertEquals(TaskOutcome.SUCCESS, result.task(":local-sdk:build").getOutcome()); - assertEquals(List.of("javafx.base.jar", "javafx.controls.jar", "javafx.graphics.jar"), compileClassPath(result).get(0)); - assertEquals(List.of(), modulePath(result)); + assertEquals(Arrays.asList("javafx.base.jar", "javafx.controls.jar", "javafx.graphics.jar"), compileClassPath(result).get(0)); + assertEquals(Arrays.asList(), modulePath(result)); } private static List> modulePath(BuildResult result) { @@ -132,11 +137,11 @@ private static List> runtimeClassPath(BuildResult result) { private static List> path(BuildResult result, String pathArg) { // Parse classpath or module path from Gradle's '--debug' output. // The :compileJava and :run tasks log them on that logging level. - return result.getOutput().lines().filter(l -> l.contains(pathArg)).map(l -> { + return Arrays.stream(result.getOutput().split("\r?\n|\r")).filter(l -> l.contains(pathArg)).map(l -> { int pathArgIndex = l.indexOf(pathArg) + pathArg.length(); String pathString = l.substring(pathArgIndex, l.indexOf(" ", pathArgIndex)); - if (pathString.isBlank()) { - return List.of(); + if (pathString.trim().isEmpty()) { + return Arrays.asList(); } String[] path = pathString.split(System.getProperty("path.separator")); return Arrays.stream(path).map(jar -> new File(jar).getName()).sorted().collect(Collectors.toList()); @@ -144,6 +149,21 @@ private static List> path(BuildResult result, String pathArg) { } private BuildResult build(String task) { + return build(task, new Properties()); + } + + private BuildResult buildWithJava11(String task) { + Properties gradleProperties = new Properties(); + gradleProperties.setProperty("org.gradle.java.home", System.getProperty("java11Home")); + return build(task, gradleProperties); + } + + private BuildResult build(String task, Properties gradleProperties) { + try (OutputStream out = new FileOutputStream("test-project/gradle.properties")) { + gradleProperties.store(out, null); + } catch (IOException ioe) { + throw new RuntimeException(ioe); + } return GradleRunner.create() .withProjectDir(new File("test-project")) .withGradleVersion(getGradleVersion())