DEV Community

Maxime Guilbert
Maxime Guilbert

Posted on • Edited on

5 2

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!

AWS GenAI LIVE image

How is generative AI increasing efficiency?

Join AWS GenAI LIVE! to find out how gen AI is reshaping productivity, streamlining processes, and driving innovation.

Learn more

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay