4

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?

Community
  • 1
  • 1
Alex R
  • 11,364
  • 15
  • 100
  • 180
  • Are you aware of the command [git describe](https://git-scm.com/docs/git-describe)? Sounds like your use case might make use of it. – 1615903 May 19 '17 at 04:44

2 Answers2

4

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.

khmarbaise
  • 92,914
  • 28
  • 189
  • 235
-1

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...

Mark Adelsberger
  • 42,148
  • 4
  • 35
  • 52
  • This will produce warnings during your builds – khmarbaise May 19 '17 at 06:57
  • @khmarbaise - Yes, variable version numbers will create a warning. And as I noted, it isn't something I recommend. But it *is* the best way I see to do what was asked. If you can think of a better way, post an answer. – Mark Adelsberger May 19 '17 at 14:32