Maven-release plugin: Branch without snapshot in version - maven

I want to automate our branching process and for that i am using maven-release-plugin.
Following command is used for branching:
mvn --batch-mode release:branch -DupdateBranchVersions=true
-DupdateWorkingCopyVersions=$MoveWorkingCopyToNextVersion
-DautoVersionSubmodules=true -Darguments="-DskipTests"
-DreleaseVersion=$BranchVersion -DbranchName=$BranchVersion
-DscmCommentPrefix=$ReleaseJira:
-Dusername=$BuildUser -Dpassword=$BuildUserPassword
My problem is that with above command version of pom created in branch is "BranchVersion-SNAPSHOT" while i want pom version in branch should be "BranchVersion" i.e. it should not contain snapshot.
I know this is not standard approach but our current release process will not work if branch contains "SNAPSHOT".
Any suggestions?

If you don't get updateVersionsToSnapshot working could you use the versions plugin afterwards to set the version on the Pom
http://mojo.codehaus.org/versions-maven-plugin/set-mojo.html

Related

What is the supported way to change version on deploy for Artifactory Maven Jenkins plugin

I'm using the Maven jfrog plugin: https://jenkins.io/doc/pipeline/steps/artifactory/#rtmavendeployer-set-maven-deployer
I want to change the version of the artifact on deploy so it has the feature branch name in it.
I tried to run the set version goal. It succeeds but just totally ignores the version
rtMavenRun (
pom: "pom.xml",
goals: 'versions:set -DnewVersion=1.2.3-SNAPSHOT clean install',
resolverId: 'resolver-unique-id',
deployerId: 'deployer-unique-id'
)
The problem is the following: You call the goal versions:set and the phases clean and install in one Maven run. Before the first goal runs, Maven has already resolved the version for all goals/phases that are run. Therefore, you change the version in the POM, but as Maven has already read it, you will not be able to see it during this run.
What are possible solutions?
You can run versions:set first and then clean install in a separate Maven run.
You can use the property ${revision} in the definition of <version> and then set this property on the command line.

Jenkins & Github, how to append a version number?

We have 2 branches in github,
master
release
In our jenkins we have a job for each of these branches.
We want to increment a version number programmatically for release each time jenkins builds the release. We want also to increment the version number in the github release branch. Can you give me some directions on how to do it and what jenkin plugins i need? Thanks
This can be performed using a simple command:
mvn release:prepare release:perform --batch-mode
Explanation:
release:prepare
Prepare for a release in SCM. Steps through several phases to ensure the POM is ready to be released and then prepares SCM to eventually contain a tagged version of the release and a record in the local copy of the parameters used. It will update your version number from e.g. "1.0.1-SNAPSHOT" to "1.0.1" and commit it to the tag. Also working version will be incremented and updated to "-SNAPSHOT" again, e.g. "1.0.2-SNAPSHOT".
This can be followed by a call to release:perform. For more info see example
release:perform
Perform a release from SCM, either from a specified tag, or the tag representing the previous release in the working copy created by release:prepare. For more info see example
--batch-mode allows a non-interactive script executeion. For details please read about maven-release-plugin
Use the maven release plugin to handle incrementing the version number. At that point all you have to do is have the Jenkins build run maven with the release plugin goals.
Here is a good article on it.
http://www.vineetmanohar.com/2009/10/how-to-automate-project-versioning-and-release-with-maven/
With that all you have to do in Jenkins is have it run the following command on the checked out repository.
mvn release:prepare release:perform -B

Updating the versions in a Maven multi-module project

I have Maven multi-module project and I would like to update the development versions to a given value using a script. The aggregator POM is only an aggregator and the children do not inherit from it. This is important because the artifacts all inherit from other POM files. Here is my structure
aggregator/
--projectA
--projectB
Also, projectB has a Maven dependency on projectA.
First I tried:
mvn -DnewVersion=0.28-SNAPSHOT -DupdateMatchingVersions=true versions:set
It only updated the aggregator project's version.
If I run the Maven release process, it correctly updates projectB's dependency on projectA to use the new development version after the release build. Because the release process handles this well, I thought that using the release plugin might solve my issue.
So I tried the following:
mvn -DdevelopmentVersion=0.28-SNAPSHOT -DautoVersionSubmodules=true --batch-mode release:update-versions
This updated all of my sub-projects correctly. However it did not update projectB's dependency version for projectA.
What is a simple way to update all the development versions in my project, including projectB's dependency on projectA?
You may have more luck with the release plugin but it may require some tweaking
versions:set is designed to update the version of the pom that it executes against... ie the root of the reactor.
If you follow it's conventions, then it will work... But you need to know its conventions.
When you have /project/parent/version and /project/version both specified but "accidentally" at the same value, the versions plugin assumes that the two versions are just accidentally the same, and so does not update the child project's version when the parent version is being updated. updateMatchingVersions tells the plugin to assume that it us not an accident and that the child should be in lock step.
If you only specify /project/parent/version and leave the project version unspecified, therefore relying on inheritance, the plugin will add the child project to the list of version changes (and hence loop through all the projects again to ensure it catches any additional required changes)
The versions plugin does not currently provide an option to force everything to the one version... Though that might be a good idea.
You can get what you want with three commands, eg
mvn versions:set -DnewVersion=...
cd projectA
mvn versions:set -DnewVersion=...
cd ../projectB
mvn versions:set -DnewVersion=...
This is because versions:set will attempt to "grow" the reactor if the parent directory contains an aggregator pom that references the invoked project...
In other words when you have a reactor with no common parent, versions assumes that the common version number is by accident, but it will pick up the intent from the wider reactor
# for each module into aggregator pom
for module in $(grep "\<module\>" pom.xml | sed 's/<\/module>//g' | sed 's/.*<module>//g' | sed 's/.*\///g')
do
# set the version of the module
# and update reference to this module into others modules
mvn versions:set -DgenerateBackupPoms=false -DartifactId=$module \
-DnewVersion=$newVersion -DupdateMatchingVersions=true
done
# set the version of the aggregator pom
mvn versions:set versions:commit -DnewVersion=$newVersion
i found your same problem ,then i clone versions plugin code , then I found if you set gropuId,artifcatId,oldVersion value tobe * will solve the problem;
like this :
mvn versions:set -DnewVersion=xxx -DgroupId=* -DartifactId=* -DoldVersion=*

