Search code examples
mavencontinuous-integrationversioningmulti-module

How to built a multi-module Maven project with 4-digits of version string that includes CI build numbers?


I've been asked to do this, so please don't suggest that I shouldn't need to. :-p

We currently build a multi-module project with Maven. We have no problems doing so. We're using the Maven release plugin, and we get SNAPSHOT builds for development and release builds in Jenkins. The release plugin automagically increments the 3rd place of the version string. Life is good.

But, I've been asked to add a 4th place in our version strings, which is populated with the Jenkins build #.

The canonical way I see suggested to do this works fine with a single module: You define a property like build.number, to have a default value of "0-SNAPSHOT", and you define your POM to have a element value like "1.9.${build.number}". And, you set your Jenkins job to define build.number to be the Jenkins build #, for its invocation of Maven.

That would be great, if we had a single module, but we don't. We have multiple modules, and in Maven I can't either 1. not specify a version in the child module POMs, nor 2. use a property in the version of the child module POMs.

I gather it's a bad idea for Maven POMs to try to produce multiple artifacts in a single module (using. say, profiles), so I don't want to try to smoosh this project down to a single module.

I probably could try instead splitting it into separate projects, except that seems drastic, and besides, this project really is producing very tightly-related artifacts, so I want to be sure to build all the artifacts for any source code change in the project.

Any solutions?


Solution

  • The maven-release-plugin has two parameters named releaseVersion and developmentVersion. There you set the version to build and the next version, respectively.

    Using Jenkins, you can fill these variables with Jenkins generated content, using e.g. the build number. If you want to read the parts of the version number from the POM, you can use build-helper:parse-version and use terms like ${parsedVersion.majorVersion}.