是否有一种方法在编译期间在Java类中使用maven属性 [英] Is there a way to use maven property in java class during compilation
本文介绍了是否有一种方法在编译期间在Java类中使用maven属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我只想在编译时在我的java类中使用maven占位符,以减少重复。
像这样:
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}
}
$提前感谢。 h2_lin>解决方案只需在src / main / resources文件中创建文件app.properties即可。
application.version = $ {project.version}
>
< 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();
cloudProperties.load(inputStream);
}
catch(IOException e){
LOGGER.error(e.getMessage(),e);
}
finally {
Closeables.closeQuietly(inputStream);
}
并提供类似的方法
public static String projectVersion(){
return p.getProperty(application.version);
}
I just want to use maven placeholder in my java class at compile time in order to reduce duplication.
Something like that:
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}"
}
}
Thanks in advance.
解决方案 simply create file app.properties in src/main/resources with content like this
application.version=${project.version}
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();
cloudProperties.load( inputStream );
}
catch ( IOException e ) {
LOGGER.error( e.getMessage(), e );
}
finally {
Closeables.closeQuietly( inputStream );
}
and provide method like this
public static String projectVersion() {
return p.getProperty( "application.version" );
}
这篇关于是否有一种方法在编译期间在Java类中使用maven属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文