Bianca Torres
Bianca Torres

Reputation: 21

Can Commitizen update pom.xml file?

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

Upvotes: 1

Views: 561

Answers (2)

MrAlucardDante
MrAlucardDante

Reputation: 1

There is an even simpler solution, that works for any language.

In your pom.xml, add a properties that stores the version number (for instance app.version)

<app.version>1.0.0</app.version>

Then in your commitizen configuration, add the version_files element and point to the newly added property in your pom:

version_files = [
    "pom.xml:app.version"
]

Commitizen documentation

Upvotes: 0

Norbert
Norbert

Reputation: 1462

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.

Upvotes: 0

Related Questions