简体   繁体   English

使用xmllint获取pom.xml版本

[英]Get pom.xml version with xmllint

I have a pom.xml as such 我有一个pom.xml

<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获取版本,但是当我运行时

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

it outputs nothing 它什么也没输出

Since your XML is using namespaces, there's no real nice way to do it. 由于您的XML使用名称空间,因此没有真正好的方法。 For a direct xpath query, if it's safe to ignore the namespace, you could do something like this: 对于直接xpath查询,如果忽略命名空间是安全的,您可以执行以下操作:

$ 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. 如果您想以“正确”的方式执行此操作,则需要启动shell来设置命名空间并输出所需的值。 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()'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM