Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing pom.xml file within a Jenkins pipeline

I am trying to parse my pom.xml in jenkins pipeline plugin. I intend to modify it and save it back.

My problem is that it gives me "unclassified field java.lang.String version"

My code is as follows:

@NonCPS
groovy.util.Node getPom(path) {
  new XmlParser().parseText(readFile(path)).version
}
node {
  groovy.util.Node pomNode = getPom("pom.xml")
  println pomNode
}

Similar issue has been discussed here: Parsing an XML file within a Jenkins pipeline

like image 750
Aarij Siddiqui Avatar asked Jul 22 '16 16:07

Aarij Siddiqui


People also ask

How do I use POM xml in Jenkins?

You have to mention the path of your application's pom. xml in relation to jenkins work space. Once Jenkins build starts it will download the source code to . jenkins\jobs\yourjobname\workspace and builds it there, you can check this location if you are not sure of the pom path.

Where is root pom in Jenkins?

You need to put in Jenkins the path to your Eclipse pom. xml. ./Users/enislavmollov/Documents/Maven workspace/MavenProkect/pom. xml or . \Users\enislavmollov\Documents\Maven workspace\MavenProkect\pom.

What is @NonCPS?

The @NonCPS annotation is useful when you have methods which use objects which aren't serializable. Normally, all objects that you create in your pipeline script must be serializable (the reason for this is that Jenkins must be able to serialize the state of the script so that it can be paused and stored on disk).


1 Answers

Why not use (you need pipeline utility steps plugin for this):

pom = readMavenPom file: 'pom.xml'

Now you have access to all data in the pom (as a Model).

like image 128
Krzysztof Krasoń Avatar answered Nov 02 '22 23:11

Krzysztof Krasoń