Skip to content

Commit be78a62

Browse files
committed
Add version substitution support
1 parent ce1c127 commit be78a62

File tree

4 files changed

+149
-9
lines changed

4 files changed

+149
-9
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>de.dentrassi.maven.rpm.test</groupId>
7+
<artifactId>test21-modifyversion</artifactId>
8+
<version>1.0.1.redhat-00001</version>
9+
<packaging>jar</packaging>
10+
11+
<name>Test Package #20</name>
12+
<description>Test modify version generation</description>
13+
14+
<url>https://dentrassi.de</url>
15+
16+
<organization>
17+
<name>Jens Reimann</name>
18+
<url>http://dentrassi.de</url>
19+
</organization>
20+
21+
<licenses>
22+
<license>
23+
<name>Eclipse Public License - v 1.0</name>
24+
<distribution>repo</distribution>
25+
<url>https://www.eclipse.org/legal/epl-v10.html</url>
26+
</license>
27+
</licenses>
28+
29+
<properties>
30+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
31+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
32+
<skipSigning>true</skipSigning>
33+
</properties>
34+
35+
<build>
36+
37+
<plugins>
38+
<plugin>
39+
<groupId>de.dentrassi.maven</groupId>
40+
<artifactId>rpm</artifactId>
41+
<version>@project.version@</version>
42+
<executions>
43+
<execution>
44+
<goals>
45+
<goal>rpm</goal>
46+
</goals>
47+
<configuration>
48+
<attach>false</attach>
49+
<group>Application/Misc</group>
50+
51+
<modifyVersion>true</modifyVersion>
52+
<versionReplacements>
53+
<versionReplacement>
54+
<search>-</search>
55+
<replace>_</replace>
56+
</versionReplacement>
57+
</versionReplacements>
58+
59+
<signature>
60+
<keyId>${keyId}</keyId>
61+
<keyringFile>${user.home}/.gnupg/secring.gpg</keyringFile>
62+
<passphrase>${passphrase}</passphrase>
63+
<hashAlgorithm>SHA1</hashAlgorithm>
64+
<skip>${skipSigning}</skip>
65+
</signature>
66+
67+
</configuration>
68+
</execution>
69+
</executions>
70+
</plugin>
71+
</plugins>
72+
</build>
73+
74+
<profiles>
75+
<profile>
76+
<id>sign</id>
77+
<activation>
78+
<activeByDefault>false</activeByDefault>
79+
</activation>
80+
<properties>
81+
<skipSigning>false</skipSigning>
82+
</properties>
83+
</profile>
84+
</profiles>
85+
86+
</project>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
def log () {
3+
Process proc = ('rpm -qip ' + basedir + "/target/test21-modifyversion-1.0.1.redhat-00001-1.noarch.rpm").execute();
4+
return proc.in.getText().trim();
5+
}
6+
7+
String text = log()
8+
return text.contains("Version : 1.0.1.redhat_00001") &&
9+
text.contains("Source RPM : test21-modifyversion-1.0.1.redhat_00001-1.src.rpm")

