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

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}"
}
}
}
}

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

how to run build in build.gradle script?

command line ./gradlew -P foo.
I want to complete the build.gradle script when the above command is executed, but I am at a loss because there is no documentation.
// build .gradle
if (hasProperty('foo')) {
// run build like ./gradlew clean build
}
how can run build in build.gradle script?

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):-

Jenkins Gradle build with wrapper, using artifactory plugin

Is there a way to do this?. Want to publish gradle .jar in Artifactory. I was able to do a maven build with installed maven version in jenkins an publishing successfully the war in artifactory, but now devs want to move to gradle build using gradle wrapper instead of maven.
Normal Gradle build works just fine using this code.
stage('Clean Build') {
withGradle { // using gradle wrapper
sh './gradlew clean build'
}
}
But when I try to implement this with Artifactory plugin I can´t. I´m getting.
[Pipeline] ArtifactoryGradleBuild
[api-build] $ /var/lib/jenkins/workspace/api-build/gradlew --init-script /var/lib/jenkins/workspace/api-build#tmp/artifactory/init-artifactory11329321758849387716gradle clean artifactoryPublish -b ./build.gradle
Starting a Gradle Daemon (subsequent builds will be faster)
FAILURE: Build failed with an exception.
* What went wrong:
Failed to create Jar file /var/lib/jenkins/.gradle/caches/jars-8/8535adf040d7c5fdb7fd6bc28bb0ef3f/ok.
> Prefix string "ok" too short: length must be at least 3
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 8s
this is the pipeline code:
node {
def server = Artifactory.server 'artifactory'
def rtGradle = Artifactory.newGradleBuild()
def buildInfo
jdk = tool name: 'JDK11'
//jdk = tool name: 'Java 8'
env.JAVA_HOME = "${jdk}"
stage('Get Code from Bitbucket') {
git branch: "${branch}",
credentialsId: 'jenkins',
url: 'ssh://git#bitbucket.org/project1/api1'
sh "chmod 755 gradlew"
}
stage('Artifactory Configuration') {
// Set Artifactory repositories for dependencies resolution and artifacts deployment.
rtGradle.deployer server: server, repo: 'myrepo'
rtGradle.useWrapper = true
}
stage('Gradle build') {
withGradle {
buildInfo = rtGradle.run rootDir: ".", buildFile: 'build.gradle', tasks: 'clean artifactoryPublish'
}
}
stage('Publish build info') {
server.publishBuildInfo buildInfo
}
}
Was anybody able to make this work? thank you in advance.
This issue is duplicates of this and there is also a PR fix for that which describes the root cause of the bug.
As a temporary workaround, use Gradle V6.5.1 or below.

incrementing maven version through jenkins declarative pipeline

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.

Resources