访问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 属性
在编译时放入文件,然后在运行时读取该文件。
Use the properties-maven-plugin to write out specific pom properties
into 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屋!
查看全文