Skip to content

Commit 60e4c8c

Browse files
authored
Move jlink parameters into project.properties (#1062)
* Overhaul JLink * Add Semeru support * Add Microsoft JDK support * Add Amazon support, * Add Zulu support * Mac: Fix Java.runtime bundle version * Update CLI documentation
1 parent 3925b53 commit 60e4c8c

File tree

15 files changed

+420
-240
lines changed

15 files changed

+420
-240
lines changed

ant/project.properties

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ javac.source=1.8
3232
javac.target=1.8
3333
java.download=https://bell-sw.com/pages/downloads/#/java-11-lts
3434

35+
# Java vendor to bundle into software (e.g. "*BellSoft|Adoptium|Microsoft|Amazon|IBM")
36+
jlink.java.vendor="BellSoft"
37+
# Java vendor to bundle into software (e.g. "11.0.17+7")
38+
jlink.java.version="11.0.17+7"
39+
# Java garbage collector flavor to use (e.g. "hotspot|openj9")
40+
jlink.java.gc="hotspot"
41+
# Java garbage collector version to use (e.g. openj9: "0.35.0", zulu: "11.62.17")
42+
jlink.java.gc.version="gc-ver-is-empty"
43+
3544
# Skip bundling the java runtime
3645
# jre.skip=true
3746

build.xml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -240,10 +240,12 @@
240240
<echo level="info">Downloading and bundling the jre for ${target.os}</echo>
241241
<java jar="${dist.dir}/${project.filename}.jar" fork="true" failonerror="true">
242242
<arg value="jlink"/>
243-
<arg value="--platform"/>
244-
<arg value="${target.os}"/>
245-
<arg value="--arch"/>
246-
<arg value="${target.arch}"/>
243+
<arg line="--platform ${target.os}"/>
244+
<arg line="--arch ${target.arch}"/>
245+
<arg line="--vendor ${jlink.java.vendor}"/>
246+
<arg line="--version ${jlink.java.version}"/>
247+
<arg line="--gc ${jlink.java.gc}"/>
248+
<arg line="--gcversion ${jlink.java.gc.version}"/><!-- openj9 only -->
247249
</java>
248250
</target>
249251

src/org/dyorgio/jna/platform/mac/NSObject.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public void release() {
5858
}
5959

6060
@Override
61-
@SuppressWarnings("FinalizeDeclaration")
61+
@SuppressWarnings({"FinalizeDeclaration","deprecation","removal"})
6262
protected void finalize() throws Throwable {
6363
release();
6464
super.finalize();

src/qz/build/JLink.java

Lines changed: 69 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
import org.apache.commons.io.FileUtils;
1515
import org.apache.logging.log4j.LogManager;
1616
import org.apache.logging.log4j.Logger;
17+
import qz.build.jlink.Arch;
18+
import qz.build.jlink.Platform;
19+
import qz.build.jlink.Vendor;
20+
import qz.build.jlink.Url;
1721
import qz.common.Constants;
1822
import qz.utils.*;
1923

@@ -22,44 +26,56 @@
2226
import java.nio.file.*;
2327
import java.util.HashMap;
2428
import java.util.LinkedHashSet;
25-
import java.util.Locale;
2629

2730
public class JLink {
2831
private static final Logger log = LogManager.getLogger(JLink.class);
29-
private static final String JAVA_VENDOR = "BellSoft";
30-
private static final String JAVA_VERSION = "11.0.17+7";
31-
private static final String JAVA_MAJOR = JAVA_VERSION.split("\\.")[0];
32-
private static final String JAVA_MINOR = JAVA_VERSION.split("\\.")[1];
33-
private static final String JAVA_PATCH = JAVA_VERSION.split("\\.|\\+|-")[2];
34-
private static final String JAVA_VERSION_FILE = JAVA_VERSION.replaceAll("\\+", "_");
35-
private static final String JAVA_DEFAULT_GC_ENGINE = "hotspot";
36-
private static final String JAVA_DEFAULT_ARCH = VendorArch.ADOPT_AMD64.use;
32+
public static final Vendor JAVA_DEFAULT_VENDOR = Vendor.BELLSOFT;
33+
private static final String JAVA_DEFAULT_VERSION = "11.0.17+7";
34+
private static final String JAVA_DEFAULT_GC_ENGINE = "hotspot"; // or "openj9"
35+
private static final String JAVA_DEFAULT_GC_VERSION = "0.35.0"; // openj9 gc only
3736

3837
private Path jarPath;
3938
private Path jdepsPath;
4039
private Path jlinkPath;
4140
private Path jmodsPath;
4241
private Path outPath;
4342
private Version jdepsVersion;
44-
private String javaVendor;
45-
private String targetPlatform;
43+
private Platform hostPlatform;
44+
private Platform targetPlatform;
45+
private Arch hostArch;
46+
private Arch targetArch;
47+
private Vendor javaVendor;
48+
private String gcEngine;
49+
private String javaVersion;
50+
private String gcVersion;
51+
52+
private Version javaSemver;
53+
4654
private LinkedHashSet<String> depList;
4755

48-
public JLink(String targetPlatform, String arch, String gcEngine) throws IOException {
49-
this.javaVendor = JAVA_VENDOR;
50-
this.targetPlatform = targetPlatform;
56+
public JLink(String targetPlatform, String targetArch, String javaVendor, String javaVersion, String gcEngine, String gcVersion) throws IOException {
57+
this.hostPlatform = Platform.getCurrentPlatform();
58+
this.hostArch = Arch.getCurrentArch();
59+
60+
this.targetPlatform = Platform.parse(targetPlatform, this.hostPlatform);
61+
this.targetArch = Arch.parse(targetArch, this.hostArch);
62+
this.javaVendor = Vendor.parse(javaVendor, JAVA_DEFAULT_VENDOR);
63+
this.gcEngine = getParam("gcEngine", gcEngine, JAVA_DEFAULT_GC_ENGINE);
64+
this.javaVersion = getParam("javaVersion", javaVersion, JAVA_DEFAULT_VERSION);
65+
this.gcVersion = getParam("gcVersion", gcVersion, JAVA_DEFAULT_GC_VERSION);
66+
67+
this.javaSemver = SystemUtilities.getJavaVersion(this.javaVersion);
5168

52-
if(needsDownload(SystemUtilities.getJavaVersion(JAVA_VERSION), Constants.JAVA_VERSION)) {
53-
log.warn("Java versions are incompatible, locating a suitable runtime for Java " + JAVA_MAJOR + "...");
54-
String hostArch = System.getProperty("os.arch");
55-
String hostJdk = downloadJdk(JAVA_VENDOR, null, hostArch, gcEngine);
69+
if(needsDownload(javaSemver, Constants.JAVA_VERSION)) {
70+
log.warn("Java versions are incompatible, locating a suitable runtime for Java " + javaSemver.getMajorVersion() + "...");
71+
String hostJdk = downloadJdk(this.hostArch, this.hostPlatform);
5672
calculateToolPaths(Paths.get(hostJdk));
5773
} else {
5874
calculateToolPaths(null);
5975
}
6076

61-
String extractedJdk = downloadJdk(javaVendor, targetPlatform, arch, gcEngine);
62-
jmodsPath = Paths.get(extractedJdk, "jmods");
77+
String targetJdk = downloadJdk(this.targetArch, this.targetPlatform);
78+
jmodsPath = Paths.get(targetJdk, "jmods");
6379
log.info("Selecting jmods folder: {}", jmodsPath);
6480

6581
calculateJarPath()
@@ -69,16 +85,7 @@ public JLink(String targetPlatform, String arch, String gcEngine) throws IOExcep
6985
}
7086

7187
public static void main(String ... args) throws IOException {
72-
JLink jlink = new JLink(null, null, null).calculateJarPath();
73-
System.out.println(jlink.jarPath);
74-
if(true) {
75-
System.exit(0);
76-
}
77-
78-
79-
new JLink(args.length > 0 ? args[0] : null,
80-
args.length > 1 ? args[1] : null,
81-
args.length > 2 ? args[2] : null);
88+
new JLink(null, null, null, null, null, null).calculateJarPath();
8289
}
8390

8491
/**
@@ -103,53 +110,18 @@ private static boolean needsDownload(Version want, Version installed) {
103110
/**
104111
* Download the JDK and return the path it was extracted to
105112
*/
106-
private static String downloadJdk(String javaVendor, String platform, String arch, String gcEngine) throws IOException {
107-
if(platform == null) {
108-
// Must match ArgValue.JLINK --platform values
109-
switch(SystemUtilities.getOsType()) {
110-
case MAC:
111-
platform = "mac";
112-
break;
113-
case WINDOWS:
114-
platform = "windows";
115-
break;
116-
default:
117-
platform = "linux";
118-
}
119-
120-
log.info("No platform specified, assuming '{}'", platform);
121-
}
122-
123-
arch = VendorArch.match(javaVendor, arch, JAVA_DEFAULT_ARCH);
124-
platform = VendorOs.match(javaVendor, platform);
125-
126-
127-
if(gcEngine == null) {
128-
gcEngine = JAVA_DEFAULT_GC_ENGINE;
129-
log.info("No garbage collector specified, assuming '{}'", gcEngine);
130-
}
131-
132-
String fileExt;
133-
switch(VendorUrlPattern.getVendor(javaVendor)) {
134-
case BELL:
135-
fileExt = platform.equals("linux") ? "tar.gz" : "zip";
136-
break;
137-
case ADOPT:
138-
default:
139-
fileExt = platform.equals("windows") ? "zip" : "tar.gz";
140-
}
141-
142-
String url = VendorUrlPattern.format(javaVendor, arch, platform, gcEngine, JAVA_MAJOR, JAVA_VERSION, JAVA_VERSION_FILE, fileExt);
113+
private String downloadJdk(Arch arch, Platform platform) throws IOException {
114+
String url = new Url(this.javaVendor).format(arch, platform, this.gcEngine, this.javaSemver, this.gcVersion);
143115

144116
// Saves to out e.g. "out/jlink/jdk-AdoptOpenjdk-amd64-platform-11_0_7"
145-
String extractedJdk = new Fetcher(String.format("jlink/jdk-%s-%s-%s-%s", javaVendor.toLowerCase(Locale.ENGLISH), arch, platform, JAVA_VERSION_FILE), url)
117+
String extractedJdk = new Fetcher(String.format("jlink/jdk-%s-%s-%s-%s", javaVendor.value(), arch.value(), platform.value(), javaSemver.toString().replaceAll("\\+", "_")), url)
146118
.fetch()
147119
.uncompress();
148120

149121
// Get first subfolder, e.g. jdk-11.0.7+10
150122
for(File subfolder : new File(extractedJdk).listFiles(pathname -> pathname.isDirectory())) {
151123
extractedJdk = subfolder.getPath();
152-
if(platform.equals("mac") && Paths.get(extractedJdk, "/Contents/Home").toFile().isDirectory()) {
124+
if(platform == Platform.MAC && Paths.get(extractedJdk, "/Contents/Home").toFile().isDirectory()) {
153125
extractedJdk += "/Contents/Home";
154126
}
155127
log.info("Selecting JDK home: {}", extractedJdk);
@@ -165,18 +137,20 @@ private JLink calculateJarPath() {
165137
} else {
166138
// Detect out/dist/qz-tray.jar for IDE usage
167139
jarPath = SystemUtilities.getJarParentPath()
168-
.resolve("../../")
140+
.resolve("dist")
169141
.resolve(Constants.PROPS_FILE + ".jar");
170142
}
171143
log.info("Assuming jar path: {}", jarPath);
172144
return this;
173145
}
174146

175147
private JLink calculateOutPath() {
176-
if(targetPlatform.equals("mac")) {
177-
outPath = jarPath.resolve("../Java.runtime/Contents/Home").normalize();
178-
} else {
179-
outPath = jarPath.resolve("../runtime").normalize();
148+
switch(targetPlatform) {
149+
case MAC:
150+
outPath = jarPath.resolve("../Java.runtime/Contents/Home").normalize();
151+
break;
152+
default:
153+
outPath = jarPath.resolve("../runtime").normalize();
180154
}
181155
log.info("Assuming output path: {}", outPath);
182156
return this;
@@ -231,7 +205,7 @@ private JLink calculateDepList() throws IOException {
231205
}
232206

233207
private JLink deployJre() throws IOException {
234-
if(targetPlatform.equals("mac")) {
208+
if(targetPlatform == Platform.MAC) {
235209
// Deploy Contents/MacOS/libjli.dylib
236210
Path macOS = Files.createDirectories(outPath.resolve("../MacOS").normalize());
237211
Path jliLib = macOS.resolve("libjli.dylib");
@@ -247,9 +221,10 @@ private JLink deployJre() throws IOException {
247221
// Deploy Contents/Info.plist
248222
HashMap<String, String> fieldMap = new HashMap<>();
249223
fieldMap.put("%BUNDLE_ID%", MacUtilities.getBundleId() + ".jre"); // e.g. io.qz.qz-tray.jre
250-
fieldMap.put("%BUNDLE_VERSION%", String.format("%s.%s.%s", JAVA_MAJOR, JAVA_MINOR, JAVA_PATCH));
251-
fieldMap.put("%BUNDLE_VERSION_FULL%", JAVA_VERSION);
252-
fieldMap.put("%BUNDLE_VENDOR%", javaVendor);
224+
fieldMap.put("%BUNDLE_VERSION%", String.format("%s.%s.%s", javaSemver.getMajorVersion(), javaSemver.getMinorVersion(), javaSemver.getPatchVersion()));
225+
fieldMap.put("%BUNDLE_VERSION_FULL%", javaSemver.toString());
226+
fieldMap.put("%BUNDLE_VENDOR%", javaVendor.getVendorName());
227+
fieldMap.put("%BUNDLE_PRODUCT%", javaVendor.getProductName());
253228
log.info("Deploying {}/Info.plist", macOS.getParent());
254229
FileUtilities.configureAssetFile("assets/mac-runtime.plist.in", macOS.getParent().resolve("Info.plist"), fieldMap, JLink.class);
255230
}
@@ -270,13 +245,15 @@ private JLink deployJre() throws IOException {
270245
// Remove all but java/javaw
271246
String[] keepFiles;
272247
String keepExt;
273-
if(targetPlatform.equals("windows")) {
274-
keepFiles = new String[]{ "java.exe", "javaw.exe" };
275-
// Windows stores ".dll" files in bin
276-
keepExt = ".dll";
277-
} else {
278-
keepFiles = new String[]{ "java" };
279-
keepExt = null;
248+
switch(targetPlatform) {
249+
case WINDOWS:
250+
keepFiles = new String[]{ "java.exe", "javaw.exe" };
251+
// Windows stores ".dll" files in bin
252+
keepExt = ".dll";
253+
break;
254+
default:
255+
keepFiles = new String[]{ "java" };
256+
keepExt = null;
280257
}
281258

282259
Files.list(outPath.resolve("bin")).forEach(binFile -> {
@@ -300,4 +277,11 @@ private JLink deployJre() throws IOException {
300277
throw new IOException("An error occurred deploying the jre. Please check the logs for details.");
301278
}
302279

280+
public static String getParam(String paramName, String value, String fallback) {
281+
if(value != null && !value.isEmpty() && !value.trim().isEmpty()) {
282+
return value;
283+
}
284+
log.info("No {} specified, assuming '{}'", paramName, fallback);
285+
return fallback;
286+
}
303287
}

src/qz/build/VendorArch.java

Lines changed: 0 additions & 50 deletions
This file was deleted.

src/qz/build/VendorOs.java

Lines changed: 0 additions & 51 deletions
This file was deleted.

0 commit comments

Comments
 (0)