gpt4 book ai didi

java - 引用 pom.xml 中的标签

转载 作者:行者123 更新时间:2023-11-30 08:54:04 24 4
gpt4 key购买 nike

在 java 中如何或通常可以引用 pom.xml 中的标签?您可以在 pom 中进行引用,例如:<outputDirectory>${project.build.directory} .

但我想做这样的事情:

...
System.out.println(${project.version});
...

这可能吗?

最佳答案

在构建时,您可以过滤一些资源文件,以便在运行时读取文件并获取您正在寻找的值:

http://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html

  <resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>

或者如果它更复杂,你可以使用程序集插件:

https://maven.apache.org/plugins/maven-assembly-plugin/examples/single/filtering-some-distribution-files.html

例如:src/main/resources/info.properties

VERSION=${project.version}

在运行时:

    Properties props = new Properties();
props.load(this.getClass().getResourceAsStream("info.properties"));

System.out.println(props.get("VERSION"));

关于java - 引用 pom.xml 中的标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29560019/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com