Skip to content

Commit

Permalink
use Gradle's built-in Java Module System support (#181)
Browse files Browse the repository at this point in the history
  • Loading branch information
abhinayagarwal committed Sep 20, 2023
1 parent 68163be commit 1779079
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 85 deletions.
47 changes: 17 additions & 30 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
plugins {
id 'java-gradle-plugin'
id 'com.github.hierynomus.license' version '0.15.0'
id 'com.gradle.plugin-publish' version '1.0.0-rc-2'
id 'com.github.ben-manes.versions' version '0.27.0'
id 'maven-publish'
id 'com.gradle.plugin-publish' version '1.2.1'
id 'com.github.ben-manes.versions' version '0.47.0'
id 'com.github.hierynomus.license' version '0.16.1'
}

group 'com.gluonhq'
version '1.0.21-SNAPSHOT'

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

repositories {
mavenCentral()
maven {
url "https://plugins.gradle.org/m2/"
}
gradlePluginPortal()
maven {
url "https://oss.sonatype.org/content/repositories/snapshots"
}
Expand All @@ -27,7 +24,7 @@ dependencies {

testImplementation gradleTestKit()
implementation 'com.gluonhq:substrate:0.0.61-SNAPSHOT'
implementation 'org.openjfx:javafx-plugin:0.0.10'
implementation 'org.openjfx:javafx-plugin:0.1.0'
}

gradlePlugin {
Expand All @@ -37,32 +34,22 @@ gradlePlugin {
displayName = 'GluonFX Plugin'
description = 'GluonFX plugin allows to run JavaFX application on the JVM or to create their native images.'
implementationClass = 'com.gluonhq.gradle.GluonFXPlugin'
website = 'https://github.com/gluonhq/gluonfx-gradle-plugin'
vcsUrl = 'https://github.com/gluonhq/gluonfx-gradle-plugin'
tags.set([ 'java', 'javafx', 'gluon', 'client', 'substrate', 'graalvm', 'aot' ])
}
}
}

pluginBundle {
website = 'https://github.com/gluonhq/gluonfx-gradle-plugin'
vcsUrl = 'https://github.com/gluonhq/gluonfx-gradle-plugin'
tags = [ 'java', 'javafx', 'gluon', 'client', 'substrate', 'graalvm', 'aot' ]
}

