有没有办法在编译期间在 Java 类中使用 maven 属性 [英] Is there a way to use maven property in Java class during compilation
本文介绍了有没有办法在编译期间在 Java 类中使用 maven 属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我只想在编译时在我的 Java 类中使用 maven 占位符以减少重复.
I just want to use maven placeholder in my Java class at compile time in order to reduce duplication.
类似的东西:
pom.xml
<properties>
<some.version>1.0</some.version>
</properties>
SomeVersion.java
package some.company;
public class SomeVersion {
public static String getVersion() {
return "${some.version}"
}
}
推荐答案
只需在 src/main/resources 中创建文件 app.properties,内容如下
simply create file app.properties in src/main/resources with content like this
application.version=${project.version}
然后像这样启用maven过滤
then enable maven filtering like this
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
仅此而已 - 在应用程序代码中只需读取属性文件
That's all - in app code just read properties file
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" );
}
这篇关于有没有办法在编译期间在 Java 类中使用 maven 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文