Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make plugin compatible with Java 8 #162

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
build/
*.iml
out/
/test-project/gradle.properties
11 changes: 10 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ group = 'org.openjfx'
version = '0.1.1-SNAPSHOT'

java {
toolchain.languageVersion = JavaLanguageVersion.of(11)
toolchain.languageVersion = JavaLanguageVersion.of(8)
}

repositories {
Expand All @@ -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 {
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/org/openjfx/gradle/JavaFXModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -55,7 +56,7 @@ public enum JavaFXModule {
private final List<JavaFXModule> dependentModules;

JavaFXModule(JavaFXModule...dependentModules) {
this.dependentModules = List.of(dependentModules);
this.dependentModules = Arrays.asList(dependentModules);
}

public static Optional<JavaFXModule> fromModuleName(String moduleName) {
Expand Down Expand Up @@ -86,13 +87,14 @@ public static Set<JavaFXModule> getJavaFXModules(List<String> moduleNames) {

return moduleNames.stream()
.map(JavaFXModule::fromModuleName)
.flatMap(Optional::stream)
.filter(Optional::isPresent)
.map(Optional::get)
.collect(Collectors.toSet());
}

public static void validateModules(List<String> moduleNames) {
var invalidModules = moduleNames.stream()
.filter(module -> JavaFXModule.fromModuleName(module).isEmpty())
List<String> invalidModules = moduleNames.stream()
.filter(module -> !JavaFXModule.fromModuleName(module).isPresent())
.collect(Collectors.toList());

if (! invalidModules.isEmpty()) {
Expand Down
17 changes: 10 additions & 7 deletions src/main/java/org/openjfx/gradle/JavaFXOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -177,20 +178,20 @@ public void setModules(List<String> modules) {
}

public void modules(String...moduleNames) {
setModules(List.of(moduleNames));
setModules(Arrays.asList(moduleNames));
}

private void declareFXDependencies(String conf) {
// Use 'withDependencies' to declare the dependencies late (i.e., right before dependency resolution starts).
// 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<JavaFXModule> javaFXModules = JavaFXModule.getJavaFXModules(this.modules);
if (customSDKArtifactRepository == null) {
javaFXModules.stream()
.sorted()
Expand All @@ -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<JavaFXModule> 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<String, String> dependencyNotation = new HashMap<>();
dependencyNotation.put("name", javaFXModule.getModuleName());
dependencySet.add(getDependencies().create(dependencyNotation));
});
}
});
}
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/org/openjfx/gradle/JavaFXPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@

import java.io.File;
import java.util.Arrays;
import java.util.List;

import static org.openjfx.gradle.JavaFXOptions.MAVEN_JAVAFX_ARTIFACT_GROUP_ID;

Expand Down Expand Up @@ -111,9 +110,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())
));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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.
Expand Down
8 changes: 5 additions & 3 deletions src/test/java/org/openjfx/gradle/JavaFXModuleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -50,7 +52,7 @@ void existingModuleName() {

@Test
void nonExistingModuleName() {
assertTrue(JavaFXModule.fromModuleName("javafx.unknown").isEmpty());
assertFalse(JavaFXModule.fromModuleName("javafx.unknown").isPresent());
}

@Test
Expand All @@ -65,14 +67,14 @@ void getArtifactName() {

@Test
void validateWithValidModules() {
var moduleNames = List.of(JavaFXModule.CONTROLS.getModuleName(), JavaFXModule.WEB.getModuleName());
List<String> 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<String> moduleNames = Arrays.asList("javafx.unknown", JavaFXModule.CONTROLS.getModuleName(), JavaFXModule.WEB.getModuleName());

try {
JavaFXModule.validateModules(moduleNames);
Expand Down
70 changes: 45 additions & 25 deletions src/test/java/org/openjfx/gradle/JavaFXPluginSmokeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,21 @@
*/
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;
import org.gradle.testkit.runner.TaskOutcome;
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;
Expand All @@ -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();
}
Expand All @@ -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<List<String>> modulePath(BuildResult result) {
Expand All @@ -132,18 +137,33 @@ private static List<List<String>> runtimeClassPath(BuildResult result) {
private static List<List<String>> 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.<String>of();
if (pathString.trim().isEmpty()) {
return Arrays.<String>asList();
}
String[] path = pathString.split(System.getProperty("path.separator"));
return Arrays.stream(path).map(jar -> new File(jar).getName()).sorted().collect(Collectors.toList());
}).filter(p -> !p.isEmpty()).collect(Collectors.toList());
}

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())
Expand Down