使用xmllint获取pom.xml版本 [英] Get pom.xml version with xmllint

查看:443
本文介绍了使用xmllint获取pom.xml版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我也有一个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>

我正在尝试使用xmllint获取版本,

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

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

它什么也没输出

推荐答案

由于您的XML使用名称空间,因此没有真正好的方法.对于直接的xpath查询,如果可以安全地忽略名称空间,则可以执行以下操作:

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

如果您想以正确"的方式进行操作,则需要启动Shell来设置名称空间并输出所需的值.不幸的是,您必须清除输出中的提示.

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()'

这篇关于使用xmllint获取pom.xml版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