We have a workflow requirement that essentially mean that we need to have the artifact version of a module externally defined from the current branch in git.
I.e. if we are on the master branch in git, I need <version>master-...</version>
and if we are on the bugfixX branch, I need <version>bugfixX-....</version>
for the generated artifact for this pom.xml.
I have previously found that https://github.com/koraktor/mavanagaiata can provide the SHA-1 hash as a property and it appears from the documentation that it can also provide the branch, so perhaps if it could be run early enough in the process we could set the property and just put <version>${our.version}</version>
in the pom. If this is possible, I would very much like to see a working pom.xml (and reward a 500 point bounty for it too).
If not, I guess we are into either preprocessing or "git checkout" do extra magic with some of the hooks (which I have not yet tried, working code would be great too).
We have a top level pom, which can be run to generate a property file in ".." before building the modules where this functionality I'm asking about needs to go.
Any suggestions on how to solve this?
Indeed, Maven can't change the version of it's own project in one run with other goals. On top of it, as far as I know, Maven doesn't support arbitrary properties in the <version>
tag. Therefore, a separate execution is required to run a goal which will change the version of the POM. There are various plugins which can do it - for this case one might use the versions:set
goal from versions
plugin - http://mojo.codehaus.org/versions-maven-plugin/set-mojo.html
So, one might execute it as follows for example:
mvn versions:set -DgenerateBackupPoms=false -DnewVersion=$branch-SNAPSHOT
where the $branch
variable has to contain current Git branch name; it can be extracted with git rev-parse
, like this:
branch=$(git rev-parse --abbrev-ref HEAD)
But still, one needs to execute it somehow. You can do manually, but it is cumbersome. So, my guess is that indeed the most robust solution would be to approach this from Git side. That is - a Git hook. Here is the complete Git post-checkout
hook which will do the job (same code as above with some filtering to run the hook only when the branch is checked out, not the individual files only):
#!/bin/bash echo 'Will change the version in pom.xml files...' # check if the checkout was to checkout a branch if [ $3 != '1' ] then echo 'git checkout did not checkout a branch - quitting';exit fi # get current branch name branch=$(git rev-parse --abbrev-ref HEAD) version=$branch-SNAPSHOT # run maven versions plugin to set new version mvn versions:set -DgenerateBackupPoms=false -DnewVersion=$version echo 'Changed version in pom.xml files to $version'
Put this content to the file PROJECTDIR\.git\hooks\post-checkout
file. Note that the hook file should be executable to run it (chmod +x post-checkout
).
Few notes about versions
plugin - it is pretty flexible and supports many options and have few other goals which might be of help, depending on your project structure (do you use parent poms or not, do childs have their own versions or do they derive from parent, etc.). So, the hook above might be modified slightly to support you specific case by using other goals from versions
plugin or by specifying additional parameters.
Pros:
Cons:
UPDATE
Hereafter is the more complicated version of the hook, which will not only set the version to the branch name, but will also preserve the suffix of the old version. For example, provided old version master-1.0-SNAPSHOT
, switching to feature1
branch will change the version of the project to feature1-1.0-SNAPSHOT
. This bash script suffers from few problems (requires branch names without dash symbol (-
) in the name, and only takes the version of the root pom), but may give an idea of how the hook may be extended: given a mix of mvn and bash commands you can extract and update quite a lot of the information in the POM.
#!/bin/bash echo 'Will change the version in pom.xml files...' # check if the checkout was to checkout a branch if [ $3 != '1' ] then echo 'git checkout did not checkout a branch - quitting';exit fi # get current branch name branch=$(git rev-parse --abbrev-ref HEAD) # get current version of the top level pom current_version=$(mvn help:evaluate -Dexpression=project.version | grep -v '\[.*') # extract version suffix suffix=$(echo $current_version | cut -d \- -f 2) # build new version version=$branch-$suffix # run maven versions plugin to set new version mvn versions:set -DgenerateBackupPoms=false -DnewVersion=$version echo 'Changed version in pom.xml files to $version'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With