1

I am working on a Java project and I'm using commitizen to enforce semantic version in CI, when I run the command cz bump and cz changelog update the .cz.yaml and changelog files with the new version but is not updating the version in pom.xml. Is there a functionality in Commitizen to update this file too?

Additionally: the application is currently being built by Maven

  • I have no working solution so far, but at least a idea how this could work. There is a `pre_bump_hook` and in that script you can use different environment variables that provide the new version. And with `mvn versions:set` it should be possible to change the version in the pom.xml. I have the same problem, but not enough time so far to solve it :D – Norbert Mar 01 '23 at 23:18

1 Answers1

0

As I mentioned in my comment above, there is a way to solve this problem and after some testing I'd like to provide you a simple solution now.

I assume you have .cz.toml file in place and already configured and use maven as build system for your Java project.

First you should add a scripts folder to the maven root. And put 2 shell scripts in there. The first is prepare_java_version.sh and the content is:

#!/bin/bash
mvn versions:set -DnewVersion=$CZ_PRE_NEW_VERSION

The second file is commit_java_version.sh and in that file you put the content:

#!/bin/bash
mvn versions:commit

Now you have to edit the .cz.toml file and append these two sections:

pre_bump_hooks = [
   "scripts/prepare_java_version.sh"
]
post_bump_hooks = [
   "scripts/commit_java_version.sh"
]

And now, you are ready to go.

Explanation: Commitizen calls the pre_bump_hooks just before the tag is created, and the shell script modifies the pom.xml to the given version. The Version used in the shell script is an environment variable provided by commitizen.

After the commit and the tag are created the post_bump_hooks are called and this shell script clean up the maven repo again. The version backup file is removed for example.

Hint: If there is a problem, and you need to revert the version change in the pom.xml just call mvn versions:revert and you have the old version. But this only works before the commit is called.

Norbert
  • 1,391
  • 1
  • 8
  • 18