DEV Community

Maxime Guilbert
Maxime Guilbert

Posted on • Edited on

How to read and write a pom in Jenkins ?

When I started to work with Jenkins with Java apps, I quickly have these questions

How to be able to read a pom?
How to update it?
Am I able to update the pom version from Jenkins??

So today, we will see how to do it!

Read

pom = readMavenPom(file: 'pom.xml')

def pom_version = pom.version
Enter fullscreen mode Exit fullscreen mode

The reading part looks like all the other read functions in Jenkins (like readYaml, readJSON...)

Links


Write

def pom = readMavenPom file: 'pom.xml'

//Do some manipulation
pom.version = "x.x.x"
... 

writeMavenPom model: pom
Enter fullscreen mode Exit fullscreen mode

The write part is really simple and you just have to give back the pom object you retrieve from reading;

Links


Bonus - Update pom version

Once we know that, we can automatically change the version in the pom!

def pom = readMavenPom file: 'pom.xml'

pom_version_array = pom.version.split('\\.')

// You can choose any part of the version you want to update
pom_version_array[1] = "${pom_version_array[1]}".toInteger() + 1

pom.version = pom_version_array.join('.')

writeMavenPom model: pom

Enter fullscreen mode Exit fullscreen mode

I hope it will help you!

Top comments (0)