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
The reading part looks like all the other read functions in Jenkins (like readYaml, readJSON...)
Links
- Jenkins documentation : https://www.jenkins.io/doc/pipeline/steps/pipeline-utility-steps/#readmavenpom-read-a-maven-project-file
Write
def pom = readMavenPom file: 'pom.xml'
//Do some manipulation
pom.version = "x.x.x"
...
writeMavenPom model: pom
The write part is really simple and you just have to give back the pom object you retrieve from reading;
Links
- Jenkins documentation : https://www.jenkins.io/doc/pipeline/steps/pipeline-utility-steps/#writemavenpom-write-a-maven-project-file
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
I hope it will help you!
Top comments (0)