Indeed, Maven cannot change the version of its own project in one go with other goals. Also, as far as I know, Maven does not support arbitrary properties in the <version> . Therefore, to achieve the goal, a separate execution is required, which will change the version of POM. There are various plugins that can do this - for this case, you can use the versions:set target from the versions plugin - http://mojo.codehaus.org/versions-maven-plugin/set-mojo.html
So, you can execute it, for example, as follows:
mvn versions:set -DgenerateBackupPoms=false -DnewVersion=$branch-SNAPSHOT
where the $branch variable should contain the current Git branch name; it can be extracted using git rev-parse , for example:
branch=$(git rev-parse --abbrev-ref HEAD)
But still, you need to somehow fulfill it. You can do it manually, but it is cumbersome. So, I think the most reliable solution would be to get closer to this from Git. That is - a Git hook. Here is the full Git post-checkout hook that will do the job (the same code as above, with some filtering to trigger the hook only when the branch is checked, not just individual files):
#!/bin/bash echo 'Will change the version in pom.xml files...'
Put this content in the file PROJECTDIR\.git\hooks\post-checkout . Note that the hook file must be executable to run it ( chmod +x post-checkout ).
A few notes about the versions plugin - it is quite flexible and supports many parameters and has several other goals that can be useful, depending on the structure of your project (whether you use parent pumps or not, the children have their own versions or they come from the parent and etc.). Thus, the above hook can be slightly modified to support a specific case, using other goals from the versions plugin or specifying additional parameters.
Pros:
- Robust
- There is no need to change anything in the pom.xml files to make this work.
- This "functionality" can be disabled simply by deactivating the hook (delete or make it not executable) - again, no changes are required in pom.xml
Minuses:
- You cannot force others to use the hook - it must be installed manually after cloning the repo (or you can provide a script to set the hook if it is assumed that Git users are afraid to touch the material inside .git).
UPDATE
In the future, this is a more complex version of the hook, which will not only install the version in the name of the branch, but also retain the suffix of the old version. For example, with the old version of master-1.0-SNAPSHOT switching to the feature1 branch will change the project version to feature1-1.0-SNAPSHOT . This bash script suffers from several problems (it requires a branch name without a dash ( - ) character in the name and accepts only the version of the root pump), but it can give an idea of how to increase the hook: given the combination of the mvn and bash commands, you can extract and update quite a lot of information in POM.
#!/bin/bash echo 'Will change the version in pom.xml files...'