Maven: change version properties in pom.xml

I have a Maven pom.xml, I build project and release project deploy with Jenkins.
But before and after build "release version" we need set my version in
For example:
I have in pom.xml
<properties>
<version-own>0.0.21-SNAPSHOT</version-own>
</properties>
before release I need set like this
<properties>
<version-own>0.0.25</version-own>
</properties>
after release I need set like this
<properties>
<version-own>0.0.27-SNAPSHOT</version-own>
</properties>
How can this be done?
If you don't have to use your own version property, consider the following that will operate on your <project><version>0.0.21-SNAPSHOT</version></project> element:
mvn versions:set versions:commit -DnewVersion="0.0.25"
That will modify your pom and adjust the version to your liking. You'll likely want to commit this change to your source code control repository, for this the scm plugin's scm:checkin goal works just fine (assuming you want this to be automated):
mvn scm:checkin -Dincludes=pom.xml -Dmessage="Setting version, preping for release."
Then you can perform your release (I recommend the maven-release-plugin), after which you can set your new version and commit it as above.
The versions plugin is your friend. Scripting the above would likely involve some parameterized build, or preferably the groovy plugin for jenkins which allows you to get the maven-specific build variables.
For starters, you can do it by hand. If your build follows maven conventions well, you could probably leverage one of a couple of maven plugins that exist for helping with the management of version numbers.
The maven-versions-plugin helps automate manual changes to version numbers. It has nice CLI commands to help tune up your poms before doing releases.
Then there's the maven-release-plugin that automates the whole process of cutting a release. It will change your snapshot to a release version, then roll it to the next snapshot after the release build. During all this process it commits discrete versions of the poms to source control.
Again, the secret to seeing success in the more automated bits of the maven community is whether your build is doing things the maven way or not. Hacked, highly tweaked, non-conventional builds usually have a lot of barriers to successful use of the release plugin.
There is one way to to that easily. With one command you can change whichever part you want:
For cut and paste:
mvn build-helper:parse-version versions:set -DbuildNumber=555 '-DnewVersion=${parsedVersion.majorVersion}.${parsedVersion.minorVersion}.${parsedVersion.incrementalVersion}-${buildNumber}'
For clarity:
mvn build-helper:parse-version versions:set -DbuildNumber=555
'-DnewVersion=
${parsedVersion.majorVersion}
.${parsedVersion.minorVersion}
.${parsedVersion.incrementalVersion}
-${buildNumber}'
This is a concise example how to update versions in one go with build values
Build-helper plugin supports regex replacements, and can even parse version numbers if need be.
http://www.mojohaus.org/build-helper-maven-plugin/
There is something like parsedVersion.nextIncrementalVersion
mvn build-helper:parse-version versions:set -DnewVersion=\${parsedVersion.majorVersion}.\${parsedVersion.minorVersion}.\${parsedVersion.nextIncrementalVersion} versions:commit
Looking at this comment you are describing that you are using this version to provide a dependency. maven-release-plugin should help you manage the versions for you. So provide that plugin in your pom.xml.
And for the step of manually providing the release and development version, create a job in jenkins which will have 2 string parameters:
developmentVersion
releaseVersion
Add "Invoke top-level Maven targets" build step to execute the releasing (for example):
clean release:clean release:prepare release:perform -DdevelopmentVersion=${developmentVersion} -DreleaseVersion=${releaseVersion}
When building the job, it will prompt you to insert both the developmentVersion and releaseVersion.
Cheers,
Despot

Maven release:branch

I have a maven multi module project.
When the code is development complete, we would want to write a branch build job in Jenkins which branches the code, increment the pom versions in the trunk, and remove the
-SNAPSHOT from the pom versions in the branch.
So, if the trunk is 2.2-SNAPSHOT, by the end of the operation, the trunk should be 2.3-SNAPSHOT, and the newly created branch should have poms with 2.2.
When I use release:branch the trunk is getting updated but it's not removing the -SNAPSHOT from the poms in the branch.
Please let me know if you have any ideas of achieving this.
Thanks in advance.
From Maven Release plugin: Create a branch
By default, the POM in the new branch keeps the same version as the local working copy, and the local POM is incremented to the next revision. If you want to update versions in the new branch and not in the working copy, run:
mvn release:branch -DbranchName=my-branch -DupdateBranchVersions=true -DupdateWorkingCopyVersions=false
Note: This can be useful if you want to create a branch from a tag
As an alternative, you might want to try the Versions Maven plugin:
mvn versions:set versions:commit -DnewVersion=2.2.0
You would still need to create the branch by hand though...

Resources