I am having one POM.xml file which is having details like "artifactId" & "version" currently I am trying to pull those details from pom.xml through Shell script.
NOTE: I dont want to print "version" and "artifactID" from dependencies block.
<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>
<groupId>com.javatpoint.application1</groupId>
<artifactId>my-application1</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>Maven Quick Start Archetype</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
I tried through grep but no luck :( .
I would recommend installing xpath
, which can be installed via yum install perl-XML-XPath
. Since xpath
is an xml parser, you can have more confidence that you are getting the correct piece of xml vs a regular expression based solution.
Here is an example with your file:
#!/bin/bash
function get_xpath_value {
xml=$1
path=$2
if [ -f $xml ]; then
# xpath returns <foo>value</foo>, so we need to unpack it
value=$( xpath $xml $path 2>/dev/null | perl -pe 's/^.+?\>//; s/\<.+?$//;' )
echo -n $value
else
echo 'Invalid xml file "$xml"!'
exit 1;
fi
}
pom_xml='foo.xml'
artifactId=$( get_xpath_value $pom_xml 'project/artifactId' )
version=$( get_xpath_value $pom_xml 'project/version' )
echo "ArtifactId is $artifactId"
echo "Version is $version"
Output
ArtifactId is my-application1
Version is 1.0
你想要什么格式?
awk -F '>|<' '/version|artifactId/ {print $3}'file
this helps ? if you have more dependices block, then we have to reset the flag value
bash-3.2$ awk '/depend/{flag=1}flag&&/artifactId|version/{next}1' test.txt
<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>
<groupId>com.javatpoint.application1</groupId>
<artifactId>my-application1</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>Maven Quick Start Archetype</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<scope>test</scope>
</dependency>
</dependencies>
awk -F '<[^>]*>' '/<dependencies>/,/<\/dependencies>/{next} /artifactId/{$1=$1;print "Artifact IF is:" $0} /version/ {$1=$1;print "Version is:" $0}' input.xml
Artifact IF is: my-application1
Version is: 1.0
Above code , first ignores the text between dependencies
block. After that it search for artifactid
and print its value. Similarly for Version. Field separator is defined to take care of xml
tags.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.