1

I have a multi-module maven project in Kubernates. We intend to version all these modules together. But as of now, I end up hard-coding version in each of the module pom.xml as below

<parent>
    <artifactId>xyz-application</artifactId>
    <groupId>com.xyz</groupId>
    <version>2.50.0.g</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.xyz</groupId>
<artifactId>xyz-Library</artifactId>
<version>2.50.0.g</version>

The main parent module has the below configuration

<modelVersion>4.0.0</modelVersion>
<groupId>com.xyz</groupId>
<artifactId>xyz-application</artifactId>
<version>2.50.0.g</version>
<packaging>pom</packaging>

Reference

Currently, I'm using the mvn release plugin in the pom.xml

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-release-plugin</artifactId>
    <version>2.5.3</version>
    <configuration>
        <autoVersionSubmodules>true</autoVersionSubmodules>
        <tagNameFormat>${product_label}-@{project.version}</tagNameFormat>
        <releaseProfiles>releases</releaseProfiles>
        <preparationGoals>clean deploy</preparationGoals>
        <arguments>-s settings.xml</arguments> 
    </configuration>
</plugin>

Since the project is in Kubernetes, here is the Jenkins file content I'm using while the release

def MVN_RELEASE = "release:clean release:prepare release:perform -Dmaven.source.skip=true"
stage('Release artifacts to artifactory') {
    steps {
        container('maven') {
            withCredentials() {
                sh "mvn ${MVN_RELEASE}  -Dresume=false"
            }
        }
    }
}

Now, during release, I need to update the parent pom and all sub-modules poms version by a new version of my choice, I can use a mvn version plugin, something like

  1. mvn versions:set -DnewVersion=3.00.0-SNAPSHOT -DprocessAllModules -DgenerateBackupPoms=false and then
  2. mvn versions:commit -DprocessAllModules

At what stage in Jenkins/pom.xml should I add this? Now, this plugin is useful only when I want to bump to a specific version of my choice (like 3.00.0-Snapshot), otherwise, the mvn release plugin should bump the version automatically to something like 2.50.1-SNAPSHOT. Is that correct understanding?

How can I gracefully handle this pom version update issue?

Thanks in advance!

P_user526
  • 65
  • 3
  • 11

1 Answers1

1

I do not see why you want the version plugin.

First of all, you don't need to add the <version> tag to the modules. You need to specify the version of the parent, but you can inherit your version (which is the same) from the parent.

Secondly, the maven release plugin has parameters releaseVersion and developmentVersion which allow you to set a release and future development version on the command line.

