Repository navigation
Expand file tree
/
Copy pathJlinkTask.java
More file actions
97 lines (73 loc) · 2.83 KB
/
Copy pathJlinkTask.java
File metadata and controls
97 lines (73 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package com.github.stickerifier.stickerify;
import org.gradle.api.DefaultTask;
import org.gradle.api.file.DirectoryProperty;
import org.gradle.api.file.FileSystemOperations;
import org.gradle.api.file.ProjectLayout;
import org.gradle.api.logging.LogLevel;
import org.gradle.api.provider.ListProperty;
import org.gradle.api.provider.Property;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.Nested;
import org.gradle.api.tasks.OutputDirectory;
import org.gradle.api.tasks.TaskAction;
import org.gradle.jvm.toolchain.JavaCompiler;
import org.gradle.process.ExecOperations;
import org.jetbrains.annotations.NotNull;
import javax.inject.Inject;
import java.io.ByteArrayOutputStream;
import java.util.List;
public abstract class JlinkTask extends DefaultTask {
@Input
public abstract ListProperty<@NotNull String> getOptions();
@Input
public abstract ListProperty<@NotNull String> getModules();
/**
* If the selected JVM is <a href="https://openjdk.org/jeps/493">JEP 493</a> compatible, then this should be set to false.
*/
@Input
public abstract Property<@NotNull Boolean> getIncludeModulePath();
@OutputDirectory
public abstract DirectoryProperty getOutputDirectory();
@Nested
public abstract Property<JavaCompiler> getJavaCompiler();
@Inject
protected abstract FileSystemOperations getFs();
@Inject
protected abstract ExecOperations getExec();
@Inject
public JlinkTask(ProjectLayout layout) {
setGroup("build");
setDescription("Generates a custom Java runtime image using jlink.");
getOptions().convention(List.of());
getModules().convention(List.of("ALL-MODULE-PATH"));
getIncludeModulePath().convention(true);
getOutputDirectory().convention(layout.getBuildDirectory().dir(getName()));
}
@TaskAction
public void createJre() {
var installationPath = getJavaCompiler().get().getMetadata().getInstallationPath();
var jlink = installationPath.file("bin/jlink").getAsFile();
var jmods = installationPath.dir("jmods").getAsFile();
var jlinkOutput = getOutputDirectory().dir("jre").get().getAsFile();
getFs().delete(deleteSpec -> deleteSpec.delete(jlinkOutput));
var stdout = new ByteArrayOutputStream();
var result = getExec().exec(execSpec -> {
execSpec.setIgnoreExitValue(true);
execSpec.setCommandLine(jlink.getAbsolutePath());
execSpec.args(getOptions().get());
if (getIncludeModulePath().get()) {
execSpec.args("--module-path", jmods.getAbsolutePath());
}
execSpec.args("--add-modules", String.join(",", getModules().get()));
execSpec.args("--output", jlinkOutput.getAbsolutePath());
execSpec.setStandardOutput(stdout);
execSpec.setErrorOutput(stdout);
});
var stdoutStr = stdout.toString();
if (!stdoutStr.isEmpty()) {
var level = result.getExitValue() == 0 ? LogLevel.INFO : LogLevel.ERROR;
getLogger().log(level, stdoutStr);
}
result.assertNormalExitValue();
}
}