implement jlink plugin
This commit is contained in:
parent
b28af424ff
commit
20f5a45db9
4 changed files with 190 additions and 3 deletions
|
@ -21,10 +21,10 @@ if (project.hasProperty('gradle.publish.key')) {
|
|||
plugins {
|
||||
jlinkPlugin {
|
||||
id = 'org.xbib.gradle.plugin.jlink'
|
||||
implementationClass = 'org.xbib.gradle.plugin.jlink.JlinkPlugin'
|
||||
implementationClass = 'org.xbib.gradle.plugin.jlink.JLinkPlugin'
|
||||
version = project.version
|
||||
description = 'Gradle Jlink plugin'
|
||||
displayName = 'Gradle Jlink plugin'
|
||||
description = 'Gradle JLink plugin'
|
||||
displayName = 'Gradle JLink plugin'
|
||||
tags.set(['jlink'])
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,25 @@
|
|||
package org.xbib.gradle.plugin.jlink;
|
||||
|
||||
import org.gradle.api.provider.ListProperty;
|
||||
import org.gradle.api.provider.Property;
|
||||
|
||||
public class JLinkExtension {
|
||||
|
||||
ListProperty<String> modules;
|
||||
|
||||
Property<Integer> compress;
|
||||
|
||||
Property<Boolean> stripDebug;
|
||||
|
||||
Property<Boolean> noHeaderFiles;
|
||||
|
||||
Property<Boolean> noManPages;
|
||||
|
||||
Property<Endian> endian;
|
||||
|
||||
enum Endian {
|
||||
LITTLE,
|
||||
BIG,
|
||||
NATIVE,
|
||||
}
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
package org.xbib.gradle.plugin.jlink;
|
||||
|
||||
import org.gradle.api.Plugin;
|
||||
import org.gradle.api.Project;
|
||||
import org.gradle.api.artifacts.Configuration;
|
||||
import org.gradle.api.attributes.Usage;
|
||||
import org.gradle.api.plugins.JavaPlugin;
|
||||
import org.gradle.api.tasks.TaskProvider;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public abstract class JLinkPlugin implements Plugin<Project> {
|
||||
|
||||
@Override
|
||||
public void apply(Project project) {
|
||||
project.getPluginManager().apply(JavaPlugin.class);
|
||||
JLinkExtension extension = project.getExtensions().create("jlink", JLinkExtension.class);
|
||||
extension.modules.convention(List.of("java.base"));
|
||||
extension.compress.convention(2);
|
||||
extension.stripDebug.convention(true);
|
||||
extension.noHeaderFiles.convention(true);
|
||||
extension.noManPages.convention(true);
|
||||
extension.endian.convention(JLinkExtension.Endian.NATIVE);
|
||||
TaskProvider<JLinkTask> jlinkTask = project.getTasks().register("jlink", JLinkTask.class);
|
||||
project.getTasks().withType(JLinkTask.class).forEach(it -> {
|
||||
it.modules.set(extension.modules);
|
||||
it.compress.set(extension.compress);
|
||||
it.stripDebug.set(extension.stripDebug);
|
||||
it.noHeaderFiles.set(extension.noHeaderFiles);
|
||||
it.noManPages.set(extension.noManPages);
|
||||
it.endian.set(extension.endian);
|
||||
});
|
||||
Configuration configuration = project.getConfigurations().create("jlink");
|
||||
configuration.setCanBeConsumed(true);
|
||||
configuration.setCanBeResolved(false);
|
||||
configuration.setVisible(false);
|
||||
configuration.attributes(a -> {
|
||||
a.attribute(Usage.USAGE_ATTRIBUTE, project.getObjects().named(Usage.class, "jlink"));
|
||||
});
|
||||
configuration.outgoing(o -> {
|
||||
o.artifact(jlinkTask.flatMap(t -> t.outputDirectory));
|
||||
});
|
||||
}
|
||||
}
|
|
@ -0,0 +1,118 @@
|
|||
package org.xbib.gradle.plugin.jlink;
|
||||
|
||||
import org.gradle.api.file.DirectoryProperty;
|
||||
import org.gradle.api.plugins.JavaPluginExtension;
|
||||
import org.gradle.api.provider.ListProperty;
|
||||
import org.gradle.api.provider.Property;
|
||||
import org.gradle.api.provider.Provider;
|
||||
import org.gradle.api.tasks.AbstractExecTask;
|
||||
import org.gradle.api.tasks.Input;
|
||||
import org.gradle.api.tasks.InputDirectory;
|
||||
import org.gradle.api.tasks.Nested;
|
||||
import org.gradle.api.tasks.OutputDirectory;
|
||||
import org.gradle.jvm.toolchain.JavaCompiler;
|
||||
import org.gradle.jvm.toolchain.JavaToolchainService;
|
||||
import org.gradle.jvm.toolchain.JavaToolchainSpec;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.FileVisitResult;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.SimpleFileVisitor;
|
||||
import java.nio.file.attribute.BasicFileAttributes;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
public class JLinkTask extends AbstractExecTask<JLinkTask> {
|
||||
@Nested
|
||||
Property<JavaCompiler> javaCompiler;
|
||||
|
||||
@Input
|
||||
ListProperty<String> modules;
|
||||
|
||||
@Input
|
||||
Property<Integer> compress;
|
||||
|
||||
@Input
|
||||
Property<Boolean> stripDebug;
|
||||
|
||||
@Input
|
||||
Property<Boolean> noHeaderFiles;
|
||||
|
||||
@Input
|
||||
Property<Boolean> noManPages;
|
||||
|
||||
@Input
|
||||
Property<JLinkExtension.Endian> endian;
|
||||
|
||||
@InputDirectory
|
||||
DirectoryProperty modulePath;
|
||||
|
||||
@OutputDirectory
|
||||
DirectoryProperty outputDirectory;
|
||||
|
||||
public JLinkTask(Class<JLinkTask> taskType) {
|
||||
super(taskType);
|
||||
JavaToolchainSpec toolchain = getProject().getExtensions().getByType(JavaPluginExtension.class).getToolchain();
|
||||
JavaToolchainService service = getProject().getExtensions().getByType(JavaToolchainService.class);
|
||||
Provider<JavaCompiler> defaultJlinkTool = service.compilerFor(toolchain);
|
||||
javaCompiler.convention(defaultJlinkTool);
|
||||
modules.convention(List.of("java.base"));
|
||||
modulePath.convention(javaCompiler.map(it -> it.getMetadata().getInstallationPath().dir("jmods")));
|
||||
outputDirectory.convention(getProject().getLayout().getBuildDirectory().dir("jlink-jre"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exec() {
|
||||
setExecutable(javaCompiler.get().getMetadata().getInstallationPath().file("bin/jlink"));
|
||||
File jlinkOutput = outputDirectory.dir("jre").get().getAsFile();
|
||||
try {
|
||||
delete(jlinkOutput.toPath());
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
List<String> args = new ArrayList<>(List.of(
|
||||
"--module-path",
|
||||
modulePath.get().getAsFile().getAbsolutePath(),
|
||||
"--add-modules",
|
||||
String.join(",", modules.get()),
|
||||
"--compress",
|
||||
"${compress.get()}",
|
||||
"--output",
|
||||
jlinkOutput.getAbsolutePath()
|
||||
));
|
||||
if (stripDebug.get()) {
|
||||
args.add("--strip-debug");
|
||||
}
|
||||
if (noHeaderFiles.get()) {
|
||||
args.add("--no-header-files");
|
||||
}
|
||||
if (noManPages.get()) {
|
||||
args.add("--no-man-pages");
|
||||
}
|
||||
if (endian.get() != JLinkExtension.Endian.NATIVE) {
|
||||
args.add("--endian");
|
||||
args.add(endian.get().toString().toLowerCase(Locale.ROOT));
|
||||
}
|
||||
setArgs(args);
|
||||
super.exec();
|
||||
}
|
||||
|
||||
private void delete(Path path) throws IOException {
|
||||
Files.walkFileTree(path, new SimpleFileVisitor<>() {
|
||||
@Override
|
||||
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
|
||||
Files.delete(dir);
|
||||
return FileVisitResult.CONTINUE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
|
||||
Files.delete(file);
|
||||
return FileVisitResult.CONTINUE;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue