Reputation: 11881
This question is a narrower version of the broader Automatically incrementing a build number in a Java project
Specifically I want Maven to generate versions of the format major.minor.sequence
where major.minor
are hard-coded in the pom.xml
but the sequence
number is obtained from the output of the command git rev-list HEAD --count
. I also want to append the string "-WIP" if the git status
does not contain the words nothing to commit, working tree clean
.
What is the easiest way to accomplish this?
Upvotes: 4
Views: 1937
Reputation: 97399
For such thing there is a solution in Maven 3.5.0 you can use ${sha1}
, ${changelist}
, ${revision}
for such purposes. Either a single one of them or you can combine them together.
<project>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache</groupId>
<artifactId>apache</artifactId>
<version>18</version>
</parent>
<groupId>org.apache.maven.ci</groupId>
<artifactId>ci-parent</artifactId>
<name>First CI Friendly</name>
<version>${revision}${sha1}${changelist}</version>
...
<properties>
<revision>1.3.1</revision>
<changelist>-SNAPSHOT</changelist>
<sha1/>
</properties>
</project>
By using the above you can simply build the application using:
mvn clean package
But also it's possible to do it like this:
mvn -Drevision=2.7.1 clean package
Based on your example you can use:
mvn -Dsha1=-XXXX clean package
where the XXXX
can be replaced with information extracted from Git (describe) or even better with informations from Jenkins (GIT_REVISION etc.).
Very important hint use the flatten-maven-plugin as described in the in the ci friendly documentation page.
Upvotes: 4
Reputation: 45649
Well, I would think if you really want it to work this way, you'd have to write a wrapper around your mvn
command that runs the appropriate git
commands, calculates the version string suffix, and passes it as a -Dversion.suffix=
option when calling the real mvn
executable.
(Then in your pom you'd set the version to something like 3.7.${version.suffix}
.)
You probably should be sure to avoid human-targetted interfaces (like the string "nothing to commit, working tree clean") if you can find equivalent interfaces that are intended for scripting. (For example, check out the output formats for the --porcelain
option in the git status
documentation: https://git-scm.com/docs/git-status)
That said, are you sure this is a good idea? There is no guarantee that the number of commits is a strictly-increasing value, and if you have more than one developer you could end up having different versions with the same version number. Maven's -SNAPSHOT
convention doesn't give you the specificity you're looking for, but exists more or less to acknowledge that the specificity you're looking for is usually more trouble than it's worth...
Upvotes: -1