Get pom.xml version with xmllint

耗尽温柔 提交于 2019-11-30 14:01:15

问题


I have a pom.xml as such

<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.qualtrics.sujitv</groupId>
  <artifactId>maven-test</artifactId>
  <packaging>jar</packaging>
  <version>1.2.3.4</version>
  <name>Test app</name>
  <url>http://maven.apache.org</url>

</project>

I am trying to use xmllint to get the version however when I run

xmllint --xpath 'string(project/version)' ./pom.xml

it outputs nothing


回答1:


Since your XML is using namespaces, there's no real nice way to do it. For a direct xpath query, if it's safe to ignore the namespace, you could do something like this:

$ xmllint --xpath '/*[local-name()="project"]/*[local-name()="version"]/text()' pom.xml

If you wanted to do it the "right" way, you'll need to start a shell to setup the namespaces and output the desired value. Unfortunately, you'll have to clean out the prompts in the output.

$ xmllint --shell pom.xml <<< 'setns ns=http://maven.apache.org/POM/4.0.0
cat /ns:project/ns:version/text()'


来源:https://stackoverflow.com/questions/41114695/get-pom-xml-version-with-xmllint

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!