简体   繁体   中英

Maven artifact version number with custom variable

I have in my pom this section:

<groupId>com.sample.app</groupId>
<artifactId>simpleapp</artifactId>
<version>1.0.9-${buildNumber}-SNAPSHOT</version>
<packaging>war</packaging>

Is there any nice and "maven way" to keep this variable name ${buildNumber} in that place in pom as this is now? I mean - when I am performing

mvn deploy:prepare deploy:release  -DbuildNumber=${BUILD_NUMBER}

this version section in pom.xml is updated to (when BUILD_NUMBER eq: 12):

<version>1.0.9-12-SNAPSHOT</version>

which almost is ok but this is also commited to repository. I like the fact that this tag: 1.0.9-12 in git repo is created, but I prefer to keep my original format of version in pom.xml file:

<version>1.0.9-${buildNumber}-SNAPSHOT</version>

This is because this stupid approach I have in my company to add to artefact version also build number from CI tool:(

Can someone give me some hint how to handle this?

The Maven Release Plugin has a parameter called developmentVersion that allows you to set the new version that is committed to your git branch after the release is done.

If you want to tell the Release Plugin to reuse the parts of version you used before, the Build Helper Maven Plugin helps you to parse the version. It creates properties like majorVersion and minorVersion from which you can construct the target version you like.

thanks JF Meier - I think that this will solve "my problem":

mvn --batch-mode \
-DdevelopmentVersion=1.6.6-\${buildNumber}-SNAPSHOT \
-DreleaseVersion=1.6.6-18 \
release:clean \
release:prepare \
release:perform

I don't know how to use this plugin: build-helper:parse-version to obtain major,minor and incremental number but I will use some Jenkins plugin to extract those values from pom.xml. Then I can set it dynamically, perform whole operation automatically (build + git tag + upload to nexus) and as a result my pom.xml will be updated with ${buildNumber} variable!

Thanks!

UPDATE:

I've added dependency for this helper plugin and final result is like this:

mvn --batch-mode \
build-helper:parse-version \
-DdevelopmentVersion=\${parsedVersion.majorVersion}.\${parsedVersion.minorVersion}.\${parsedVersion.nextIncrementalVersion}-\${parsedVersion.qualifier} \
-DreleaseVersion=\${parsedVersion.majorVersion}.\${parsedVersion.minorVersion}.\${parsedVersion.incrementalVersion}-31 \
release:clean \
release:prepare \
release:perform

I removed this ${buildNumber} as it is not required - Build Helper Plugin helped me a lot - I do not need any additional step in CI plan. Now I have autoincrement and during release I am able to add build plan ID.

For me case solved:-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM