Skip to content

Commit e3949fe

Browse files
authored
Default values for configuration, especially VersionInfo (#205)
* Fix: Escape angle brackets in Markdown documentation to be printed out. GitHub uses one of many kinds of Markdown syntaxes. In this one it does not display plain text inside angle brackets (like <any text>) in the headers, so we need to escape them. For details, please see the issue: microsoft/vscode#12491 * `disableVersionInfoDefaults` docs note added * Snapshot version bump according to default parameters changes * New parameter for disabling VersionInfo defaults * Filling out VersionInfo defaults * Filling out VersionInfo defaults * Unit tests for Copyright generator * Copyright generator description * JUnitParams lib added to POM.xml * Unit tests for Launch4j fileVersion generator * Additional Unit tests for Launch4j fileVersion generator * Refactoring of Launch4j fileVersion generator * "errTitle" default value provided * "errTitle" default value in docs * "versionInfo -> originalFilename" default value added * release version notes Resolves #98 * Line added to MOJO doc * trademarks & companyName also filled by defaults * Mockito Core library added * VersionInfo refactoring * VersionInfo unit tests * Adds missing VersionInfo unit tests * Adds missing Mojo param to MojoTest * Refactoring MojoTest * Missing dependency added to POM * Possibility of not filling out VersionInfo in XML at all * Documentation for VersionInfo defaults added * Throwing exception datailed descriptions * Release version without snapshot * Revert "Release version without snapshot" This reverts commit d019663. * default scope for filling out defaults inside VersionInfo * Newline's at the end of the files * Private constructors for utility classes added
1 parent a18045e commit e3949fe

File tree

13 files changed

+986
-41
lines changed

13 files changed

+986
-41
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ The full list of all the parameters is available [here](src/main/resources/MOJO.
2020

2121
# Version Notes
2222

23+
## Version notes 2.3.0 - 2022-12-30
24+
- provides default values for plugin configuration, especially for `<VersionInfo>`, see Issue [#98](../../issues/98)
25+
- adds a `disableVersionInfoDefaults` parameter to be able to disable provided defaults, see PR [#205](../../pull/205)
26+
- adds documentation notes regarding a new defaults, see [VERSIONINFO.md](src/main/resources/VERSIONINFO.md)
27+
- throwing exceptions with detailed description of default values formula, when cannot fulfill default values (no configuration data for formula), it helps plugin user with debugging what is wrong
28+
2329
## Version notes 2.2.0 - 2022-11-24
2430
- upgrades Launch4j to version 3.50 and adopts config to the bew requirements, see Issue [#199](../../issues/199)
2531
and PR [#200](../../pull/200) for more details what has to be changed

pom.xml

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<groupId>com.akathist.maven.plugins.launch4j</groupId>
2323
<artifactId>launch4j-maven-plugin</artifactId>
2424
<packaging>maven-plugin</packaging>
25-
<version>2.2.1-SNAPSHOT</version>
25+
<version>2.3.0-SNAPSHOT</version>
2626

2727
<name>Maven Launch4j Plugin</name>
2828
<description>This plugin creates Windows executables from Java jar files using the Launch4j utility.</description>
@@ -119,12 +119,29 @@
119119
<artifactId>maven-artifact-transfer</artifactId>
120120
<version>0.13.1</version>
121121
</dependency>
122+
<dependency>
123+
<groupId>org.apache.commons</groupId>
124+
<artifactId>commons-lang3</artifactId>
125+
<version>3.12.0</version>
126+
</dependency>
122127
<dependency>
123128
<groupId>junit</groupId>
124129
<artifactId>junit</artifactId>
125130
<version>4.13.2</version>
126131
<scope>test</scope>
127132
</dependency>
133+
<dependency>
134+
<groupId>pl.pragmatists</groupId>
135+
<artifactId>JUnitParams</artifactId>
136+
<version>1.1.1</version>
137+
<scope>test</scope>
138+
</dependency>
139+
<dependency>
140+
<groupId>org.mockito</groupId>
141+
<artifactId>mockito-core</artifactId>
142+
<version>4.11.0</version>
143+
<scope>test</scope>
144+
</dependency>
128145
<dependency>
129146
<groupId>org.apache.maven.plugin-testing</groupId>
130147
<artifactId>maven-plugin-testing-harness</artifactId>

src/main/java/com/akathist/maven/plugins/launch4j/Launch4jMojo.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ public class Launch4jMojo extends AbstractMojo {
174174
* like if java can't be found. If this is a console app and not a gui, then this value
175175
* is used to prefix any error messages, as in ${errTitle}: ${errorMessage}.
176176
*/
177-
@Parameter
177+
@Parameter(defaultValue = "${project.name}")
178178
private String errTitle;
179179

180180
/**
@@ -262,7 +262,7 @@ public class Launch4jMojo extends AbstractMojo {
262262
* Details about the classpath your application should have.
263263
* This is required if you are not wrapping a jar.
264264
*/
265-
@Parameter()
265+
@Parameter
266266
private ClassPath classPath;
267267

268268
/**
@@ -283,6 +283,12 @@ public class Launch4jMojo extends AbstractMojo {
283283
@Parameter
284284
private VersionInfo versionInfo;
285285

286+
/**
287+
* If set to true, it will prevent filling out the VersionInfo params with default values.
288+
*/
289+
@Parameter(defaultValue = "false")
290+
private boolean disableVersionInfoDefaults;
291+
286292
/**
287293
* Various messages you can display.
288294
*/
@@ -340,6 +346,17 @@ private void doExecute() throws MojoExecutionException {
340346
return;
341347
}
342348

349+
if (!disableVersionInfoDefaults) {
350+
try {
351+
if(versionInfo == null) {
352+
versionInfo = new VersionInfo();
353+
}
354+
versionInfo.tryFillOutByDefaults(project, outfile);
355+
} catch (RuntimeException exception) {
356+
throw new MojoExecutionException("Cannot fill out VersionInfo by defaults", exception);
357+
}
358+
}
359+
343360
final File workDir = setupBuildEnvironment();
344361
if (infile != null) {
345362
if (infile.exists()) {
@@ -834,6 +851,7 @@ public String toString() {
834851
", singleInstance=" + singleInstance +
835852
", splash=" + splash +
836853
", versionInfo=" + versionInfo +
854+
", disableVersionInfoDefaults=" + disableVersionInfoDefaults +
837855
", messages=" + messages +
838856
", manifest=" + manifest +
839857
", saveConfig=" + saveConfig +

src/main/java/com/akathist/maven/plugins/launch4j/VersionInfo.java

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,22 @@
1818
*/
1919
package com.akathist.maven.plugins.launch4j;
2020

21+
import com.akathist.maven.plugins.launch4j.generators.CopyrightGenerator;
22+
import com.akathist.maven.plugins.launch4j.generators.Launch4jFileVersionGenerator;
2123
import net.sf.launch4j.config.LanguageID;
24+
import org.apache.commons.lang3.StringUtils;
25+
import org.apache.maven.model.Organization;
2226
import org.apache.maven.plugins.annotations.Parameter;
27+
import org.apache.maven.project.MavenProject;
2328

29+
import java.io.File;
2430
import java.util.HashMap;
2531
import java.util.Map;
2632

2733
/**
2834
* Information that appears in the Windows Explorer.
2935
*/
3036
public class VersionInfo {
31-
3237
private static final Map<String, LanguageID> LANGUAGE_TO_LANGUAGE_ID;
3338

3439
static {
@@ -110,6 +115,27 @@ public class VersionInfo {
110115
@Parameter
111116
String trademarks;
112117

118+
public VersionInfo() {
119+
}
120+
121+
public VersionInfo(String fileVersion, String txtFileVersion, String fileDescription,
122+
String copyright, String productVersion, String txtProductVersion,
123+
String productName, String companyName, String internalName,
124+
String originalFilename, String language, String trademarks) {
125+
this.fileVersion = fileVersion;
126+
this.txtFileVersion = txtFileVersion;
127+
this.fileDescription = fileDescription;
128+
this.copyright = copyright;
129+
this.productVersion = productVersion;
130+
this.txtProductVersion = txtProductVersion;
131+
this.productName = productName;
132+
this.companyName = companyName;
133+
this.internalName = internalName;
134+
this.originalFilename = originalFilename;
135+
this.language = language;
136+
this.trademarks = trademarks;
137+
}
138+
113139
net.sf.launch4j.config.VersionInfo toL4j() {
114140
net.sf.launch4j.config.VersionInfo ret = new net.sf.launch4j.config.VersionInfo();
115141

@@ -137,6 +163,75 @@ private void setLanguage(net.sf.launch4j.config.VersionInfo ret) {
137163
ret.setLanguage(languageID);
138164
}
139165

166+
void tryFillOutByDefaults(MavenProject project, File outfile) {
167+
if (project == null) {
168+
throw new IllegalArgumentException("'project' is required, but it is null.");
169+
}
170+
if (outfile == null) {
171+
throw new IllegalArgumentException("'outfile' is required, but it is null.");
172+
}
173+
174+
String version = project.getVersion();
175+
Organization organization = project.getOrganization();
176+
177+
tryFillOutByDefaultVersionInL4jFormat(version);
178+
tryFillOutCopyrightByDefaults(project.getInceptionYear(), organization);
179+
tryFillOutOrganizationRelatedDefaults(organization);
180+
tryFillOutSimpleValuesByDefaults(
181+
version,
182+
project.getName(),
183+
project.getArtifactId(),
184+
project.getDescription()
185+
);
186+
187+
originalFilename = getDefaultWhenOriginalIsBlank(originalFilename, outfile.getName(), "originalFilename", "${project.version}");
188+
}
189+
190+
private void tryFillOutByDefaultVersionInL4jFormat(String version) {
191+
final String defaultFileVersion = Launch4jFileVersionGenerator.generate(version);
192+
fileVersion = getDefaultWhenOriginalIsBlank(fileVersion, defaultFileVersion, "fileVersion", "${project.version}");
193+
productVersion = getDefaultWhenOriginalIsBlank(productVersion, defaultFileVersion, "productVersion", "${project.version}");
194+
}
195+
196+
private void tryFillOutCopyrightByDefaults(String inceptionYear, Organization organization) {
197+
final String defaultCopyright = CopyrightGenerator.generate(inceptionYear, organization);
198+
copyright = getDefaultWhenOriginalIsBlank(copyright, defaultCopyright, "copyright", "${project.inceptionYear},${project.organization.name}");
199+
}
200+
201+
private void tryFillOutOrganizationRelatedDefaults(Organization organization) {
202+
if (organization != null) {
203+
companyName = getDefaultWhenOriginalIsBlank(companyName, organization.getName(), "companyName", "${project.organization.name}");
204+
trademarks = getDefaultWhenOriginalIsBlank(trademarks, organization.getName(), "trademarks", "${project.organization.name}");
205+
}
206+
}
207+
208+
private void tryFillOutSimpleValuesByDefaults(String version,
209+
String name,
210+
String artifactId,
211+
String description) {
212+
txtFileVersion = getDefaultWhenOriginalIsBlank(txtFileVersion, version, "txtFileVersion", "${project.version}");
213+
txtProductVersion = getDefaultWhenOriginalIsBlank(txtProductVersion, version, "txtProductVersion", "${project.version}");
214+
productName = getDefaultWhenOriginalIsBlank(productName, name, "productName", "${project.name}");
215+
internalName = getDefaultWhenOriginalIsBlank(internalName, artifactId, "internalName", "${project.artifactId}");
216+
fileDescription = getDefaultWhenOriginalIsBlank(fileDescription, description, "fileDescription", "${project.description}");
217+
}
218+
219+
private String getDefaultWhenOriginalIsBlank(final String originalValue,
220+
final String defaultValue,
221+
final String originalParameterName,
222+
final String defaultValueFormulaParams) {
223+
if (StringUtils.isBlank(originalValue)) {
224+
if(StringUtils.isNotBlank(defaultValue)) {
225+
return defaultValue;
226+
}
227+
228+
throw new IllegalStateException("Please fill the missing configuration values. " +
229+
"Error when trying to fulfill default value for VersionInfo parameter:'" + originalParameterName + "' with formula params:'" + defaultValueFormulaParams + "'.");
230+
}
231+
232+
return originalValue;
233+
}
234+
140235
@Override
141236
public String toString() {
142237
return "VersionInfo{" +
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.akathist.maven.plugins.launch4j.generators;
2+
3+
import org.apache.commons.lang3.StringUtils;
4+
import org.apache.maven.model.Organization;
5+
6+
import java.time.LocalDate;
7+
8+
public class CopyrightGenerator {
9+
private CopyrightGenerator() {
10+
}
11+
12+
/**
13+
* Parameters should be taken from MavenProject properties:
14+
* @param projectInceptionYear as ${project.inceptionYear}
15+
* @param projectOrganization as ${project.organization}
16+
*/
17+
public static String generate(String projectInceptionYear, Organization projectOrganization) {
18+
String inceptionYear = generateInceptionYear(projectInceptionYear);
19+
int buildYear = LocalDate.now().getYear();
20+
String organizationName = generateOrganizationName(projectOrganization);
21+
22+
return String.format("Copyright © %s%d%s. All rights reserved.", inceptionYear, buildYear, organizationName);
23+
}
24+
25+
private static String generateInceptionYear(String projectInceptionYear) {
26+
if(StringUtils.isNotBlank(projectInceptionYear)) {
27+
return projectInceptionYear + "-";
28+
}
29+
30+
return "";
31+
}
32+
33+
private static String generateOrganizationName(Organization projectOrganization) {
34+
if(projectOrganization != null && StringUtils.isNotBlank(projectOrganization.getName())) {
35+
return " " + projectOrganization.getName();
36+
}
37+
38+
return "";
39+
}
40+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.akathist.maven.plugins.launch4j.generators;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
import java.util.regex.Pattern;
6+
7+
public class Launch4jFileVersionGenerator {
8+
private static final int REQUIRED_NESTED_VERSION_LEVELS = 4;
9+
private static final String SIMPLE_PROJECT_VERSION_REGEX = "^((\\d(\\.)?)*\\d+)(-\\w+)?$";
10+
private static final Pattern simpleProjectVersionPattern = Pattern.compile(
11+
SIMPLE_PROJECT_VERSION_REGEX, Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE
12+
);
13+
14+
private Launch4jFileVersionGenerator() {
15+
}
16+
17+
/**
18+
* Converts projectVersion into a format "x.x.x.x" ('x' as a number), which is required by Launch4j.
19+
*
20+
* For shorter versions like "x.x.x" it will append zeros (to the 4th level) at the end like "x.x.x.0".
21+
* Every text flag like "-SNAPSHOT" or "-alpha" will be cut off.
22+
* Too many nested numbers (more than 4 levels) will be cut off as well: "1.2.3.4.5.6" -> "1.2.3.4".
23+
*
24+
* Param should be taken from MavenProject property:
25+
* @param projectVersion as ${project.version}
26+
*/
27+
public static String generate(String projectVersion) {
28+
if(projectVersion == null) {
29+
return null;
30+
}
31+
if(!simpleProjectVersionPattern.matcher(projectVersion).matches()) {
32+
throw new IllegalArgumentException("'project.version' is in invalid format. Regex pattern: " + SIMPLE_PROJECT_VERSION_REGEX);
33+
}
34+
35+
String versionLevels = removeTextFlags(projectVersion);
36+
String limitedVersionLevels = cutOffTooManyNestedLevels(versionLevels);
37+
38+
return appendMissingNestedLevelsByZeros(limitedVersionLevels);
39+
}
40+
41+
private static String removeTextFlags(String version) {
42+
if(version.contains("-")) {
43+
String[] parts = version.split("-");
44+
return parts[0];
45+
}
46+
47+
return version;
48+
}
49+
50+
private static String cutOffTooManyNestedLevels(String versionLevels) {
51+
String[] levels = versionLevels.split("\\.");
52+
53+
if(levels.length > REQUIRED_NESTED_VERSION_LEVELS) {
54+
List<String> limitedLevels = Arrays.asList(levels)
55+
.subList(0, REQUIRED_NESTED_VERSION_LEVELS);
56+
return String.join(".", limitedLevels);
57+
}
58+
59+
return versionLevels;
60+
}
61+
62+
private static String appendMissingNestedLevelsByZeros(String versionLevels) {
63+
String[] levels = versionLevels.split("\\.");
64+
65+
StringBuilder filledLevels = new StringBuilder(versionLevels);
66+
for (int i = levels.length; i < REQUIRED_NESTED_VERSION_LEVELS; i++) {
67+
filledLevels.append(".0");
68+
}
69+
70+
return filledLevels.toString();
71+
}
72+
}

0 commit comments

Comments
 (0)