Devops Tutorial 33: Read pom xml file from Jenkinsfile
What you will Learn:
Read pom xml file from Jenkins pipeline
Read pom xml file from Jenkins pipeline
Create a new file ‘pom.xml’ in Github and commit it (you can look for any sample pom.xml from internet)
Create a new pipeline job and click ‘Pipeline syntax’. Select ‘readMavenPom’ from ‘Sample Step’ dropdown as shown below and generate pipeline script
Copy the above snippet.
Next create a stage to checkout from SCM (like we did in previous article)
Next, define a global variable ‘readxml’ (see line#1 below).
Also create a new stage (lines 10-13). In line#12, we are reading the xml file ‘pom.xml’ and storing its contents in the ‘readxml’ variable
Now let’s say we want to read the value of ‘name’ variable
In line#14, we are printing the value of ‘name’ property
Apply and Save.
Also Check: Selenium Online Training
Build Now, wait for the successful job completion
Open logs. Notice below that the value of ‘name’ property is successfully printed as ‘demo’
The value matches with Github
Similarly, we can read other properties as shown below
Apply and Save
Build Now, wait for the successful job completion
The same value ‘DummyProject’ is seen in Github
See below, we can also create a variable (line#14) to store the value of ‘name’ property. We can then use this variable in line#15
Apply and Save. Build now.
See below. Even now the correct value is printed.
Let us say you want to fetch the value of nested property, example ‘version’ property as shown below. The parent of this property is ‘parent’
See line#17 below. We can use readxml.parent.version to retrieve the value
Apply/Save/Build
Notice below that correct version is fetched and it matches with github
Below is the complete code:
def readxml;
node
{
stage('checking out from scm')
{
checkout([$class: 'GitSCM', branches: [[name: '*/main']], extensions: [], userRemoteConfigs: [[url: 'https://github.com/W2AGit/JenkinsPipelineAsACode.git']]])
}
stage('reading xml')
{
readxml = readMavenPom file: '';
def name_var = readxml.name;
echo "The value of name is: ${name_var}"
echo "The value of description is: ${readxml.description}"
echo "The version is: ${readxml.parent.version}"
}
}
This is how we can read the xml using pipeline.
Thank you for reading!
Also Check: Selenium Tutorials