mavenazure-devopsparent-pom

Maven parent and child pom auto-increment version using Azure devops


I have multiple microservices built on JHipster which used to be built separately using Maven. Now we have a parent pom that will have all the common dependencies for all the APIs that can be built multiple times, and deploy the same version multiple times of the parent pom. All the APIs will be using this as the parent pom. The catch is that we are using Azure DevOps for the build process, where the common artifacts are being sent to Azure Artifacts, and there the version is immutable. So if this parent pom is going to be built and packaged into Azure Artifacts, I need to figure out the below.

  1. How to auto-increment or not auto-increment the version of the parent pom based on the changes done. ( For this am trying to use maven-release-plugin and auto-increment the version, but if there are any best-practices please do let me know, am going with the default values as of now. Planning on using the same in the child pom as well, but not sure if this is the correct approach)
  2. How to ensure that the child projects using this parent pom will be able to auto-increment the version during release.

P.S: Earlier we had a problem where any change in a common repository, needed the developers to bump the version of the pom manually before the build, as Azure Artifacts would not allow the same version to be built and deployed to Artifacts.


Solution

  • You can try to generate a variable for the version of the parent pom and each child pom during the build pipeline run. For example: PARENT_PACKAGE_VERSION, CHILD_01_PACKAGE_VERSION, CHILD_02_PACKAGE_VERSION, ...

    The variables can be combined by some quantities and variables (include custom variables, predefined variables and some expressions). In this way, you can set up the version number almost as freely as possible.

    In the pom.xml file, you can fill the version field with the variable name.

    enter image description here

    In the pipeline, set up a shell script, using if conditions to determine whether change the value of the variable. Then replace variable name with the actual value in the pom.xml file.

    For example:

    sed -i 's/PACKAGE_VERSION/$(PACKAGE_VERSION)/g' pom.xml
    

    After above steps, build and publish the Maven package.

    For the version of the parent pom, to avoid the error caused by publishing same version number to the Azure Artifacts, you can set the version number with more node, for example, <MajorVersion>.<MinorVersion>.<BatchVersion>. The MajorVersion and MinorVersion may be not changed frequently. The BatchVersion can be changed or auto-increased when any updates occur in the parent package and/or child packages.