14
7
I want to read a pom.xml ('Project Object Model' of Maven) and extract the version information. Here is an example:
<?xml version="1.0" encoding="UTF-8"?><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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany</groupId>
<artifactId>project-parent</artifactId>
<name>project-parent</name>
<version>1.0.74-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.sybase.jconnect</groupId>
<artifactId>jconnect</artifactId>
<version>6.05-26023</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>1.5.2</version>
</dependency>
<dependency>
<groupId>com.sun.jdmk</groupId>
<artifactId>jmxtools</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>org.easymock</groupId>
<artifactId>easymock</artifactId>
<version>2.4</version>
</dependency>
</dependencies>
</project>
How can I extract the version '1.0.74-SNAPSHOT' from above?
Would love to be able to do so using simple bash scripting sed or awk. Otherwise a simple python is preferred.
EDIT
Constraint
The linux box is in a corporate environment so I can only use tools that are already installed (not that I cannot request utility such as xml2, but I have to go through a lot of red-tape). Some of the solutions are very good (learn a few new tricks already), but they may not be applicable due to the restricted environment
updated xml listing
I added the dependencies tag to the original listing. This will show some hacky solution may not work in this case
Distro
The distro I am using is RHEL4
http://stackoverflow.com/questions/893585/how-to-parse-xml-in-bash – Ciro Santilli 新疆改造中心法轮功六四事件 – 2015-10-07T10:57:49.950
Is this http://stackoverflow.com/questions/29004/parsing-xml-using-unix-terminal sufficient?
– bbaja42 – 2011-12-20T22:08:43.480Not really. There are a lot of version tag in the xml (e.g. under dependencies tag). I only want '/project/version' – Anthony Kong – 2011-12-20T22:20:40.603
Which xml-related tools and libraries are available? Are jvm-based soltuions OK? – Vi. – 2011-12-20T23:22:14.773
So far I can tell xml2, xmlgrep and perl XML module are not present. Most unix command-line utilities are present. The distro is Redhat EL 4. – Anthony Kong – 2011-12-20T23:38:52.677
(I couldn't add a comment so I have to reply as an answer, overkill somewhat) Some great answers can be found here..... http://stackoverflow.com/questions/2735548/xslt-document-function-returns-empty-result-on-maven-pom/2737427#2737427
– JStrahl – 2013-01-18T10:12:10.267