Skip to content

Commit 830b726

Browse files
committed
architecture checks improved and refactoring
1 parent 93cff54 commit 830b726

File tree

11 files changed

+181
-55
lines changed

11 files changed

+181
-55
lines changed

src/main/java/io/github/fvarrui/javapackager/maven/CreateWindowsExeLaunch4j.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@
1717
import org.apache.commons.lang3.StringUtils;
1818
import org.twdata.maven.mojoexecutor.MojoExecutor.Element;
1919

20+
import io.github.fvarrui.javapackager.model.Arch;
2021
import io.github.fvarrui.javapackager.model.WindowsConfig;
2122
import io.github.fvarrui.javapackager.model.WindowsExeCreationTool;
2223
import io.github.fvarrui.javapackager.packagers.AbstractCreateWindowsExe;
2324
import io.github.fvarrui.javapackager.packagers.Context;
2425
import io.github.fvarrui.javapackager.packagers.WindowsPackager;
2526
import io.github.fvarrui.javapackager.utils.FileUtils;
27+
import io.github.fvarrui.javapackager.utils.Logger;
2628

2729
/**
2830
* Creates Windows executable with Maven
@@ -47,9 +49,15 @@ protected File doApply(WindowsPackager packager) throws Exception {
4749
String jreMinVersion = packager.getJreMinVersion();
4850
File jarFile = packager.getJarFile();
4951
File appFolder = packager.getAppFolder();
52+
Arch arch = packager.getArch();
5053

5154
createAssets(packager);
5255

56+
// warns about architecture
57+
if (arch != Arch.x86) {
58+
Logger.warn("Launch4J only can generate 32-bit executable");
59+
}
60+
5361
// copies JAR to app folder
5462
String jarPath;
5563
if (winConfig.isWrapJar()) {

src/main/java/io/github/fvarrui/javapackager/maven/ResolveLicenseFromPOM.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package io.github.fvarrui.javapackager.maven;
22

33
import java.io.File;
4-
import java.io.IOException;
54
import java.net.MalformedURLException;
65
import java.net.URI;
76
import java.net.URISyntaxException;

src/main/java/io/github/fvarrui/javapackager/model/Arch.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ public enum Arch {
88
x64,
99
x86;
1010

11-
public static Arch getDefault() {
12-
switch (SystemUtils.OS_ARCH) {
11+
public static Arch getArch(String archString) {
12+
switch (archString) {
1313
case "x86":
1414
case "i386":
1515
case "i486":
@@ -22,15 +22,19 @@ public static Arch getDefault() {
2222
case "aarch64":
2323
return aarch64;
2424
default:
25-
throw new IllegalArgumentException("Unknown architecture " + SystemUtils.OS_ARCH);
25+
throw new IllegalArgumentException("Unknown architecture " + archString);
2626
}
2727
}
2828

29-
public Architecture toRpmArchitecture() {
30-
switch (this.toString()) {
31-
case "aarch64": return Architecture.AARCH64;
32-
case "x64": return Architecture.X86_64;
33-
case "x86": return Architecture.I386;
29+
public static Arch getDefault() {
30+
return getArch(SystemUtils.OS_ARCH);
31+
}
32+
33+
public Architecture toRpmArchitecture() {
34+
switch (this) {
35+
case aarch64: return Architecture.AARCH64;
36+
case x64: return Architecture.X86_64;
37+
case x86: return Architecture.I386;
3438
default: return null;
3539
}
3640
}

src/main/java/io/github/fvarrui/javapackager/model/Platform.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,13 @@ public static Platform getCurrentPlatform() {
2323
return null;
2424
}
2525

26+
public static Platform getPlatform(String platformString) {
27+
switch (platformString.toLowerCase()) {
28+
case "linux": return linux;
29+
case "darwin": return mac;
30+
case "windows": return windows;
31+
default: throw new IllegalArgumentException("Unknown platform " + platformString);
32+
}
33+
}
34+
2635
}

src/main/java/io/github/fvarrui/javapackager/model/WindowsConfig.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package io.github.fvarrui.javapackager.model;
22

3-
import static org.apache.commons.lang3.StringUtils.defaultIfBlank;
43
import static io.github.fvarrui.javapackager.utils.ObjectUtils.defaultIfNull;
4+
import static org.apache.commons.lang3.StringUtils.defaultIfBlank;
55

66
import java.io.File;
77
import java.io.Serializable;
88
import java.util.LinkedHashMap;
99
import java.util.UUID;
1010

11+
import org.apache.commons.collections4.MapUtils;
12+
1113
import io.github.fvarrui.javapackager.packagers.Packager;
1214

1315
/**
@@ -351,6 +353,11 @@ public void setDefaults(Packager packager) {
351353
this.setInternalName(defaultIfBlank(this.getInternalName(), packager.getName()));
352354
this.setOriginalFilename(defaultIfBlank(this.getOriginalFilename(), packager.getName() + ".exe"));
353355
this.setMsiUpgradeCode(defaultIfBlank(this.getMsiUpgradeCode(), UUID.randomUUID().toString()));
356+
// init setup languages
357+
if (MapUtils.isEmpty(this.getSetupLanguages())) {
358+
this.getSetupLanguages().put("english", "compiler:Default.isl");
359+
this.getSetupLanguages().put("spanish", "compiler:Languages\\Spanish.isl");
360+
}
354361
}
355362

356363
}

src/main/java/io/github/fvarrui/javapackager/packagers/BundleJre.java

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.util.ArrayList;
55
import java.util.Arrays;
66
import java.util.List;
7+
import java.util.Map;
78
import java.util.stream.Collectors;
89

910
import org.apache.commons.lang3.StringUtils;
@@ -58,23 +59,32 @@ protected File doApply(Packager packager) throws Exception {
5859
throw new Exception("'" + specificJreFolder + "' is not a directory!");
5960
}
6061

61-
// checks if the specified jre is valid (it looks for 'release' file into it, and if so, checks if it matches the right platform
62+
// checks if the specified jre is valid (it looks for 'release' file into it, and if so, checks if it matches the right platform
63+
boolean validJre = true;
6264
if (!JDKUtils.isValidJRE(platform, specificJreFolder)) {
6365

64-
Logger.warn("An invalid JRE may have been specified for '" + platform + "' platform: " + specificJreFolder + " ('release' file not found)");
65-
66-
// try to fix the path to the JRE on MacOS adding Contents/Home to JRE path
66+
// if platform is mac
6767
if (platform.equals(Platform.mac)) {
6868

69+
// try to fix the path to the JRE on MacOS adding Contents/Home to JRE path
6970
File fixedJreFolder = new File(specificJreFolder, "Contents/Home");
7071
if (JDKUtils.isValidJRE(platform, fixedJreFolder)) {
7172
specificJreFolder = fixedJreFolder;
72-
Logger.warn("Specified 'jrePath' fixed: " + specificJreFolder);
73+
Logger.warn("Specified 'jrePath' fixed: " + specificJreFolder);
74+
} else {
75+
validJre = false;
7376
}
7477

78+
} else {
79+
validJre = false;
7580
}
7681

7782
}
83+
if (!validJre) {
84+
Logger.warn("An invalid JRE may have been specified for '" + platform + "' platform: " + specificJreFolder);
85+
} else if (JDKUtils.isJDK(specificJreFolder)) {
86+
Logger.warn("Wow! Embedding a JDK instead of a JRE ... are you sure you want to do that?");
87+
}
7888

7989
// removes old jre folder from bundle
8090
if (destinationFolder.exists()) FileUtils.removeFolder(destinationFolder);
@@ -124,7 +134,12 @@ protected File doApply(Packager packager) throws Exception {
124134
Logger.info("Using " + modulesDir + " modules directory");
125135

126136
if (destinationFolder.exists()) FileUtils.removeFolder(destinationFolder);
137+
138+
// gets JDK release info
139+
Map<String,String> releaseMap = JDKUtils.getRelease(jdkPath);
140+
String releaseInfo = "add:IMAGE_TYPE=\"JRE\":OS_ARCH=\"" + releaseMap.get("OS_ARCH") + "\":OS_NAME=" + releaseMap.get("OS_NAME") + "\"";
127141

142+
// full path to jlink command
128143
File jlink = new File(currentJdk, "/bin/jlink");
129144

130145
// generates customized jre using modules
@@ -136,7 +151,8 @@ protected File doApply(Packager packager) throws Exception {
136151
"--output", destinationFolder,
137152
"--no-header-files",
138153
"--no-man-pages",
139-
"--strip-debug",
154+
"--strip-debug",
155+
"--release-info", releaseInfo,
140156
(VersionUtils.getJavaMajorVersion() < 21 ? "--compress=2" : null)
141157
);
142158

src/main/java/io/github/fvarrui/javapackager/packagers/CreateWindowsExeWinRun4j.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import io.github.fvarrui.javapackager.model.WindowsConfig;
1313
import io.github.fvarrui.javapackager.model.WindowsExeCreationTool;
1414
import io.github.fvarrui.javapackager.utils.FileUtils;
15+
import io.github.fvarrui.javapackager.utils.JDKUtils;
1516
import io.github.fvarrui.javapackager.utils.Logger;
1617
import io.github.fvarrui.javapackager.utils.RcEdit;
1718
import io.github.fvarrui.javapackager.utils.VelocityUtils;
@@ -68,11 +69,16 @@ protected File doApply(WindowsPackager packager) throws Exception {
6869
// creates generic manifest
6970
FileUtils.copyFileToFile(iconFile, getGenericIcon());
7071

72+
// checks if desired arch matches with JRE arch
73+
if (bundleJre && !JDKUtils.isValidJRE(Platform.windows, arch, packager.getJreDestinationFolder())) {
74+
throw new Exception("Bundled JRE must match " + Platform.windows + " " + arch);
75+
}
76+
7177
// creates generic exe
72-
if (arch == Arch.x64) {
78+
if (arch == Arch.x86) {
79+
FileUtils.copyResourceToFile("/windows/WinRun4J.exe", getGenericExe());
80+
} else {
7381
FileUtils.copyResourceToFile("/windows/WinRun4J64.exe", getGenericExe());
74-
} else if (arch == Arch.x86) {
75-
FileUtils.copyResourceToFile("/windows/WinRun4J.exe", getGenericExe());
7682
}
7783

7884
// uses vmLocation only if a JRE is bundled

src/main/java/io/github/fvarrui/javapackager/packagers/GenerateRpm.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import org.redline_rpm.header.Os;
1212
import org.redline_rpm.header.RpmType;
1313

14-
import io.github.fvarrui.javapackager.model.Arch;
1514
import io.github.fvarrui.javapackager.utils.FileUtils;
1615
import io.github.fvarrui.javapackager.utils.Logger;
1716
import io.github.fvarrui.javapackager.utils.VelocityUtils;

src/main/java/io/github/fvarrui/javapackager/packagers/Packager.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,6 @@ private void init() throws Exception {
140140
throw new Exception("Invalid name specified: " + name, e);
141141
}
142142

143-
// init setup languages
144-
if (platform.equals(Platform.windows) && (winConfig.getSetupLanguages() == null || winConfig.getSetupLanguages().isEmpty())) {
145-
winConfig.getSetupLanguages().put("english", "compiler:Default.isl");
146-
winConfig.getSetupLanguages().put("spanish", "compiler:Languages\\Spanish.isl");
147-
}
148-
149143
doInit();
150144

151145
// removes not necessary platform specific configs

src/main/java/io/github/fvarrui/javapackager/packagers/WindowsPackager.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import org.apache.commons.lang3.StringUtils;
88

9+
import io.github.fvarrui.javapackager.model.Arch;
910
import io.github.fvarrui.javapackager.model.Platform;
1011
import io.github.fvarrui.javapackager.utils.Logger;
1112
import io.github.fvarrui.javapackager.utils.VelocityUtils;
@@ -38,6 +39,14 @@ public WindowsPackager() {
3839
@Override
3940
public void doInit() throws Exception {
4041

42+
// sets default system architecture
43+
if (getArch() != Arch.x64 && getArch() != Arch.x86) {
44+
if (Platform.windows.isCurrentPlatform())
45+
arch(Arch.getDefault());
46+
else
47+
arch(Arch.x64);
48+
}
49+
4150
// sets windows config default values
4251
this.winConfig.setDefaults(this);
4352

@@ -79,7 +88,7 @@ public File doCreateApp() throws Exception {
7988
classpath = StringUtils.join(classpaths, ";");
8089
}
8190

82-
// invokes launch4j to generate windows executable
91+
// invokes windows exe artifact generator (building tool dependant)
8392
executable = Context.getContext().createWindowsExe(this);
8493

8594
Logger.infoUnindent("Windows EXE file created in " + executable + "!");

0 commit comments

Comments
 (0)