maven:从命令行(commandline)读取取pom.xml中的属性(property)

如何在命令行(shell)读取pom.xml中的属性呢?
我在网上找到如下回答:
https://stackoverflow.com/questions/23802951/get-pom-xml-property-from-commandline

就是用maven的内置插件 maven-help-plugin 来实现,在windows 命令行下如下实现:

mvn help:evaluate -Dexpression=[property-name] | findstr /R ^^[^^\[INFO\]]

这其中用到了findstr命令(在linux下要用grep或sed来代替)过滤mvn输出的内容进行得到需要的值,以${project.version}为例

mvn help:evaluate -Dexpression=project.version | findstr /R ^^[^^\[INFO\]]

不依赖findstr过滤更简单通用的方式如下:

mvn help:evaluate -Dexpression=project.version -q -DforceStdout

但是我用的MAVEN 3.5.0下执行并不成功,什么也没有输出,百思不得其解

后来找到下面的文章,就明白了:forceStdout这个选项是在maven-help-plugin 3.2.0以上版本才有效的(我在maven 3.5.0下执行时,maven-help-plugin默认使用的版本是2.2)。

https://stackoverflow.com/questions/3545292/how-to-get-maven-project-version-to-the-bash-command-line

在这里插入图片描述

所以如下指定help插件的版本就可以正常输出了:

mvn org.apache.maven.plugins:maven-help-plugin:3.2.0:evaluate -Dexpression=project.version -q -DforceStdout

参考资料
help:evaluate的官方文档:
http://maven.apache.org/plugins/maven-help-plugin/evaluate-mojo.html
NOTE:根据这个文档,forceStdout这个选项从maven-help-plugin 3.1.0就有了,但3.1.0版本下实测也是无效的

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
在Java读取Mavenpom.xml文件可以使用以下步骤: 1. 创建文件对象 首先需要创建一个File对象,用于表示pom.xml文件的路径。可以使用以下代码创建File对象: ``` File pomFile = new File("pom.xml"); ``` 2. 加载pom.xml文件 使用Java的DOM解析器来加载pom.xml文件。可以使用以下代码来加载pom.xml文件: ``` DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(pomFile); ``` 3. 获根元素 通过Document对象获pom.xml文件的根元素。可以使用以下代码获根元素: ``` Element root = doc.getDocumentElement(); ``` 4. 获元素值 通过Element对象获pom.xml文件的元素值。可以使用以下代码获元素值: ``` String groupId = root.getElementsByTagName("groupId").item(0).getTextContent(); String artifactId = root.getElementsByTagName("artifactId").item(0).getTextContent(); String version = root.getElementsByTagName("version").item(0).getTextContent(); ``` 以上代码将获pom.xml文件的groupId、artifactId和version元素的值。您可以根据需要获其他元素的值。 完整的代码示例: ``` import java.io.File; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; public class ReadPomXml { public static void main(String[] args) throws Exception { File pomFile = new File("pom.xml"); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(pomFile); Element root = doc.getDocumentElement(); String groupId = root.getElementsByTagName("groupId").item(0).getTextContent(); String artifactId = root.getElementsByTagName("artifactId").item(0).getTextContent(); String version = root.getElementsByTagName("version").item(0).getTextContent(); System.out.println("groupId: " + groupId); System.out.println("artifactId: " + artifactId); System.out.println("version: " + version); } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

10km

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值