I have some pom files in my project with the following structure
<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/maven-v4_0_0.xsd">
<parent>
<artifactId>xparent</artifactId>
<groupId>y</groupId>
<version>2.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>someparent</artifactId>
<version>x.x.x-needs_change</version>
<packaging>pom</packaging>
<name>some name</name>
<description>some description</description>
<url>myurl</url>
<modules>
<module>mymodules</module>
</modules>
<properties>
<my.version>x.x.x-needs_change</my.version>
</properties>
<dependencies>
<dependency>
<groupId>hhhhh</groupId>
<artifactId>hhhhh</artifactId>
<version>x.x.x-should not change</version>
</dependency>
</dependencies>
</project>
I am using sed to give the current version as input and change it to a new version given. But I do not want to change version within dependency block. How do I do this?
I do not want to go the maven version plugin route. I have tried it and it does not suit my requirements.
I prefer sed / python script.
Thanks
You could try:
sed -e '/<dependencies>/,/<\/dependencies>/ !{
s!<version>[0-9.]\+</version>!<version>'"$NEWVERSION"'</version>!
}' MY_FILE
The /<dependencies>/,/<\/dependencies>/
says "find all lines between <dependencies>
and </dependencies>
".
The !
after that says "perform the following actions everywhere but those lines (i.e. do this for all lines except between <dependencies>
and </dependencies>
)".
The s!<version>[0-9.]\+</version>!<version>'"$NEWVERSION"'</version>!
says "replace <version>...</version>
with <version>$NEWVERSION</version>
, where $NEWVERSION
is some environment variable that contains the new version number.
The farting around with the quotes ('"$NEWVERSION"'
) was because I wanted single quotes for the main part of the sed command (so I don't have to worry about the exclamation mark & backslashes), but I do want $NEWVERSION
to be expanded.
Modify to suit your script.
To parse and modify XML - you really should use a xml aware parser, such as lxml instead of text tools such as sed
or awk
I assume that your POM files are indeed valid POM files, i.e. they have the enclosing <project>
tag as well.
>>> t = """<project>
... <parent>
... <artifactID> </artifactID>
... <groupID> </groupID>
... <version>2.0.0</version>
... </parent>
...
... <properties>
... <version>2.0.0</version>
... </properties>
...
... <dependencies>
... <dependency>
... <version>2.0.0</version>
... </dependency>
... </dependencies></project>
... """
>>> from lxml import etree
>>> r = etree.fromstring(t)
>>> r.xpath("//parent/version")[0].text = "3.0"
>>> r.xpath("//properties/version")[0].text = "3.0"
>>> print(etree.tostring(r))
<project>
<parent>
<artifactID> </artifactID>
<groupID> </groupID>
<version>3.0</version>
</parent>
<properties>
<version>3.0</version>
</properties>
<dependencies>
<dependency>
<version>2.0.0</version>
</dependency>
</dependencies></project>
.tostring()
after you have updated the the versions, so you should call t.xpath()
on the parsed tree not on the string - Kimvais 2012-04-05 05:09
nawk '{
a=$0;
getline;
if($0!~/depend/ && a!~/version/)
{gsub(/2.0.0/,"1.0.0",$0);print a"\n"$0}
else print a"\n"$0
}' file3
Below is the test:
pearl.302> cat file3
<parent>
<aritifactID> </artifactID>
<groupID> </groupID>
<version>2.0.0</version>
</parent>
<properties>
<version>2.0.0</version>
</properties>
<dependencies>
<dependency>
<version>2.0.0</version>
</dependency>
</dependencies>
pearl.303> nawk '{a=$0;
getline;
if($0!~/depend/ && a!~/version/)
{gsub(/2.0.0/,"1.0.0",$0);print a"\n"$0}
else print a"\n"$0 }' file3
<parent>
<aritifactID> </artifactID>
<groupID> </groupID>
<version>1.0.0</version>
</parent>
<properties>
<version>1.0.0</version>
</properties>
<dependencies>
<dependency>
<version>2.0.0</version>
</dependency>
</dependencies>
awk -v change=1 -v newver=2.3.4 '
change && /version/ {sub(/>[^<]+/, ">" newver)}
/<\/?dependencies>/ {change = !change}
{print}
'
s!\(<version>\)[0-9.]\+\(</version>\)!\1'"$NEWVERSION"'\2!
to save a few characters - Andrew Clark 2012-04-04 00:03