Use the properties-maven-plugin to write specific pom properties
to a file at compile time, and then read that file at run time.
In your 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>
And then in .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");
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…