incrementing maven version through jenkins declarative pipeline - maven

I'm trying to increment my pom version through Jenkins, but am facing many issues with the script element of the Jenkins declarative pipeline.
My goal is:
Jenkins pulls code from SCM
Run maven plugins
Increment version of application in the pom
Merge the new pom back into the SCM
I've managed to remove '-SNAPSHOT' from the version, and I've stored the version (e.g 1.0.0) in a variable within the script element of the pipeline. I am unable to then use that variable to be able tin increment it.
pipeline {
agent any
tools {
maven 'maven'
}
stages {
stage ('Git checkout branch') {
steps {
git branch: 'branch', credentialsId: '****', url: 'https://projectRepo'
}
}
stage ('Increment snapshot') {
steps {
dir('directory') {
//Remove snapshot from version in pom
sh 'mvn versions:set -DremoveSnapshot'
script {
//Get the version and assign to variable 'version'
version = '$(mvn help:evaluate -Dexpression=project.version -q -DforceStdout)'
}
//content of version can only be accessed by ${version}
sh "echo ${version}"
}
}
}
}
}
My aim is to get the version within the script tags, then perform a split like this .split("\."), then increment the last number, then, when I do a release, I'll use the maven flag to update the pom with the new version, plus add -SNAPSHOT to the end of the version

When you use the build-helper-maven-plugin, you can parse the version into its components:
https://www.mojohaus.org/build-helper-maven-plugin/parse-version-mojo.html
This offers not only elements like majorVersion and minorVersion, but also nextMajorVersion, nextMinorVersion etc.

Related

Jenkins pipeline mvn Package command not working

My test is working fine as Maven project. It fetches code from Git.
I wanted to execute the same in Pipeline so I wrote the below script for pipeline project.
pipeline {
agent any
stages {
stage('Getting the project from GIT') {
steps {
echo 'Pulling..';
git branch: 'main',
url: 'https://github.com/user/project.git';
}
}
stage('Cleaning the project') {
steps {
echo 'cleaning project ...'
sh 'mvn clean'
}
}
stage('Artifact Construction') {
steps {
echo "artificat contruction"
sh 'mvn package'
}
}
}
}
But when I execute , the third stage seems not to work.
Console output:
Maven configuration :
This is the version of Maven
I tried everything to solve this issue, can you guys help me? Is it a maven version problem?
You are running maven version - 3.0.5.
Maven resource plugin -3.2.0 is not compatible with this version of maven.
Upgrade maven installation to higher version. You can refer below plugin documentation page:
https://maven.apache.org/plugins-archives/maven-resources-plugin-3.2.0/plugin-info.html

ERROR: Could not find specified Maven installation 'maven_5_6_2'

I have a Jenkinsfile (see below) which runs a number of commands. I have the 'Pipeline Maven Integration' plugin installed in my Jenkins server (I have version 3_10_0 of Pipeline Maven Integration installed in Jenkins). But when I try to run my pipeline I get the error below in the console. Am I missing something? Is the error complaining about the Jenkins Maven plugin or the version of Maven in my Java solution?
Jenkinsfile:-
pipeline {
agent any
stages {
stage ('Compile Stage') {
steps {
withMaven(maven: 'maven_3_8_4') {
sh 'mvn clean install'
}
}
}
stage ('Test Stage') {
steps {
withMaven(maven: 'maven_3_8_4') {
sh 'mvn test'
}
}
}
stage ('Cucumber Reports') {
steps {
cucumber buildStatus: "UNSTABLE",
fileIncludePattern: "**/cucumber-report.json",
jsonReportDirectory: 'target'
}
}
}
}
Error in console:-
ERROR: Could not find specified Maven installation 'maven_3_10_0'.
Finished: FAILURE
Further information I have version 3.8.4 of Maven installed on the machine running the Jenkins server. I have also updated the Jenkins global tools config to reference version 3.8.4 of Maven, bit I still get the same error when running the pipeline.
I worked it out, the reference should have been as follows in the Jenkinsfile (as this is what is n my global tools):-

Increment Maven project.version using build-helper-plugin in Jenkins pipeline

You can use the maven-build-helper-plugin to parse the version and then use the maven-version-plugin to set new versions (see this blog):
clean build-helper:parse-version versions:set -DnewVersion=${parsedVersion.majorVersion}.${parsedVersion.minorVersion}.${parsedVersion.nextIncrementalVersion} versions:commit
This works fine when the command is executed as "maven goal"-prebuild-step in a Maven-job.
Now I'm trying to convert all my Maven-job to pipeline-jobs.
withMaven(
// Maven-Installation
maven: "${MavenHelper.MAVEN3D3D9}") {
String command = 'mvn build-helper:parse-version versions:set -DnewVersion=${parsedVersion.majorVersion}.${parsedVersion.minorVersion}.${parsedVersion.nextIncrementalVersion} versions:commit -f ' + komponente.getPomPath()
sh(command)
}
This always gives me a bad substitution error as the shell script tries to parse these variables. But in this context the variables are filled by the maven-build-helper-plugin during execution.
DnewVersion=${parsedVersion.majorVersion}.${parsedVersion.minorVersion}.${parsedVersion.nextIncrementalVersion}: bad substitution
I already tried to escape them via DnewVersion=\${parsedVersion.majorVersion}.... but still get the same error.
Any advice to get it working without incrementing it manually before passing it to the version-plugin.
The following is working:
pipeline {
agent any
tools {
maven 'Maven 3.6.0'
}
stages {
stage('Change Version') {
steps {
sh 'mvn build-helper:parse-version versions:set -DnewVersion=\\${parsedVersion.majorVersion}.\\${parsedVersion.minorVersion}.\\${parsedVersion.nextIncrementalVersion}'
sh "mvn build-helper:parse-version versions:set -DnewVersion=\\\${parsedVersion.majorVersion}.\\\${parsedVersion.minorVersion}.\\\${parsedVersion.nextIncrementalVersion}"
}
}
}
}

