create jmods of runtime dependencies and add more jlink options
This commit is contained in:
parent
e91f776db2
commit
942e765c78
6 changed files with 224 additions and 82 deletions
|
@ -8,7 +8,11 @@ import java.util.List;
|
||||||
|
|
||||||
public class JLinkExtension {
|
public class JLinkExtension {
|
||||||
|
|
||||||
private final ListProperty<String> modules;
|
private final ListProperty<String> addModules;
|
||||||
|
|
||||||
|
private final Property<Boolean> bindServices;
|
||||||
|
|
||||||
|
private final Property<String> launcher;
|
||||||
|
|
||||||
private final Property<Integer> compress;
|
private final Property<Integer> compress;
|
||||||
|
|
||||||
|
@ -27,7 +31,9 @@ public class JLinkExtension {
|
||||||
}
|
}
|
||||||
|
|
||||||
public JLinkExtension(Project project) {
|
public JLinkExtension(Project project) {
|
||||||
this.modules = project.getObjects().listProperty(String.class);
|
this.addModules = project.getObjects().listProperty(String.class);
|
||||||
|
this.bindServices = project.getObjects().property(Boolean.class);
|
||||||
|
this.launcher = project.getObjects().property(String.class);
|
||||||
this.compress = project.getObjects().property(Integer.class);
|
this.compress = project.getObjects().property(Integer.class);
|
||||||
this.stripDebug = project.getObjects().property(Boolean.class);
|
this.stripDebug = project.getObjects().property(Boolean.class);
|
||||||
this.noHeaderFiles = project.getObjects().property(Boolean.class);
|
this.noHeaderFiles = project.getObjects().property(Boolean.class);
|
||||||
|
@ -35,12 +41,28 @@ public class JLinkExtension {
|
||||||
this.endian = project.getObjects().property(Endian.class);
|
this.endian = project.getObjects().property(Endian.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setModules(List<String> modules) {
|
public void setAddModules(List<String> addModules) {
|
||||||
this.modules.set(modules);
|
this.addModules.set(addModules);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ListProperty<String> getModules() {
|
public ListProperty<String> getAddModules() {
|
||||||
return modules;
|
return addModules;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBindServices(Boolean bindServices) {
|
||||||
|
this.bindServices.set(bindServices);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Property<Boolean> getBindServices() {
|
||||||
|
return bindServices;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLauncher(String launcher) {
|
||||||
|
this.launcher.set(launcher);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Property<String> getLauncher() {
|
||||||
|
return launcher;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setCompress(Integer compress) {
|
public void setCompress(Integer compress) {
|
||||||
|
|
|
@ -3,13 +3,17 @@ package org.xbib.gradle.plugin.jlink;
|
||||||
import org.gradle.api.Plugin;
|
import org.gradle.api.Plugin;
|
||||||
import org.gradle.api.Project;
|
import org.gradle.api.Project;
|
||||||
import org.gradle.api.artifacts.Configuration;
|
import org.gradle.api.artifacts.Configuration;
|
||||||
|
import org.gradle.api.artifacts.ResolvedDependency;
|
||||||
import org.gradle.api.attributes.Usage;
|
import org.gradle.api.attributes.Usage;
|
||||||
import org.gradle.api.plugins.JavaPlugin;
|
import org.gradle.api.plugins.JavaPlugin;
|
||||||
import org.gradle.api.tasks.TaskProvider;
|
import org.gradle.api.tasks.TaskProvider;
|
||||||
import org.gradle.jvm.tasks.Jar;
|
import org.gradle.jvm.tasks.Jar;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
import java.util.function.Function;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
public abstract class JLinkPlugin implements Plugin<Project> {
|
public abstract class JLinkPlugin implements Plugin<Project> {
|
||||||
|
|
||||||
|
@ -17,7 +21,9 @@ public abstract class JLinkPlugin implements Plugin<Project> {
|
||||||
public void apply(Project project) {
|
public void apply(Project project) {
|
||||||
project.getPluginManager().apply(JavaPlugin.class);
|
project.getPluginManager().apply(JavaPlugin.class);
|
||||||
JLinkExtension extension = project.getExtensions().create("jlink", JLinkExtension.class);
|
JLinkExtension extension = project.getExtensions().create("jlink", JLinkExtension.class);
|
||||||
extension.getModules().convention(List.of("java.base"));
|
extension.getAddModules().convention(List.of("java.base"));
|
||||||
|
extension.getBindServices().convention(false);
|
||||||
|
extension.getLauncher().unsetConvention();
|
||||||
extension.getCompress().convention(6); // zip-6, default
|
extension.getCompress().convention(6); // zip-6, default
|
||||||
extension.getStripDebug().convention(true);
|
extension.getStripDebug().convention(true);
|
||||||
extension.getNoHeaderFiles().convention(true);
|
extension.getNoHeaderFiles().convention(true);
|
||||||
|
@ -33,7 +39,9 @@ public abstract class JLinkPlugin implements Plugin<Project> {
|
||||||
TaskProvider<JLinkTask> jLinkTask = project.getTasks().register("jlink", JLinkTask.class);
|
TaskProvider<JLinkTask> jLinkTask = project.getTasks().register("jlink", JLinkTask.class);
|
||||||
project.getTasks().withType(JLinkTask.class).forEach(it -> {
|
project.getTasks().withType(JLinkTask.class).forEach(it -> {
|
||||||
it.dependsOn(jModTask);
|
it.dependsOn(jModTask);
|
||||||
it.modules.set(extension.getModules());
|
it.addModules.set(extension.getAddModules());
|
||||||
|
it.bindServices.set(extension.getBindServices());
|
||||||
|
it.launcher.set(extension.getLauncher());
|
||||||
it.compress.set(extension.getCompress());
|
it.compress.set(extension.getCompress());
|
||||||
it.stripDebug.set(extension.getStripDebug());
|
it.stripDebug.set(extension.getStripDebug());
|
||||||
it.noHeaderFiles.set(extension.getNoHeaderFiles());
|
it.noHeaderFiles.set(extension.getNoHeaderFiles());
|
||||||
|
|
|
@ -35,9 +35,26 @@ public class JLinkTask extends AbstractExecTask<JLinkTask> {
|
||||||
@Nested
|
@Nested
|
||||||
Property<JavaCompiler> javaCompiler;
|
Property<JavaCompiler> javaCompiler;
|
||||||
|
|
||||||
|
@Input
|
||||||
|
ListProperty<String> addModules;
|
||||||
|
|
||||||
@Input
|
@Input
|
||||||
@Optional
|
@Optional
|
||||||
ListProperty<String> modules;
|
ListProperty<String> limitModules;
|
||||||
|
|
||||||
|
@Input
|
||||||
|
@Optional
|
||||||
|
Property<Boolean> bindServices;
|
||||||
|
|
||||||
|
@InputDirectory
|
||||||
|
DirectoryProperty modulePath;
|
||||||
|
|
||||||
|
@OutputDirectory
|
||||||
|
DirectoryProperty outputDirectory;
|
||||||
|
|
||||||
|
@Input
|
||||||
|
@Optional
|
||||||
|
Property<String> launcher;
|
||||||
|
|
||||||
@Input
|
@Input
|
||||||
@Optional
|
@Optional
|
||||||
|
@ -59,31 +76,36 @@ public class JLinkTask extends AbstractExecTask<JLinkTask> {
|
||||||
@Optional
|
@Optional
|
||||||
Property<JLinkExtension.Endian> endian;
|
Property<JLinkExtension.Endian> endian;
|
||||||
|
|
||||||
@InputDirectory
|
@Input
|
||||||
DirectoryProperty modulePath;
|
@Optional
|
||||||
|
ListProperty<String> includeLocales;
|
||||||
@OutputDirectory
|
|
||||||
DirectoryProperty outputDirectory;
|
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public JLinkTask() {
|
public JLinkTask() {
|
||||||
super(JLinkTask.class);
|
super(JLinkTask.class);
|
||||||
this.javaCompiler = getProject().getObjects().property(JavaCompiler.class);
|
javaCompiler = getProject().getObjects().property(JavaCompiler.class);
|
||||||
this.modules = getProject().getObjects().listProperty(String.class);
|
addModules = getProject().getObjects().listProperty(String.class);
|
||||||
this.compress = getProject().getObjects().property(Integer.class);
|
limitModules = getProject().getObjects().listProperty(String.class);
|
||||||
this.stripDebug = getProject().getObjects().property(Boolean.class);
|
bindServices = getProject().getObjects().property(Boolean.class);
|
||||||
this.noHeaderFiles = getProject().getObjects().property(Boolean.class);
|
modulePath = getProject().getObjects().directoryProperty();
|
||||||
this.noManPages = getProject().getObjects().property(Boolean.class);
|
outputDirectory = getProject().getObjects().directoryProperty();
|
||||||
this.endian = getProject().getObjects().property(JLinkExtension.Endian.class);
|
launcher = getProject().getObjects().property(String.class);
|
||||||
this.modulePath = getProject().getObjects().directoryProperty();
|
compress = getProject().getObjects().property(Integer.class);
|
||||||
this.outputDirectory = getProject().getObjects().directoryProperty();
|
stripDebug = getProject().getObjects().property(Boolean.class);
|
||||||
|
noHeaderFiles = getProject().getObjects().property(Boolean.class);
|
||||||
|
noManPages = getProject().getObjects().property(Boolean.class);
|
||||||
|
endian = getProject().getObjects().property(JLinkExtension.Endian.class);
|
||||||
|
includeLocales = getProject().getObjects().listProperty(String.class);
|
||||||
JavaToolchainSpec toolchain = getProject().getExtensions().getByType(JavaPluginExtension.class).getToolchain();
|
JavaToolchainSpec toolchain = getProject().getExtensions().getByType(JavaPluginExtension.class).getToolchain();
|
||||||
JavaToolchainService service = getProject().getExtensions().getByType(JavaToolchainService.class);
|
JavaToolchainService service = getProject().getExtensions().getByType(JavaToolchainService.class);
|
||||||
javaCompiler.convention(service.compilerFor(toolchain));
|
javaCompiler.convention(service.compilerFor(toolchain));
|
||||||
modules.convention(List.of("java.base"));
|
addModules.convention(List.of("java.base"));
|
||||||
|
limitModules.unsetConvention();
|
||||||
|
bindServices.convention(false);
|
||||||
Provider<Directory> modulePathProvider = javaCompiler.map(it -> {
|
Provider<Directory> modulePathProvider = javaCompiler.map(it -> {
|
||||||
Directory jmods = it.getMetadata().getInstallationPath().dir("jmods");
|
Directory jmods = it.getMetadata().getInstallationPath().dir("jmods");
|
||||||
if (jmods.getAsFile().exists()) {
|
if (jmods.getAsFile().exists()) {
|
||||||
|
getLogger().log(LogLevel.INFO, "using jmods directory found in installation for module path");
|
||||||
return jmods;
|
return jmods;
|
||||||
} else {
|
} else {
|
||||||
getLogger().log(LogLevel.WARN, "directory " + jmods + " does not exist! Is jmods package installed?");
|
getLogger().log(LogLevel.WARN, "directory " + jmods + " does not exist! Is jmods package installed?");
|
||||||
|
@ -92,30 +114,42 @@ public class JLinkTask extends AbstractExecTask<JLinkTask> {
|
||||||
});
|
});
|
||||||
modulePath.convention(modulePathProvider);
|
modulePath.convention(modulePathProvider);
|
||||||
outputDirectory.convention(getProject().getLayout().getBuildDirectory().dir("jlink"));
|
outputDirectory.convention(getProject().getLayout().getBuildDirectory().dir("jlink"));
|
||||||
|
launcher.unsetConvention();
|
||||||
|
includeLocales.unsetConvention();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void exec() {
|
public void exec() {
|
||||||
setExecutable(javaCompiler.get().getMetadata().getInstallationPath().file("bin/jlink"));
|
setExecutable(javaCompiler.get().getMetadata().getInstallationPath().file("bin/jlink"));
|
||||||
TaskProvider<JModTask> jmodTask = getProject().getTasks().named("jmod", JModTask.class);
|
TaskProvider<JModTask> jmodTask = getProject().getTasks().named("jmod", JModTask.class);
|
||||||
File jlinkOutput = outputDirectory.dir("jlink-output").get().getAsFile();
|
File jlinkOutput = outputDirectory.get().getAsFile();
|
||||||
try {
|
try {
|
||||||
createDirectory(jlinkOutput.toPath());
|
createDirectory(jlinkOutput.toPath());
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
List<String> args = new ArrayList<>(List.of(
|
List<String> args = new ArrayList<>();
|
||||||
"--module-path",
|
args.add("--module-path");
|
||||||
modulePath.get().getAsFile().getAbsolutePath(),
|
args.add(modulePath.get().getAsFile().getAbsolutePath());
|
||||||
"--module-path",
|
args.add("--module-path");
|
||||||
jmodTask.get().jmodFile.getAsFile().get().getAbsolutePath(),
|
args.add(jmodTask.get().jmodDirectory.getAsFile().get().getAbsolutePath());
|
||||||
"--add-modules",
|
args.add("--add-modules");
|
||||||
String.join(",", modules.get()),
|
args.add(String.join(",", addModules.get()));
|
||||||
"--compress",
|
if (limitModules.isPresent()) {
|
||||||
"zip-" + compress.get(),
|
args.add("--limit-modules");
|
||||||
"--output",
|
args.add(String.join(",", limitModules.get()));
|
||||||
jlinkOutput.getAbsolutePath()
|
}
|
||||||
));
|
args.add("--compress");
|
||||||
|
args.add("zip-" + compress.get());
|
||||||
|
args.add("--output");
|
||||||
|
args.add(jlinkOutput.getAbsolutePath());
|
||||||
|
if (bindServices.get()) {
|
||||||
|
args.add("--bind-services");
|
||||||
|
}
|
||||||
|
if (launcher.isPresent()) {
|
||||||
|
args.add("--launcher");
|
||||||
|
args.add(launcher.get());
|
||||||
|
}
|
||||||
if (stripDebug.get()) {
|
if (stripDebug.get()) {
|
||||||
args.add("--strip-debug");
|
args.add("--strip-debug");
|
||||||
}
|
}
|
||||||
|
@ -129,8 +163,13 @@ public class JLinkTask extends AbstractExecTask<JLinkTask> {
|
||||||
args.add("--endian");
|
args.add("--endian");
|
||||||
args.add(endian.get().toString().toLowerCase(Locale.ROOT));
|
args.add(endian.get().toString().toLowerCase(Locale.ROOT));
|
||||||
}
|
}
|
||||||
|
if (includeLocales.isPresent()) {
|
||||||
|
args.add("--include-locales");
|
||||||
|
args.add(String.join(",", includeLocales.get()));
|
||||||
|
}
|
||||||
setArgs(args);
|
setArgs(args);
|
||||||
System.err.println( "executing " + getExecutable() + " with " + args);
|
System.err.println("executing " + getExecutable() + " with " + args);
|
||||||
|
getLogger().log(LogLevel.INFO, "executing " + getExecutable() + " with " + args);
|
||||||
super.exec();
|
super.exec();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -142,12 +181,52 @@ public class JLinkTask extends AbstractExecTask<JLinkTask> {
|
||||||
return javaCompiler;
|
return javaCompiler;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setModules(List<String> modules) {
|
public void setModulePath(Directory modulePath) {
|
||||||
this.modules.set(modules);
|
this.modulePath.set(modulePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ListProperty<String> getModules() {
|
public DirectoryProperty getModulePath() {
|
||||||
return modules;
|
return modulePath;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setOutputDirectory(Directory outputDirectory) {
|
||||||
|
this.outputDirectory.set(outputDirectory);
|
||||||
|
}
|
||||||
|
|
||||||
|
public DirectoryProperty getOutputDirectory() {
|
||||||
|
return outputDirectory;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAddModules(List<String> addModules) {
|
||||||
|
this.addModules.set(addModules);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ListProperty<String> getAddModules() {
|
||||||
|
return addModules;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLimitModules(List<String> limitModules) {
|
||||||
|
this.limitModules.set(limitModules);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ListProperty<String> getLimitModules() {
|
||||||
|
return limitModules;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBindServices(Boolean bindServices) {
|
||||||
|
this.bindServices.set(bindServices);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Property<Boolean> getBindServices() {
|
||||||
|
return bindServices;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLauncher(String launcher) {
|
||||||
|
this.launcher.set(launcher);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Property<String> getLauncher() {
|
||||||
|
return launcher;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setCompress(Integer compress) {
|
public void setCompress(Integer compress) {
|
||||||
|
@ -190,20 +269,12 @@ public class JLinkTask extends AbstractExecTask<JLinkTask> {
|
||||||
return endian;
|
return endian;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setModulePath(Directory modulePath) {
|
public void setIncludeLocales(List<String> includeLocales) {
|
||||||
this.modulePath.set(modulePath);
|
this.includeLocales.set(includeLocales);
|
||||||
}
|
}
|
||||||
|
|
||||||
public DirectoryProperty getModulePath() {
|
public ListProperty<String> getIncludeLocales() {
|
||||||
return modulePath;
|
return includeLocales;
|
||||||
}
|
|
||||||
|
|
||||||
public void setOutputDirectory(Directory outputDirectory) {
|
|
||||||
this.outputDirectory.set(outputDirectory);
|
|
||||||
}
|
|
||||||
|
|
||||||
public DirectoryProperty getOutputDirectory() {
|
|
||||||
return outputDirectory;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void createDirectory(Path path) throws IOException {
|
private static void createDirectory(Path path) throws IOException {
|
||||||
|
@ -221,9 +292,9 @@ public class JLinkTask extends AbstractExecTask<JLinkTask> {
|
||||||
return FileVisitResult.CONTINUE;
|
return FileVisitResult.CONTINUE;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
Files.delete(path);
|
Files.deleteIfExists(path);
|
||||||
}
|
}
|
||||||
// create only parent, jlink aborts if output directory exists
|
// jlink bails out if directory is created, so we create the parent
|
||||||
Files.createDirectories(path.getParent());
|
Files.createDirectories(path.getParent());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,13 +1,19 @@
|
||||||
package org.xbib.gradle.plugin.jlink;
|
package org.xbib.gradle.plugin.jlink;
|
||||||
|
|
||||||
|
import org.gradle.api.artifacts.Configuration;
|
||||||
|
import org.gradle.api.artifacts.ResolvedArtifact;
|
||||||
|
import org.gradle.api.artifacts.ResolvedDependency;
|
||||||
|
import org.gradle.api.file.Directory;
|
||||||
|
import org.gradle.api.file.DirectoryProperty;
|
||||||
import org.gradle.api.file.RegularFile;
|
import org.gradle.api.file.RegularFile;
|
||||||
import org.gradle.api.file.RegularFileProperty;
|
import org.gradle.api.file.RegularFileProperty;
|
||||||
|
import org.gradle.api.logging.LogLevel;
|
||||||
import org.gradle.api.plugins.JavaPluginExtension;
|
import org.gradle.api.plugins.JavaPluginExtension;
|
||||||
import org.gradle.api.provider.Property;
|
import org.gradle.api.provider.Property;
|
||||||
import org.gradle.api.tasks.AbstractExecTask;
|
import org.gradle.api.tasks.AbstractExecTask;
|
||||||
import org.gradle.api.tasks.InputFile;
|
import org.gradle.api.tasks.InputFile;
|
||||||
import org.gradle.api.tasks.Nested;
|
import org.gradle.api.tasks.Nested;
|
||||||
import org.gradle.api.tasks.OutputFile;
|
import org.gradle.api.tasks.OutputDirectory;
|
||||||
import org.gradle.api.tasks.TaskProvider;
|
import org.gradle.api.tasks.TaskProvider;
|
||||||
import org.gradle.jvm.tasks.Jar;
|
import org.gradle.jvm.tasks.Jar;
|
||||||
import org.gradle.jvm.toolchain.JavaCompiler;
|
import org.gradle.jvm.toolchain.JavaCompiler;
|
||||||
|
@ -21,9 +27,11 @@ import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.SimpleFileVisitor;
|
import java.nio.file.SimpleFileVisitor;
|
||||||
import java.nio.file.attribute.BasicFileAttributes;
|
import java.nio.file.attribute.BasicFileAttributes;
|
||||||
import java.util.ArrayList;
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
import java.util.function.Function;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
public class JModTask extends AbstractExecTask<JModTask> {
|
public class JModTask extends AbstractExecTask<JModTask> {
|
||||||
|
|
||||||
|
@ -33,8 +41,8 @@ public class JModTask extends AbstractExecTask<JModTask> {
|
||||||
@InputFile
|
@InputFile
|
||||||
RegularFileProperty jarFile;
|
RegularFileProperty jarFile;
|
||||||
|
|
||||||
@OutputFile
|
@OutputDirectory
|
||||||
RegularFileProperty jmodFile;
|
DirectoryProperty jmodDirectory;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public JModTask() {
|
public JModTask() {
|
||||||
|
@ -47,28 +55,42 @@ public class JModTask extends AbstractExecTask<JModTask> {
|
||||||
TaskProvider<Jar> jarTask = getProject().getTasks().named("jar", Jar.class);
|
TaskProvider<Jar> jarTask = getProject().getTasks().named("jar", Jar.class);
|
||||||
Objects.requireNonNull(jarTask);
|
Objects.requireNonNull(jarTask);
|
||||||
jarFile.convention(jarTask.get().getArchiveFile());
|
jarFile.convention(jarTask.get().getArchiveFile());
|
||||||
jmodFile = getProject().getObjects().fileProperty();
|
jmodDirectory = getProject().getObjects().directoryProperty();
|
||||||
jmodFile.convention(getProject().getLayout().getBuildDirectory()
|
jmodDirectory.convention(getProject().getLayout().getBuildDirectory().dir("jmods"));
|
||||||
.dir("jmod").get().file(getProject().getName() + ".jmod"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void exec() {
|
public void exec() {
|
||||||
setExecutable(javaCompiler.get().getMetadata().getInstallationPath().file("bin/jmod"));
|
setExecutable(javaCompiler.get().getMetadata().getInstallationPath().file("bin/jmod"));
|
||||||
try {
|
try {
|
||||||
createDirectory(jmodFile.getAsFile().get().toPath());
|
System.err.println("creating directory " + jmodDirectory.getAsFile().get().toPath());
|
||||||
|
createDirectory(jmodDirectory.getAsFile().get().toPath());
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
getLogger().log(LogLevel.ERROR, e.getMessage(), e);
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
List<String> args = new ArrayList<>(List.of(
|
List<String> args = List.of(
|
||||||
"create",
|
"create",
|
||||||
"--class-path",
|
"--class-path",
|
||||||
jarFile.get().getAsFile().getAbsolutePath(),
|
jarFile.get().getAsFile().getAbsolutePath(),
|
||||||
jmodFile.get().getAsFile().getAbsolutePath()
|
jmodDirectory.get().file(getProject().getName() + ".jmod").getAsFile().getAbsolutePath()
|
||||||
));
|
);
|
||||||
setArgs(args);
|
setArgs(args);
|
||||||
System.err.println( "executing " + getExecutable() + " with " + args);
|
System.err.println("executing " + getExecutable() + " with " + args);
|
||||||
|
getLogger().log(LogLevel.INFO, "executing " + getExecutable() + " with " + args);
|
||||||
super.exec();
|
super.exec();
|
||||||
|
for (ResolvedArtifact artifact : collectArtifacts(getProject().getConfigurations().getByName("runtimeClasspath"))) {
|
||||||
|
args = List.of(
|
||||||
|
"create",
|
||||||
|
"--class-path",
|
||||||
|
artifact.getFile().getAbsolutePath(),
|
||||||
|
jmodDirectory.get().file(artifact.getName() + ".jmod").getAsFile().getAbsolutePath()
|
||||||
|
);
|
||||||
|
setArgs(args);
|
||||||
|
System.err.println("executing " + getExecutable() + " with " + args);
|
||||||
|
getLogger().log(LogLevel.INFO, "executing " + getExecutable() + " with " + args);
|
||||||
|
super.exec();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setJavaCompiler(JavaCompiler javaCompiler) {
|
public void setJavaCompiler(JavaCompiler javaCompiler) {
|
||||||
|
@ -87,12 +109,12 @@ public class JModTask extends AbstractExecTask<JModTask> {
|
||||||
return jarFile;
|
return jarFile;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setJmodFile(RegularFile jmodFile) {
|
public void setJmodDirectory(Directory jmodDirectory) {
|
||||||
this.jmodFile.set(jmodFile);
|
this.jmodDirectory.set(jmodDirectory);
|
||||||
}
|
}
|
||||||
|
|
||||||
public RegularFileProperty getJmodFile() {
|
public DirectoryProperty getJmodDirectory() {
|
||||||
return jmodFile;
|
return jmodDirectory;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void createDirectory(Path path) throws IOException {
|
private static void createDirectory(Path path) throws IOException {
|
||||||
|
@ -110,9 +132,26 @@ public class JModTask extends AbstractExecTask<JModTask> {
|
||||||
return FileVisitResult.CONTINUE;
|
return FileVisitResult.CONTINUE;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
Files.delete(path);
|
Files.deleteIfExists(path);
|
||||||
}
|
}
|
||||||
// create only parent
|
Files.createDirectory(path);
|
||||||
Files.createDirectories(path.getParent());
|
}
|
||||||
|
|
||||||
|
private static List<ResolvedArtifact> collectArtifacts(Configuration configuration) {
|
||||||
|
return configuration.getResolvedConfiguration()
|
||||||
|
.getFirstLevelModuleDependencies()
|
||||||
|
.stream()
|
||||||
|
.flatMap(JModTask::of)
|
||||||
|
.flatMap(dependency -> dependency.getModuleArtifacts().stream())
|
||||||
|
.distinct()
|
||||||
|
.toList();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Stream<ResolvedDependency> of(ResolvedDependency node) {
|
||||||
|
return of(node, ResolvedDependency::getChildren);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static <T> Stream<T> of(T node, Function<T, Collection<T>> childrenFn) {
|
||||||
|
return Stream.concat(Stream.of(node), childrenFn.apply(node).stream().flatMap(n -> of(n, childrenFn)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,11 +32,6 @@ class JLinkGradleBuild {
|
||||||
id("org.xbib.gradle.plugin.jlink")
|
id("org.xbib.gradle.plugin.jlink")
|
||||||
}
|
}
|
||||||
group = "org.example"
|
group = "org.example"
|
||||||
java {
|
|
||||||
toolchain {
|
|
||||||
languageVersion.set(JavaLanguageVersion.of(21))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
'''
|
'''
|
||||||
tapFile("app/src/main/java/org/example/app/Main.java") << '''
|
tapFile("app/src/main/java/org/example/app/Main.java") << '''
|
||||||
package org.example.app;
|
package org.example.app;
|
||||||
|
@ -98,7 +93,10 @@ class JLinkGradleBuild {
|
||||||
}
|
}
|
||||||
|
|
||||||
GradleRunner runner(String... args) {
|
GradleRunner runner(String... args) {
|
||||||
|
def err = new PrintWriter(System.err)
|
||||||
GradleRunner.create()
|
GradleRunner.create()
|
||||||
|
.forwardStdError(err)
|
||||||
|
.forwardStdOutput(err)
|
||||||
.forwardOutput()
|
.forwardOutput()
|
||||||
.withPluginClasspath()
|
.withPluginClasspath()
|
||||||
.withProjectDir(projectDir)
|
.withProjectDir(projectDir)
|
||||||
|
|
|
@ -11,12 +11,16 @@ class JLinkTest extends Specification {
|
||||||
@Delegate
|
@Delegate
|
||||||
JLinkGradleBuild build = new JLinkGradleBuild()
|
JLinkGradleBuild build = new JLinkGradleBuild()
|
||||||
|
|
||||||
def "can use jlink plugin with success=#success"() {
|
def "can use jlink plugin"() {
|
||||||
given:
|
given:
|
||||||
def taskToRun = ":app:jlink"
|
def taskToRun = ":app:jlink"
|
||||||
appBuildFile << """
|
appBuildFile << """
|
||||||
|
dependencies {
|
||||||
|
runtimeOnly "org.junit.jupiter:junit-jupiter-engine:5.10.2"
|
||||||
|
}
|
||||||
jlink {
|
jlink {
|
||||||
modules.set(List.of("org.example.app"))
|
addModules.set(List.of("org.example.app"))
|
||||||
|
launcher.set("app=org.example.app/org.example.app.Main")
|
||||||
}
|
}
|
||||||
"""
|
"""
|
||||||
appModuleInfoFile << """
|
appModuleInfoFile << """
|
||||||
|
|
Loading…
Reference in a new issue