src/main/java/de/dentrassi/rpm/builder/RpmMojo.java

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ public void setGenerateDefaultSourcePackage(final boolean generateDefaultSourceP
309309
/**
310310
* Whether the plugin should try to evaluate to hostname
311311
* <p>
312-
* If set to {@code false}, then he build hostname {@code localhost} will be
312+
* If set to {@code false}, then the build hostname {@code localhost} will be
313313
* used instead of the actual hostname
314314
* </p>
315315
*/
@@ -793,6 +793,25 @@ public void setSignatureConfiguration(final String signatureConfiguration) {
793793
@Parameter(defaultValue = "SHA-256", property = "rpm.fileDigestAlgorithm")
794794
String fileDigestAlgorithm;
795795

796+
797+
/**
798+
* Whether the plugin should modify internal RPM version for compliance.
799+
* <p>
800+
* If set to {@code true}, then any replacements specified in the {@code versionReplacements}
801+
* property will be applied.
802+
*/
803+
@Parameter(property = "rpm.modifyVersion", defaultValue = "false")
804+
boolean modifyVersion = false;
805+
806+
/**
807+
* A list of potential version substitutions to perform on the RPM internal version. This will
808+
* not affect the filename; just the version inside the generated RPM. This allows a user
809+
* to customise the version if the Maven version does not match RPM requirements e.g. it contains
810+
* a '-' (which would need to be replaced by an '_'). The 'search' string is a regex.
811+
*/
812+
@Parameter(property = "rpm.versionReplacements")
813+
List<VersionSubstitution> versionReplacements = new ArrayList<>();
814+
796815
private Instant outputTimestampInstant;
797816

798817
@Component(role = SignatureConfiguration.class)
@@ -845,7 +864,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
845864
this.logger.debug("Default ruleset: %s", this.defaultRuleset);
846865

847866
final String packageName = makePackageName();
848-
final RpmVersion version = makeVersion();
867+
final RpmVersion version = makeVersion(modifyVersion);
849868

850869
this.logger.info("RPM base information - name: %s, version: %s, arch: %s", packageName, version, this.architecture);
851870

@@ -951,9 +970,9 @@ private String makeTargetFilename() {
951970

952971
if (outputFileName == null || outputFileName.isEmpty()) {
953972
if (this.naming.getDefaultFormat() == Naming.DefaultFormat.LEGACY) {
954-
outputFileName = RpmFileNameProvider.LEGACY_FILENAME_PROVIDER.getRpmFileName(makePackageName(), makeVersion(), this.architecture);
973+
outputFileName = RpmFileNameProvider.LEGACY_FILENAME_PROVIDER.getRpmFileName(makePackageName(), makeVersion(false), this.architecture);
955974
} else {
956-
outputFileName = RpmFileNameProvider.DEFAULT_FILENAME_PROVIDER.getRpmFileName(makePackageName(), makeVersion(), this.architecture);
975+
outputFileName = RpmFileNameProvider.DEFAULT_FILENAME_PROVIDER.getRpmFileName(makePackageName(), makeVersion(false), this.architecture);
957976
}
958977
this.logger.debug("Using generated file name - %s", outputFileName, outputFileName);
959978
}
@@ -1312,7 +1331,7 @@ private String makePackageName() {
13121331
}
13131332
}
13141333

1315-
private RpmVersion makeVersion() {
1334+
private RpmVersion makeVersion(boolean modifyVersion) {
13161335
if (!this.forceRelease && isSnapshotVersion()) {
13171336
if (this.snapshotVersion != null && !this.snapshotVersion.isEmpty()) {
13181337
this.logger.info("Building with SNAPSHOT version from <snapshotVersion> parameter: %s", this.snapshotVersion);
@@ -1323,7 +1342,14 @@ private RpmVersion makeVersion() {
13231342
this.logger.info("Building with SNAPSHOT version from project: %s", baseVersion);
13241343
return new RpmVersion(this.epoch, baseVersion, makeSnapshotReleaseString());
13251344
}
1326-
return new RpmVersion(this.epoch, this.version, this.release);
1345+
String version = this.version;
1346+
if (modifyVersion) {
1347+
for (VersionSubstitution versionSubstitution : versionReplacements) {
1348+
logger.info("For version %s looking for %s to replace with %s", version, versionSubstitution.search, versionSubstitution.replace);
1349+
version = version.replaceAll(versionSubstitution.search, versionSubstitution.replace);
1350+
}
1351+
}
1352+
return new RpmVersion(this.epoch, version, this.release);
13271353
}
13281354

13291355
private boolean isSnapshotVersion() {
@@ -1369,7 +1395,7 @@ protected void fillPackageInformation(final RpmBuilder builder) {
13691395
}
13701396

13711397
private String generateDefaultSourcePackageName() {
1372-
return RpmLead.toLeadName(makePackageName(), makeVersion()) + ".src.rpm";
1398+
return RpmLead.toLeadName(makePackageName(), makeVersion(modifyVersion)) + ".src.rpm";
13731399
}
13741400

13751401
private String makeVendor() {
@@ -1452,13 +1478,16 @@ private static void ifSet(final Consumer<String> setter, final String value, fin
14521478
}
14531479
}
14541480

1455-
14561481
public static class Changelog extends RpmInformation.Changelog {
1457-
14581482
private String date;
14591483

14601484
public String getDate() {
14611485
return date;
14621486
}
14631487
}
1488+
1489+
public static class VersionSubstitution {
1490+
private String search;
1491+
private String replace;
1492+
}
14641493
}

src/site/markdown/index.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,22 @@ of `1.0.0-SNAPSHOT` will result in `1.0.0-0.201604250101` (depending on the actu
3838
The result of these rules are SNAPSHOT releases which is always lower than the final release.
3939
Unless you override using `forceRelease` or `snapshotBuildId`.
4040

41+
### Version Modification
42+
43+
Beyond the above version specification, there is a facility to modify the version _within_ the RPM. This does not affect the file name of the produced RPM. This is useful if the Maven version is non-compliant with RPM versioning. For example if the Maven version is 1.1.rebuild-00001, the `-` could be replaced by `_` (See https://fedoraproject.org/wiki/PackagingDrafts/TildeVersioning#Basic_versioning_rules)
44+
45+
~~~xml
46+
<modifyVersion>true</modifyVersion>
47+
<versionReplacements>
48+
<versionReplacement>
49+
<search>-</search>
50+
<replace>_</replace>
51+
</versionReplacement>
52+
</versionReplacements>
53+
~~~
54+
55+
Multiple version replacements may be specified and they will be applied in sequence.
56+
4157
## Contributing
4258

4359
All contributions are welcome!

0 commit comments

Comments
 (0)