Skip to content

Commit dd9f13c

Browse files
committed
U packager fully decoupled from maven
1 parent 71d7a70 commit dd9f13c

File tree

5 files changed

+89
-36
lines changed

5 files changed

+89
-36
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,12 +311,13 @@ public void execute() throws MojoExecutionException {
311311
.vmArgs(vmArgs)
312312
.winConfig(winConfig);
313313

314+
// sets Maven specific functions
314315
packager.setCreateRunnableJar(new CreateRunnableJar());
316+
packager.setResolveLicenseFunction(new ResolveLicenseFromPOM());
315317

318+
// generate app, installers and bundles
316319
packager.createApp();
317-
318320
packager.generateInstallers();
319-
320321
packager.createBundles();
321322

322323
} catch (Exception e) {
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package io.github.fvarrui.javapackager.maven;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.net.MalformedURLException;
6+
import java.net.URL;
7+
import java.util.List;
8+
import java.util.function.Function;
9+
10+
import org.apache.maven.model.License;
11+
12+
import io.github.fvarrui.javapackager.packagers.Packager;
13+
import io.github.fvarrui.javapackager.utils.FileUtils;
14+
import io.github.fvarrui.javapackager.utils.Logger;
15+
16+
/**
17+
* Creates a runnable jar file from sources
18+
*/
19+
public class ResolveLicenseFromPOM implements Function<Packager, File> {
20+
21+
@Override
22+
public File apply(Packager packager) {
23+
Logger.infoIndent("Resolving license from POM ...");
24+
25+
File licenseFile = packager.getLicenseFile();
26+
List<License> licenses = MavenContext.getEnv().getMavenProject().getLicenses();
27+
File assetsFolder = packager.getAssetsFolder();
28+
29+
// if license not specified, gets from pom
30+
if (licenseFile == null && !licenses.isEmpty()) {
31+
String urlStr = null;
32+
try {
33+
urlStr = licenses.get(0).getUrl();
34+
URL licenseUrl = new URL(urlStr);
35+
licenseFile = new File(assetsFolder, "LICENSE");
36+
FileUtils.downloadFromUrl(licenseUrl, licenseFile);
37+
} catch (MalformedURLException e) {
38+
Logger.error("Invalid license URL specified: " + urlStr);
39+
licenseFile = null;
40+
} catch (IOException e) {
41+
Logger.error("Cannot download license from " + urlStr);
42+
licenseFile = null;
43+
}
44+
}
45+
46+
Logger.infoUnindent("License resolved " + licenseFile + "!");
47+
48+
return licenseFile;
49+
}
50+
51+
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ private File generateRpmPackage() throws Exception {
129129
),
130130
goal("rpm"),
131131
configuration(
132-
element("license", getLicenseName()),
133132
element("packager", organizationName),
134133
element("group", "Application"),
135134
element("icon", xpmIcon.getAbsolutePath()),

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

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@
1111
import static org.twdata.maven.mojoexecutor.MojoExecutor.version;
1212

1313
import java.io.File;
14-
import java.io.IOException;
15-
import java.net.MalformedURLException;
16-
import java.net.URL;
1714
import java.nio.file.InvalidPathException;
1815
import java.nio.file.Paths;
1916
import java.util.ArrayList;
@@ -24,7 +21,6 @@
2421

2522
import org.apache.commons.lang3.StringUtils;
2623
import org.apache.commons.lang3.SystemUtils;
27-
import org.apache.maven.model.License;
2824

2925
import io.github.fvarrui.javapackager.maven.MavenContext;
3026
import io.github.fvarrui.javapackager.model.Platform;
@@ -85,6 +81,18 @@ public File createRunnableJar() {
8581
return createRunnableJarFunction.apply(this);
8682
}
8783

84+
// resolve license
85+
86+
protected Function<Packager, File> resolveLicenseFunction;
87+
88+
public Function<Packager, File> getResolveLicenseFunction() {
89+
return resolveLicenseFunction;
90+
}
91+
92+
public void setResolveLicenseFunction(Function<Packager, File> resolveLicenseFunction) {
93+
this.resolveLicenseFunction = resolveLicenseFunction;
94+
}
95+
8896
// ===============================================
8997

9098
public Packager() {
@@ -154,7 +162,7 @@ public void resolveResources() throws Exception {
154162
Logger.infoIndent("Resolving resources ...");
155163

156164
// locates license file
157-
licenseFile = resolveLicense(licenseFile, MavenContext.getEnv().getMavenProject().getLicenses());
165+
licenseFile = resolveLicense(licenseFile);
158166

159167
// locates icon file
160168
iconFile = resolveIcon(iconFile, name, assetsFolder);
@@ -169,11 +177,6 @@ public void resolveResources() throws Exception {
169177
Logger.infoUnindent("Resources resolved!");
170178

171179
}
172-
173-
protected String getLicenseName() {
174-
List<License> licenses = MavenContext.getEnv().getMavenProject().getLicenses();
175-
return licenses != null && !licenses.isEmpty() && licenses.get(0) != null ? licenses.get(0).getName() : "";
176-
}
177180

178181
/**
179182
* Copies all dependencies to app folder
@@ -468,35 +471,21 @@ protected String getRequiredModules(File libsFolder, boolean customizedJre, File
468471
/**
469472
* Locates license file
470473
* @param licenseFile Specified license file
471-
* @param licenses Licenses list from POM
472474
* @return Resolved license file
473475
*/
474-
protected File resolveLicense(File licenseFile, List<License> licenses) {
476+
protected File resolveLicense(File licenseFile) {
475477

476-
// if default license file doesn't exist and there's a license specified in
477-
// pom.xml file, gets this last one
478+
// if default license file doesn't exist
478479
if (licenseFile != null && !licenseFile.exists()) {
479480
Logger.warn("Specified license file doesn't exist: " + licenseFile.getAbsolutePath());
480481
licenseFile = null;
481482
}
482-
// if license not specified, gets from pom
483-
if (licenseFile == null && !licenses.isEmpty()) {
484-
485-
String urlStr = null;
486-
try {
487-
urlStr = licenses.get(0).getUrl();
488-
URL licenseUrl = new URL(urlStr);
489-
licenseFile = new File(assetsFolder, "LICENSE");
490-
FileUtils.downloadFromUrl(licenseUrl, licenseFile);
491-
} catch (MalformedURLException e) {
492-
Logger.error("Invalid license URL specified: " + urlStr);
493-
licenseFile = null;
494-
} catch (IOException e) {
495-
Logger.error("Cannot download license from " + urlStr);
496-
licenseFile = null;
497-
}
498-
483+
484+
// invokes custom license resolver if exists
485+
if (resolveLicenseFunction != null) {
486+
licenseFile = resolveLicenseFunction.apply(this);
499487
}
488+
500489
// if license is still null, looks for LICENSE file
501490
if (licenseFile == null || !licenseFile.exists()) {
502491
licenseFile = new File(MavenContext.getEnv().getMavenProject().getBasedir(), "LICENSE");
@@ -521,13 +510,23 @@ protected File resolveLicense(File licenseFile, List<License> licenses) {
521510
* @throws Exception Process failed
522511
*/
523512
protected File resolveIcon(File iconFile, String name, File assetsFolder) throws Exception {
513+
514+
// searchs for specific icons
515+
switch (platform) {
516+
case linux: iconFile = FileUtils.exists(linuxConfig.getPngFile()) ? linuxConfig.getPngFile() : null; break;
517+
case mac: iconFile = FileUtils.exists(macConfig.getIcnsFile()) ? macConfig.getIcnsFile() : null; break;
518+
case windows: iconFile = FileUtils.exists(winConfig.getIcoFile()) ? winConfig.getIcoFile() : null; break;
519+
default:
520+
}
524521

525-
String iconExtension = IconUtils.getIconFileExtensionByPlatform(platform);
522+
String iconExtension = IconUtils.getIconFileExtensionByPlatform(platform);
526523

524+
// if not specific icon specified for target platform, searchs for an icon in "${assetsDir}" folder
527525
if (iconFile == null) {
528526
iconFile = new File(assetsDir, platform + "/" + name + iconExtension);
529527
}
530528

529+
// if there's no icon yet, uses default one
531530
if (!iconFile.exists()) {
532531
iconFile = new File(assetsFolder, iconFile.getName());
533532
FileUtils.copyResourceToFile("/" + platform + "/default-icon" + iconExtension, iconFile);
@@ -538,7 +537,6 @@ protected File resolveIcon(File iconFile, String name, File assetsFolder) throws
538537
return iconFile;
539538
}
540539

541-
542540
/**
543541
* Bundling app folder in tarball and/or zipball
544542
* @throws Exception Process failed

src/main/java/io/github/fvarrui/javapackager/utils/FileUtils.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,5 +208,9 @@ public static void downloadFromUrl(URL url, File file) throws IOException {
208208
org.apache.commons.io.FileUtils.copyURLToFile(url, file);
209209
Logger.info("File downloaded from [" + url + "] to [" + file.getAbsolutePath() + "]");
210210
}
211+
212+
public static boolean exists(File file) {
213+
return file != null && file.exists();
214+
}
211215

212216
}

0 commit comments

Comments
 (0)