Versions in Maven and Source
I’m a big fan of Maven, a build (and project) management tool. When you are working with Maven, each artifact that you develop (jar, or war file for example) has a version that’s distinct from version in your SCM system. The Maven Book has a good discussion about how versions are managed, but some there are often questions on projects about how to use Maven versions when you also have the SCM version. “SCM Version 6453” gives more information that “Artifact version 1.1” for example. yet we have 2 version numbers to manage. Here’s one approach that works well for simple projects.
I’m assuming that you know the basics of how to specify dependencies in Maven. If you need a quick intro to Maven, see the Maven Book.
If you are developing a project that has artifacts that external clients will incorporate with Maven, you need to change the artifact version with each release, as you specify the artifact version in the dependency element in your pom. If you are just using maven to develop components that will never show up in an external repository, adding the artifact version dimension to the SCM version can appear to add little value at the expense of some overhead, unless you have a clear model of how the two interact.
Many teams using maven are using it to manage external dependencies, but their own artifacts are not published to external maven repositories. A simple webapp, for example may have one war file, and have some external dependencies, and for support and validation purposes, you can use the build number or version number. There are a couple of ways to figure out the source version that you built your webapp from.
If you use a continuous integration tool like Bamboo, you can pass in the build number on the build command line by adding -DbuildNumber=${bamboo.buildNumber} to make the property buildNumber available to your project. You can make this available the application, so that you can see the build number on a login screen or about page. From the build number you can infer the SCM version. Or you can often ask the CI system the SCM version number. For exampe, in Bamboo, you can specify -DscmRevision=${custom.svn.revision.number} if you are using subversion as your SCM.
You can also use the build-number maven plugin to embed the version in your war manifest or make it available to your flex application, for example.
Regardless of how you get the revision number the revision number still gives you enough information to identify what code you are working with. The Maven convention to keep in mind is that SNAPSHOT artifacts are for active development, and non-SNAPSHOT versions are for released artifacts, so while you are working on a new projects, you’d be building a 1.0-SNAPSHOT artifact, and when you are ready to create a release branch, you would change it to 1.0.
So when should you change the project version in maven? Here is a simple approach: One maven major/minor version per codeline. Incremental versions stay on the same codeline.
So if you are using an active development line pattern (see Software Configuration Management Patterns) this means that each Release Line has a maven version, and your Mainline is use to create the SNAPSHOT version of the next release artifact.
For example:
-
Start mainline development with 1.0-SNAPSHOT version
-
When you have a release:
-
create the release branch and change the artifact to 1.0.
-
Change the mainline version to 1.1-SNAPSHOT (or whatever the next active version is)
To do support of the 1.0 release you have 3 choices:
- Just work with the 1.1 artifact version and use build numbers to distinguish between the current release and the support release.
- Branch off of the 1.1 codeline, change the version to 1.1.1-SNAPSHOT, and merge back when the 1.1.1 release goes out.
- Change the version on the support branch to 1.1.1-SNAPSHOT and don’t branch. Change the version to 1.1.1 when you are done, and create a tag for the release.
I prefer the third option as the simplest, and best option, assuming that there is only one stream of development work for the maintenance branch. It balances the need for identification, with the desire to minimize overhead.
To summarize:
-
New development for M.N-SNAPSHOT versions on trunk.Use SCM version & Build numbers to identify components internally.
-
M.N versions on their own branches.
-
Use M.N.n versions for maintenance releases; SNAPSHOTS for development, remove the SNAPSHOT when done.
I’m interested in hearing what others may have done to reconcile SCM and Maven artifact versions for internal components.