Standard-version gitlab integration (java,pom.xml)

Published Categorised as posts Tagged , , , , ,

Standard-version is a utility using semver and generation powered by Conventional Commits.

In this tutorial I will explain how I use standard-version to generate, update pom.xml revision version and create a git tag and run it only when a MR is merged to master by a user and not a bot.

One the MR is merged to master then standard-version will do the following:
1. Retrieve the current version of your repository by looking at packageFiles, falling back to the last git tag.
2. bump the version in bumpFiles based on your commits.
3. Generates a CHANGELOG/md based on your commits (uses conventional-changelog under the hood).
4. Creates a new tag with the new version number.

My current Setup:

-Self hosted gitlab server
-Gitlab-runner as “shell” executor in a host with NodeJS and standard-version already installed.
npm i -g standard-version
-A Java Application repo containing the following files in the root:

.versionrc file to define the packageFiles to read the current revision version from pom properties-revision and bumpFiles filename and updater module to bump the version.

.versionrc file:

   "packageFiles": [
          "filename": "pom.xml",
          "type": "pom",
          "updater": "pomupdater.js"
   "bumpFiles": [
          "filename": "pom.xml",
          "type": "pom",
          "updater": "pomupdater.js"

pomupdater.js is the javascript file that is using a regex and read the revision version from properties.

pomupdater.js file:

function versionTagRegex (contents) {
    return RegExp(`^\\s+<properties>[\\s\\S]*?(\\s+<revision>([\\d\\.]+)<\/revision>)[\\s\\S]*?<\/properties>`, 'm') 
 module.exports.readVersion = function (contents) {
     const matches = versionTagRegex(contents).exec(contents)
     if (matches === null) {
         throw new Error('Failed to read the <revision> tag in your pom file - is it present?')
     return matches[2]

module.exports.writeVersion = function (contents, version) {
    //  Find the version tag, set the new version in it.
    return contents.replace(versionTagRegex(contents), (match) => {
        //  Replace the inner part of the version tag with the new version.
       return match.replace(/revision>[^,]+<\/revision/, `revision>${version}<\/revision`)

Sample pom.xml file:

  <name>First CI Friendly</name>

Create git gitlab-ci pipeline

gitlab-ci.yml file:

# If you use docker gitlab-runner standard-version docker image
#  image: detouched/standard-version

    # != 'BOT-000' part to avoid job stuck in a loop, to run only when a user merge to master and not the bot

  stage: build
    - runner-host-shell

    - rm -rf /tmp/repo
    - git config "BOT-000"
    - git config ""
    - git config --global ""
    - git config --global "BOT-000"
    - git clone "https://oauth2:${GITLAB_KEY2}@${CI_SERVER_HOST}/${CI_PROJECT_PATH}" /tmp/repo
    - cd /tmp/repo
    - standard-version
    - git push --follow-tags origin master 
    - master

Important this “!= ‘BOT-000” in the rule to avoid job stuck in loop.

pipeline output:

Running with gitlab-runner 14.7.0 (98daeee0)
  on runner-host-shell xxxxx
Preparing the "shell" executor
Using Shell executor...
Preparing environment
Running on xxxxxx...
Getting source from Git repository
Fetching changes with git depth set to 50...
Reinitialized existing Git repository in /home/gitlab-runner/builds/xxxxxxxx/0/cicd-repos/cicd_jobs/.git/
Checking out ddaa7c21 as master...
Skipping Git submodules setup
Executing "step_script" stage of the job script

$ rm -rf /tmp/repo
$ git config "BOT-000"
$ git config ""
$ git config --global ""
$ git config --global "BOT-000"
$ git clone "https://oauth2:${GITLAB_KEY2}@${CI_SERVER_HOST}/${CI_PROJECT_PATH}" /tmp/repo
Cloning into '/tmp/repo'...
warning: redirecting to
$ cd /tmp/repo
$ standard-version
✔ bumping version in pom.xml from 0.1.6 to 0.1.7
✔ outputting changes to
✔ committing pom.xml and
✔ tagging release v0.1.7
ℹ Run `git push --follow-tags origin master` to publish
$ git push --follow-tags origin master
warning: redirecting to
   ddaa7c2..2ba7e63  master -> master
 * [new tag]         v0.1.7 -> v0.1.7
Cleaning up project directory and file based variables
Job succeeded

Once the pipeline is complete will have an updated, pom file revision version and a new repo tag with the new version.

Leave a comment

Your email address will not be published.