Gradle unable to resolve dynamic version of plugin from mavenLocal, how to get latest version?

I have created a Gradle plugin that creates some extra tasks and am publishing the plugin to MavenLocal with version 0.2.1. I can see the created jar in ~/.m2.
In another Gradle project, I am trying to pull in that plugin within the buildscript section of build.gradle, like this:
buildscript {
repositories {
mavenLocal()
}
dependencies {
classpath 'com.example:myplugin:0.2.+'
}
}
Running gradle tasks (or any other task, for that matter) causes Gradle to fail with the message:
> Plugin with id 'com.example.myplugin' not found.
However, if I change the version from 0.2.+ to 0.2.1 then it works. How do I get the latest version of the plugin into my project?
I just put what I did in here, and hope it helps you.
For example, I install the gradle-tomcat-plugin locally (2.4.1 and 2.4.2).
$ git checkout v2.4.2
$ ./gradlew publishToMavenLocal
$ git checkout v2.4.1
$ ./gradlew publishToMavenLocal
$ ls /Users/ruichen/.m2/repository/com/bmuschko/gradle-tomcat-plugin
2.4.1 maven-metadata-local.xml
2.4.2
And I added a separate task (showClasspath) to build.gradle to show me what is the version got applied:
task showClasspath {
doLast {
buildscript.configurations.classpath.each { println it.name }
}
}
The buildscript block is also pretty much like yours:
buildscript {
repositories {
mavenLocal()
}
dependencies {
classpath "com.bmuschko:gradle-tomcat-plugin:2.4.+"
}
}
The output is expected:
$ ./gradlew showClasspath
> Task :showClasspath
gradle-tomcat-plugin-2.4.2.jar

Gradle uploadArchives artifacts namespace when depending on a plugin

I have a Android app building with Gradle. I am using the gradle-release plugin to create automatic releases, and then the uploadArchives task from Gradle to upload the generated .apk to a Maven repo (Nexus).
I have to add the archives to upload dynamically at runtime, because my build is using custom Android flavors.
Everything works fine when I run the uploadArchives from the command line:
variant.outputs.each { output ->
def apkFile = output.outputFile
tasks."assemble${capitalizedVariantName}" << {
artifacts.archives [file: apkFile, classifier: variant.baseName]
}
}
uploadArchives {
repositories {
mavenDeployer {
pom.groupId = PROJECT_GROUP
pom.artifactId = PROJECT_NAME
}
}
}
Then I run:
./gradlew assembleFlavorNameRelease uploadArchives
And the .apk is correctly uploaded to Nexus.
I have the need to run the uploadArchives task BEFORE the release plugin automatically changes the version name of the project and commit.
Basically:
- current version: 0.1.0-SNAPSHOT
- run release
- version becomes: 0.1.0
- build (build task)
- upload this build to Nexus (uploadArchives task)
- update the version to: 0.1.1-SNAPSHOT (updateVersion task)
To achieve this, what I have done is to have the updateVersion task of the gradle-release plugin depending on the uploadArchives
updateVersion.dependsOn uploadArchives
Well, when I do this, the artifacts.archives. is empty, so no upload.
I suspect that, maybe, since I add the uploadArchives task as dependency of a task of the release plugin, then the "namespace" is different, so basically the uploadArchives task does not use the "same instance" of artifacts.archives, filled during the build.
updateVersion.dependsOn uploadArchives
If you do that then you end up calling uploadArchives in the same process as the release is done, but not in the same process the build is done. To get the version right for the build task itself, the release plugin spawns a new gradle build which runs the build with the correct version number. (This is done because a lot of other plugins like maven-publish are not able to pickup on a changed project version during runtime)
If you want to execute tasks in the same process as the build you need to either use the tasks beforeReleaseBuild or afterReleaseBuild to depend on. Both of them are run in the same process.
So in your case it would be
afterReleaseBuild.dependsOn uploadArchives
This runs the uploadArchives directly after the build has finished with the release version.
For a better understanding I adapted your taskgraph:
- current version: 0.1.0-SNAPSHOT
- run release
- version becomes: 0.1.0 (and is written to gradle.properties)
- spawn new gradle build
- build (build task)
- upload this build to Nexus (uploadArchives task)
- update the version to: 0.1.1-SNAPSHOT (updateVersion task)

Resources