-1

i have an xml file in which i need to find the parent version and replace with a new value.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.test.proj</groupId>
        <artifactId>proj-core</artifactId>
        <version>1.1.0.0</version>
    </parent>
    <groupId>com.test.child</groupId>
    <artifactId>sub-module</artifactId>
    <version>2.20.0.0-SNAPSHOT</version>
    <packaging>pom</packaging>
    <name>test</name>
    <description>test suite</description>
</project>

in the above xml, I am able to change the module version using mvn command, but unable to change the parent version using script. Is there any ways to grep and replace the value?

I tried to change using the python script

from xml.etree.ElementTree import ElementTree
print ElementTree(file="pom.xml").findtext("{http://maven.apache.org/POM/4.0.0}parent/version")

By default is is taking the snapshot version only.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • what value you want to replace with ? – Soumendra Mishra Sep 23 '20 at 06:46
  • @SoumendraMishra i want to provide the input value through jenkins parameter For Example, if i trigger the jenkins build, it has to first grep and show the current parent version and has to replace with the new version that i am providing via Jenkins. – srbsreeram Sep 23 '20 at 07:02
  • `"{http://maven.apache.org/POM/4.0.0}version"` gets me the SNAPSHOT version. `"{http://maven.apache.org/POM/4.0.0}parent/version"` gets me nothing at all. Are you trying to print out both of these, and then replace them both? With the same value? – tripleee Sep 23 '20 at 07:41

2 Answers2

0

Your question is probably incomplete but at least here are the beginnings of an answer.

If you can use the stand-alone tool xmlstarlet, try this:

xmlstarlet sel -N m=http://maven.apache.or '//m:version' -n /tmp/pom.xml 

Output:

1.1.0.0
2.20.0.0-SNAPSHOT

Then to modify,

xmlstarlet ed -N m=http://maven.apache.org/POM/4.0.0 -u '//m:version' -v 1.2.3.4  /tmp/pom.xml

Output:

<?xml version="1.0"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.test.proj</groupId>
    <artifactId>proj-core</artifactId>
    <version>1.2.3.4</version>
  </parent>
  <groupId>com.test.child</groupId>
  <artifactId>sub-module</artifactId>
  <version>1.2.3.4</version>
  <packaging>pom</packaging>
  <name>test</name>
  <description>test suite</description>
</project>

Source / inspiration: https://unix.stackexchange.com/questions/398157/search-replace-in-xml-file-with-sed-or-awk and the XML Starlet manual

If you want to do this in Python then see e.g. Find and Replace Values in XML using Python (seriously think about migrating to Python 3 though!)

tripleee
  • 175,061
  • 34
  • 275
  • 318
0

You can set the parent version by using Maven itself:

mvn versions:update-parent -DparentVersion=1.2.3

J Fabian Meier
  • 33,516
  • 10
  • 64
  • 142