访问 pom 中定义的 maven 属性 [英] Access maven properties defined in the pom
本文介绍了访问 pom 中定义的 maven 属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何访问在普通 maven 项目和 maven 插件项目中的 pom 中定义的 maven 属性?
How do I access maven properties defined in the pom in a normal maven project, and in a maven plugin project?
推荐答案
使用 properties-maven-plugin 在编译时将特定的 pom properties
写入文件,然后在运行时读取该文件.
Use the properties-maven-plugin to write specific pom properties
to a file at compile time, and then read that file at run time.
在您的 pom.xml 中:
<properties>
<name>${project.name}</name>
<version>${project.version}</version>
<foo>bar</foo>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0.0</version>
<executions>
<execution>
<phase>generate-resources</phase>
<goals>
<goal>write-project-properties</goal>
</goals>
<configuration>
<outputFile>${project.build.outputDirectory}/my.properties</outputFile>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
然后在 .java 中:
java.io.InputStream is = this.getClass().getResourceAsStream("my.properties");
java.util.Properties p = new Properties();
p.load(is);
String name = p.getProperty("name");
String version = p.getProperty("version");
String foo = p.getProperty("foo");
这篇关于访问 pom 中定义的 maven 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文