Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I update the version inside my pom.xml when releasing using git flow?

In maven projects the version of a project is contained in the <version> attritbute of the pom.xml file. When creating a new release in the git flow model I need to bumb the version number. This article explains how this is done (without maven):

  1. Create a release branch
  2. Change the version number and commit
  3. Merge the release branch both to develop and master

Additionally it says:

It is exactly at the start of a release branch that the upcoming release gets assigned a version number—not any earlier. Up until that moment, the develop branch reflected changes for the “next release”, but it is unclear whether that “next release” will eventually become 0.3 or 1.0, until the release branch is started. That decision is made on the start of the release branch and is carried out by the project’s rules on version number bumping.

I see two problems in conjunction with maven here:

  1. The version under development in maven would be [next version]-SNAPSHOT. So we cannot really postpone the decision which version is next up to the moment we create a release branch. Of course if we can change our mind later, but we already need to enter /some value/ here earlier.
  2. Before creating our release the version in the pom.xml was let's say 1.1-SNAPSHOT. Now we have changed that to simply 1.1 on the release branch and merged that to master. Fine. But we should also merge that branch back to develop and for that we need to adapt the version to e.g. 1.2-SNAPSHOT. And probably we should not have done that on the release branch because that commit should not be part of the release. Actually we probably should have made this change right after branching off develop because all future commits on develop will be for the next version.

When googling for the problem I found some articles about maven-plugins that can automate the process, which may be interesting, but this question is really on how the git graph should look like and where the version bump commits should be and not how I can automate this using a maven-plugin.

like image 843
yankee Avatar asked Mar 27 '15 13:03

yankee


2 Answers

For normal releases, just do the snapshot version bump after merging the release branch:

  1. Create the release branch off develop and remove the snapshot from the version
  2. Merge it into master
  3. Merge it into develop
  4. Change the version on develop to the next snapshot version
  5. Push both master and develop

As you push all the changes at the same time, the team will only see the snapshot version increase.

For hotfixes, this is not possible as you create it off the master branch. There is a workaround for this scenario, here's an example using the raw git commands.

Example: You have 1.0.0 on master and want to create a 1.0.1 hotfix version. Your develop is already at 1.1.0-SNAPSHOT.

  1. git checkout master
  2. git checkout -b hotfix/1.0.1
  3. Make your hotfix!
  4. mvn versions:set -DnewVersion=1.0.1
  5. git commit -a -m "hotfix release 1.0.1"
  6. git checkout master
  7. git merge hotfix/1.0.1 (easy, because we created the branch off master)
  8. git checkout develop
  9. mvn versions:set -DnewVersion=1.0.0
  10. git commit -a -m "workaround to avoid merge conflicts"
  11. git merge hotfix/1.0.1 (will work because of the commit before)
  12. mvn versions:set -DnewVersion=1.1.0-SNAPSHOT
  13. git commit -a -m "set version back to 1.1.0-snapshot"

Not very nice but it works. This solution is also used by jgitflow (a Maven plugin to support you with git flow).

A nice alternative is to not ever commit the version bumps in the pom.xml and to set it before your build is run on the CI server. Example CI Pipeline:

  1. Checkout the branch
  2. Derive release version from the branch name and enrich it with a build number or timestamp, e.g. for build 42 of release/1.0.1 it would be 1.0.1-42
  3. Set the version using mvn versions:set -DnewVersion=1.0.1-42
  4. Build the release and publish it

The version number will not be as pure, but you'll never have merge conflicts anymore and you can always track a version back to it's build.

like image 102
chris Avatar answered Sep 19 '22 08:09

chris


Keep in mind that Git was developed for the Linux kernel which has it's own version rules.

For Maven, you should create a release branch which gets the snapshot version of the next release. This change should be a single commit (i.e. just the change of the version number in pom.xml). When merging that, checkout master and use git merge --strategy=ours <release-branch>

--strategy=ours means: Make a merge by saying "everything in master has been correctly merged with the release branch"; no changes are being made to master. Afterwards, Git will treat the branches as merged (i.e. having no changes) despite the different version number in both branches.

To avoid all kinds of problems when building master with Maven, use an odd or very high version number which never changes like 99.DEV-SNAPSHOT.

When you make the release, strip the -SNAPSHOT from the version in the release branch and commit. Afterwards, you checkout master and merge once more with --strategy=ours.

Note: If you do this, you must not make any other changes on the release branch but changing the versions. Any other hotfixes will be lost! You can only cherry pick them.

like image 35
Aaron Digulla Avatar answered Sep 20 '22 08:09

Aaron Digulla