J Fabian Meier
  • 33,516
  • 10
  • 64
  • 142
  • While defining MVN_RELEASE, can I define it in this way - ```"def MVN_RELEASE = "releaseVersion=3.00.0 developmentVersion=3.00.1 release:clean release:prepare release:perform -Dmaven.source.skip=true"" ```? – P_user526 May 20 '20 at 15:50
  • Why `release:clean`? Why `3.00.0` instead of `3.0.0` ? Apart from that a development could be a `3.0.1-SNAPSHOT` ... – khmarbaise May 20 '20 at 15:52
  • What changes do I have to do - 1. if I want mvn release to automatically increment the version to 2.50.1-SNAPSHOT? 2. if I want mvn release to not increment the version and keep it 2.50.0, basically over-ride the existing 2.50.0 with new released code? – P_user526 May 20 '20 at 16:04
  • You cannot overwrite a version once it is released. – J Fabian Meier May 20 '20 at 16:38
  • if I want mvn release to automatically increment the version to 2.50.1-SNAPSHOT, should I leave releaseVersion and developmentVersion empty? ```"def MVN_RELEASE = "releaseVersion= developmentVersion= release:clean release:prepare release:perform -Dmaven.source.skip=true"``` – P_user526 May 20 '20 at 17:03
  • Then just set `releaseVersion` and don't touch `developmentVersion` at all. – J Fabian Meier May 20 '20 at 17:31
  • You're saying, if I want mvn to automatically increment to 2.50.1-Snapshot, then just leave the existing releaseVersion as it is? ```"def MVN_RELEASE = "releaseVersion=2.50.0-Snashot developmentVersion=3.00.1 release:clean release:prepare release:perform -Dmaven.source.skip=true"``` – P_user526 May 20 '20 at 17:36
  • I don't understand your command line at all. You cannot release something as snapshot and it doesn't make sense to set it to a release version afterwards. – J Fabian Meier May 20 '20 at 17:40
  • Hey @JFMeier, can you please suggest as ```def MVN_RELEASE=?``` how to handle the case when I don't want to update the release version myself, and let mvn increment it itself. – P_user526 May 20 '20 at 18:11
  • 1
    If you want to release as version `2.5.0` and then want to Maven to automatically increase it to `2.5.1-SNAPSHOT`, just set `-DreleaseVersion=2.5.0` on the command line. – J Fabian Meier May 20 '20 at 18:36
  • 1
    When I ran the ```"release:prepare release:perform -DreleaseVersion=2.0.1 -DdevelopmentVersion=2.0.2-SNAPSHOT -Dmaven.source.skip=true"```, 2.0.1 version was released. I ran ```"release:prepare release:perform -DreleaseVersion=2.0.1 -Dmaven.source.skip=true"``` again and got the error - ```hint: Updates were rejected because the remote contains work that you do not have locally. This is usually caused by another repository pushing to the same ref. You may want to first integrate the remote changes hint: (e.g., 'git pull ...') before pushing again.``` – P_user526 May 28 '20 at 17:18
  • If a version `2.0.1` was released, it cannot be released again. Use the next version. – J Fabian Meier May 28 '20 at 17:20
  • But in this case, I wanted mvn to automatically update the version (to 2.0.2 or something like that). That's why I used your suggestion to just set DreleaseVersion and not mention DdevelopmentVersion. – P_user526 May 28 '20 at 17:46
  • It does not work that way. If you just leave out both `releaseVersion` and `developmentVersion`, then the release is done from the SNAPSHOT version which is automatically increased. If you choose to set them from the command line, you need to care for these things yourself. – J Fabian Meier May 28 '20 at 17:57
  • Got it. Thanks. In the main post, you mentioned "you can inherit your version (which is the same) from the parent.". Currently, I have to mention the parent pom version in the child poms. Is there any way I can just mention the version in parent pom, and the child poms can inherit the version, without us manually updating the child pom versions every time? – P_user526 May 28 '20 at 18:11
  • Not in combination with the Maven release plugin. You need to specify the version of the parent. Then you can inherit your own version from there. – J Fabian Meier May 28 '20 at 18:22
  • I'm not sure I understood your point. Can you please elaborate? How are you suggesting the inheritance of parent pom versions in the child poms? – P_user526 May 28 '20 at 18:37
  • You don't need a `` tag in your child pom, but you need a version tag inside `` in your child pom. – J Fabian Meier May 28 '20 at 18:52
  • When I used the tag in parents' pom(where the version is mentioned) and inside parent in the child poms, I noticed that when we run the Maven release plugin, all versions (in parent and child) gets replaced by something like 1.0.0. So the cifriendly versions are gone right after one release. Until https://issues.apache.org/jira/browse/MRELEASE-935 is resolved, this remains a bug and we will have to manually update the pom versions. There is no workaround that, unless you have a suggestion. – P_user526 May 28 '20 at 19:06
  • So there is no alternative to automatically update child pom versions (or inherit version from parent), right? Either we update all versions (parent and child) manually and then do the release, or use releaseVersion tag from the command line for release. Is that correct? – P_user526 May 28 '20 at 19:32
  • Either you let the release plugin update your versions or you use `versions:set`. – J Fabian Meier May 28 '20 at 19:32
  • And to use "release plugin" to update the versions, we will have to manually update the versions in parent and all the child poms, or we use releaseVersion from command line. You suggested in the main post that we don't need to version plugin, which brings in ```versions:set```. – P_user526 May 28 '20 at 19:46
  • No, this is not correct. I do it the following way: I have e.g. version 1.0.0-SNAPSHOT in my project (everywhere). Then I do a release without specifying `releaseVersion` or `developmentVersion`. Then I get a release for `1.0.0` (everywhere) and all versions in all POMs are automatically updated to`1.0.1-SNAPSHOT`. – J Fabian Meier May 28 '20 at 19:52