Skip to content
Merged
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 CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Java Module Packaging Gradle Plugin - Changelog

## Version 1.3.1
- [#147](https://github.com/gradlex-org/java-module-packaging/issues/147) - Add 'enableNativeAccess' feature to 'FatModuleJar' task
- [#146](https://github.com/gradlex-org/java-module-packaging/issues/146) - Update default 'fatModueJar' launcher to 'build.jenesis.launcher:0.6.5'

## Version 1.3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.gradle.api.file.ConfigurableFileCollection;
import org.gradle.api.internal.file.FileOperations;
import org.gradle.api.internal.file.copy.CopySpecInternal;
import org.gradle.api.provider.ListProperty;
import org.gradle.api.provider.Property;
import org.gradle.api.tasks.Classpath;
import org.gradle.api.tasks.Input;
Expand All @@ -32,6 +33,9 @@ public abstract class FatModuleJar extends Jar {
@Input
public abstract Property<String> getMainClass();

@Input
public abstract ListProperty<String> getEnableNativeAccess();

@Classpath
public abstract ConfigurableFileCollection getLauncherPath();

Expand All @@ -56,6 +60,10 @@ protected void copy() {
getManifest()
.attributes(singletonMap("Main-Class", getLauncherMainClass().get()));

if (!getEnableNativeAccess().get().isEmpty()) {
getManifest().attributes(singletonMap("Enable-Native-Access", "ALL-UNNAMED"));
}

CopySpecInternal extendedSpec = (CopySpecInternal) getFiles().copySpec();
extendedSpec.with(getRootSpec());

Expand Down Expand Up @@ -87,14 +95,22 @@ protected void copy() {

private File writeApplicationProperties() {
File applicationProperties = new File(getTemporaryDir(), "application.properties");
String enableNativeAccess;
if (getEnableNativeAccess().get().isEmpty()) {
enableNativeAccess = "";
} else {
enableNativeAccess = "enableNativeAccess="
+ String.join(",", getEnableNativeAccess().get()) + "\n";
}
try {
Files.createDirectories(applicationProperties.toPath().getParent());
Files.write(
applicationProperties.toPath(),
String.format(
"mainModule=%s\nmainClass=%s\nmodulepath=%s",
"mainModule=%s\nmainClass=%s\n%smodulepath=%s",
getMainModule().get(),
getMainClass().get(),
enableNativeAccess,
getModulePath().getFiles().stream()
.map(this::nameWithoutExtension)
.collect(Collectors.joining(",")))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ void offers_one_fat_jar_task_for_the_default_target() {
build.build(":app:fatModuleJar");
assertThat(build.projectDir.file("app/build/libs/app-1.0-all.jar").getAsPath())
.isRegularFile();
assertThat(build.projectDir
.file("app/build/tmp/fatModuleJar/application.properties")
.getAsPath())
.hasContent("""
mainModule=org.example.app
mainClass=org.example.app.Main
modulepath=app-1.0,commons-csv-1.14.1,commons-io-2.20.0,commons-codec-1.19.0
""");
}

@Test
Expand Down Expand Up @@ -159,16 +167,36 @@ void fat_jar_runs() throws IOException, InterruptedException {
.toAbsolutePath()
.toString();

System.out.println(javaCmd + " -jar " + fatJar);
String[] command = {javaCmd, "-jar", fatJar};
Process process = new ProcessBuilder(command).redirectErrorStream(true).start();

var reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
assertThat(line).isEqualTo("org.example.app / true");
}
assertThat(process.waitFor()).isEqualTo(0);
}

@Test
void can_enable_native_access() throws IOException, InterruptedException {
build.appBuildFile.appendText("""
version = "1.0"
tasks.fatModuleJar {
enableNativeAccess.set(listOf("org.example.app"))
}
""");

build.build(":app:fatModuleJar");

assertThat(build.projectDir
.file("app/build/tmp/fatModuleJar/application.properties")
.getAsPath())
.hasContent("""
mainModule=org.example.app
mainClass=org.example.app.Main
enableNativeAccess=org.example.app
modulepath=app-1.0,commons-csv-1.14.1,commons-io-2.20.0,commons-codec-1.19.0
""");
}
}
Loading