只需在 src/main/resources 中创建文件 app.properties ,内容如下
application.version=${project.version}
然后像这样启用Maven过滤
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
就是这样 - 在应用程序代码中只需读取属性文件
ClassPathResource resource = new ClassPathResource( "app.properties" );
p = new Properties();
InputStream inputStream = null;
try {
inputStream = resource.getInputStream();
p.load( inputStream );
} catch ( IOException e ) {
LOGGER.error( e.getMessage(), e );
} finally {
Closeables.closeQuietly( inputStream );
}
并提供这样的方法
public static String projectVersion() {
return p.getProperty( "application.version" );
}