Reputation: 1427
I'm working on a multi module maven project with Jenkins. I have a Build-Job which I want to package my project with a unique version number and deploy it to Nexus.
The version number should be - where the "maven-version" is the version maintained in the root POM and "build-number" is Jenkins' job build number. For example: 1.2.3-1234
I'm using the maven-versions-plugin to set the desired version number but I have the problem that I want to have the first part of the version (1.2.3) maintained in the POM. So I need a way to extrakt the version from the POM into a Jenkins environment variable.
Until yet I found no direct way of doing this. My solution is to use a groovy script which pares the POM and writes the version number into a temporary property file. After that I use the EnvInject plugin to create the environment varaible for later reinjecting as version number for the "mvn versions:set" command.
My groovy script:
import jenkins.util.*;
def project = new XmlSlurper().parse(new File("pom.xml"))
def version = project.version.toString();
def mainversion = version.substring(0, version.indexOf("-SNAPSHOT"));
println "Version: $mainversion";
def versionFile = new File("v.properties");
versionFile << "VERSION=$mainversion";
This indirection through the property file is very ugly and error prone. Is there any way to directly create an environment variable within the groovy script? It is possible using a system groovy script but these kind of scripts are always executed on the master. So my job will not be runable on slaves which doesn't work for me because I do not execute builds on the master.
Upvotes: 3
Views: 1578
Reputation: 2254
Welcome to another version of the Jenkins Chain upstream-downstream variable problem. I assume you are probably trying to do some sort of continuous integration pipeline.
At my company, we solved this one of two ways:
Option 1: Brute force by doing a sed search and replace as a execute shell in the beginning:
#sed -i '0,/<version>.*<\/version>/s/<version>.*<\/version>/<version>'${SVN_REVISION}'<\/version>/g' pom.xml;
#sed -i '0,/<name>.*<\/name>/s/<name>.*<\/name>/<name>'"${JOB_NAME}"'<\/name>/g' pom.xml;
#find . -name pom.xml | xargs sed -i 's/${build.number}/'${SVN_REVISION}'/g';
Option 2: Use Artifactory instead of Nexus and pay for Artifactory Pro. The Build/Release Management functions of the Artifactory jenkins plugin have saved us hours and hours of this kind of grief.
Upvotes: 1