访问 pom 中定义的 maven 属性
如何访问在普通 maven 项目和 maven 插件项目中的 pom 中定义的 maven 属性?
如何访问在普通 maven 项目和 maven 插件项目中的 pom 中定义的 maven 属性?
使用属性 maven 插件在编译时将特定的 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");