如何在普通 maven 项目和 maven 插件项目中访问 pom 中定义的 maven 属性?
最佳答案
使用 properties-maven-plugin在编译时将特定的 pom properties
写入文件,然后在运行时读取该文件。
在您的 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");
关于java - 访问 pom 中定义的 maven 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11500533/