Maven auto increment release version

Ganga picture Ganga · Nov 6, 2014 · Viewed 7.8k times · Source

I am working on an initiative to auto increment the X.Y.Z version during the build process. In this case we are not removing the SNAPSHOT suffix (to make it a released version), rather increasing the minor part of the version:

  • X.Y.Z-SNAPSHOT => X.Y.Z+1-SNAPSHOT
  • NOT X.Y.Z-SNAPSHOT => X.Y.Z

I cannot use the maven release plugin since it is ONLY capable of cutting off SNAPSHOT suffix to make it released versions. So, I ended up creating a custom script which has the logic to increment versions.

My question here is how can I best implement the following steps:

  1. Check out the pom.xml from perforce.
  2. Run the custom script to increment version.
  3. Commit the pom.xml change if deployment was successful.

I created a maven profile autoversion which partially meets the above needs. Maven first increments the version as part of generate resources phase. However, it ends up deploying the older version of the project.

mvn -Pautoversion clean deploy

         <profile>
        <id>autoversion</id>
        <build>
            <plugins>
                                    <plugin>
                                      <artifactId>exec-maven-plugin</artifactId>
                                      <groupId>org.codehaus.mojo</groupId>
                                      <executions>
                                            <execution>
                                              <id>Calculate version</id>
                                              <phase>generate-resources</phase>
                                              <goals>
                                                    <goal>exec</goal>
                                              </goals>
                                              <configuration>
                                                    <executable>${basedir}/autoincrementversion.sh</executable>
                                                    <arguments>
                                                            <argument>-bdj</argument>
                                                    </arguments>
                                              </configuration>
                                            </execution>
                                      </executions>
                                    </plugin>
                                    <plugin>
                                            <groupId>org.apache.maven.plugins</groupId>
                                            <artifactId>maven-scm-plugin</artifactId>
                                            <version>1.9</version>
                                            <executions>
                                                    <execution>
                                                            <phase>generate-resources</phase>
                                                            <goals>
                                                                    <goal>edit</goal>
                                                                    <goal>checkin</goal>
                                                            </goals>
                                                            <configuration>
                                                                    <username>jenkins</username>
                                                                    <basedir>./</basedir>
                                                                    <includes>pom.xml</includes>
                                                                    <message>Auto increment pom version</message>
                                                            </configuration>
                                                    </execution>
                                            </executions>
                                    </plugin>
            </plugins>
        </build>
    </profile>


[INFO] ------------------------------------------------------------------------
[INFO] Building project 99.22.8
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- versions-maven-plugin:1.3.1:set (default-cli) @ project ---
[INFO] Searching for local aggregator root...
[INFO] Local aggregation root: /opt/project/auto-increment-release
[INFO] Processing com.project
[INFO]     Updating project com.project
[INFO]         from version 99.22.8 to 99.22.9
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.135s
[INFO] Finished at: Wed Nov 05 23:15:13 PST 2014
[INFO] Final Memory: 11M/152M
[INFO] ------------------------------------------------------------------------

Any pointers to achieve the requirement is highly appreciated.

Answer

uvsmtid picture uvsmtid · Jul 22, 2015

We are considering to actually use Maven Release Plugin (MRP) in similar situation changing ALL version numbers to something like X.Y.Z.N:

  • X.Y.Z is semantic part which changes rarely during proper human-guided release
  • N is incremental part (meaningless except sequence ordering) which MRP is able to increment automatically

Yes, MRP will create two additional commits (with released version and next SNAPSHOT version), but... If your developers make snapshot builds they need X.Y.Z.N-SNAPSHOT versions. And if you want incremental release, you also need X.Y.Z.N too. Hence, the same two commits.

All you may avoid optionally is X.Y.Z.N tagging waiting for X.Y.Z only.