publishing {
publications {
maven(MavenPublication) {
groupId = project.group
artifactId = 'gluonfx-gradle-plugin'
version = project.version
}
}
repositories {
maven {
if (project.hasProperty('sonatypeUsername') && project.hasProperty('sonatypePassword')) {
credentials {
username project.property('sonatypeUsername')
password project.property('sonatypePassword')
}
url = 'https://oss.sonatype.org/content/repositories/snapshots/'
url = "https://oss.sonatype.org/content/repositories/snapshots/"
def sonatypeUsername = providers.gradleProperty('sonatypeUsername')
def sonatypePassword = providers.gradleProperty('sonatypePassword')
if (sonatypeUsername.isPresent() && sonatypePassword.isPresent()) {
username = sonatypeUsername.get()
password = sonatypePassword.get()
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionSha256Sum=038794feef1f4745c6347107b6726279d1c824f3fc634b60f86ace1e9fbd1768
distributionUrl=https\://services.gradle.org/distributions/gradle-6.3-bin.zip
distributionSha256Sum=591855b517fc635b9e04de1d05d5e76ada3f89f5fc76f87978d1b245b4f69225
distributionUrl=https\://services.gradle.org/distributions/gradle-8.3-bin.zip
networkTimeout=10000
validateDistributionUrl=true
17 changes: 17 additions & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
@@ -1 +1,18 @@
pluginManagement {
repositories {
gradlePluginPortal()
}
}

plugins {
id 'com.gradle.enterprise' version '3.14.1'
}

gradleEnterprise {
buildScan {
termsOfServiceUrl = 'https://gradle.com/terms-of-service'
termsOfServiceAgree = 'yes'
}
}

rootProject.name = 'gluonfx-gradle-plugin'
15 changes: 12 additions & 3 deletions src/main/java/com/gluonhq/gradle/attach/AttachConfiguration.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* BSD 3-Clause License
*
* Copyright (c) 2018, 2021, Gluon Software
* Copyright (c) 2018, 2023, Gluon Software
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -44,6 +44,7 @@

import com.gluonhq.gradle.ClientExtension;
import com.gluonhq.substrate.Constants;
import org.gradle.api.artifacts.ModuleDependency;

public class AttachConfiguration {
private static final String DEPENDENCY_GROUP = "com.gluonhq.attach";
Expand Down Expand Up @@ -126,7 +127,12 @@ private void applyConfiguration() {
if (services != null && !services.isEmpty()) {
services.stream()
.map(asd -> generateDependencyNotation(asd, target))
.forEach(depNotion -> project.getDependencies().add(configName, depNotion));
.forEach(depNotion -> {
ModuleDependency dep = (ModuleDependency) project.getDependencies().add(configName, depNotion);
if (dep != null) {
dep.exclude(Map.of("group", "org.openjfx", "module", "*"));
}
});

// Also add util artifact if any other artifact added
Map<String, String> utilDependencyNotationMap = new HashMap<>();
Expand All @@ -139,7 +145,10 @@ private void applyConfiguration() {
Constants.PROFILE_IOS : target;
utilDependencyNotationMap.put("classifier", utilTarget);
}
project.getDependencies().add(configName, utilDependencyNotationMap);
ModuleDependency dep = (ModuleDependency) project.getDependencies().add(configName, utilDependencyNotationMap);
if (dep != null) {
dep.exclude(Map.of("group", "org.openjfx", "module", "*"));
}
}

lastAppliedConfiguration = configuration;
Expand Down
53 changes: 3 additions & 50 deletions src/main/java/com/gluonhq/gradle/tasks/NativeRunAgentTask.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, 2022, Gluon
* Copyright (c) 2021, 2023, Gluon
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -33,14 +33,9 @@
import org.gradle.api.GradleException;
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.file.FileCollection;
import org.gradle.api.plugins.ApplicationPlugin;
import org.gradle.api.tasks.JavaExec;
import org.gradle.api.tasks.TaskAction;
import org.gradle.util.GradleVersion;
import org.openjfx.gradle.JavaFXModule;
import org.openjfx.gradle.JavaFXOptions;
import org.openjfx.gradle.JavaFXPlatform;

import javax.inject.Inject;
import java.io.BufferedWriter;
Expand All @@ -53,12 +48,9 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.TreeSet;

public class NativeRunAgentTask extends NativeBaseTask {

private static final String CONFIG_JAVAFX_RUN_TASK = "configJavafxRun";

private static final String AGENTLIB_NATIVE_IMAGE_AGENT_STRING =
"-agentlib:native-image-agent=access-filter-file=src/main/resources/META-INF/native-image/filter-file.json,config-merge-dir=src/main/resources/META-INF/native-image";

Expand All @@ -75,12 +67,6 @@ public class NativeRunAgentTask extends NativeBaseTask {
public NativeRunAgentTask(Project project) {
super(project);
clientExtension = project.getExtensions().getByType(ClientExtension.class);

Task javafxRun = project.getTasks().findByName(CONFIG_JAVAFX_RUN_TASK);
if (javafxRun == null) {
throw new GradleException("javafxplugin:" + CONFIG_JAVAFX_RUN_TASK + " task not found.");
}
this.dependsOn(javafxRun.getPath());
}

@TaskAction
Expand Down Expand Up @@ -118,38 +104,12 @@ public void action() {
throw new GradleException("Run task not found.");
}

JavaFXOptions javaFXOptions = project.getExtensions().getByType(JavaFXOptions.class);
var definedJavaFXModuleNames = new TreeSet<>(javaFXOptions.getModules());
if (definedJavaFXModuleNames.isEmpty()) {
throw new GradleException("No JavaFX modules found.");
}
final FileCollection classpathWithoutJavaFXJars = execTask.getClasspath().filter(
jar -> Arrays.stream(JavaFXModule.values()).noneMatch(javaFXModule ->
jar.getName().contains(javaFXModule.getArtifactName()))
);
final FileCollection javaFXPlatformJars = execTask.getClasspath().filter(jar ->
isJavaFXJar(jar, javaFXOptions.getPlatform()));

// Remove all JavaFX jars from classpath
execTask.setClasspath(classpathWithoutJavaFXJars);

// Define JVM args for command line
var javaFXModuleJvmArgs = List.of("--module-path", javaFXPlatformJars.getAsPath());
var jvmArgs = new ArrayList<>(javaFXModuleJvmArgs);
jvmArgs.add("--add-modules");
jvmArgs.add(String.join(",", definedJavaFXModuleNames));
if (GradleVersion.current().compareTo(GradleVersion.version("6.6")) < 0) {
// Include classpath as JVM arg for Gradle versions lower than 6.6
jvmArgs.add("-cp");
jvmArgs.add(classpathWithoutJavaFXJars.getAsPath());
}

// set java_home
execTask.executable(Path.of(graalVMHome.toString(), "bin", "java").toString());

// set jvmargs
execTask.getJvmArgs().add(AGENTLIB_NATIVE_IMAGE_AGENT_STRING);
execTask.getJvmArgs().addAll(jvmArgs);
var jvmArgs = List.of(AGENTLIB_NATIVE_IMAGE_AGENT_STRING);
execTask.getJvmArgumentProviders().add(() -> jvmArgs);

// run
execTask.exec();
Expand Down Expand Up @@ -191,11 +151,4 @@ private void createFilterFile(String agentFilter) throws IOException {
bw.write("}\n");
}
}

private static boolean isJavaFXJar(File jar, JavaFXPlatform platform) {
return jar.isFile() &&
Arrays.stream(JavaFXModule.values()).anyMatch(javaFXModule ->
javaFXModule.compareJarFileName(platform, jar.getName()) ||
javaFXModule.getModuleJarFileName().equals(jar.getName()));
}
}

0 comments on commit 1779079

Please sign in to comment.