我只是想在编译时在我的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}"
}
}
答案 0 :(得分:56)
只需使用这样的内容在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" );
}
答案 1 :(得分:10)
即使它不是一个非常好的解决方案,也可以使用默认的maven资源插件。
首先,您需要指定资源插件。
<project>
<build>
<!-- Configure the source files as resources to be filtered
into a custom target directory -->
<resources>
<resource>
<directory>src/main/java</directory>
<filtering>true</filtering>
<targetPath>../filtered-sources/java</targetPath>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
</project>
之后您将需要更改编译器插件的“默认”配置。
<project>
<build>
<!-- Overrule the default pom source directory to match
our generated sources so the compiler will pick them up -->
<sourceDirectory>target/filtered-sources/java</sourceDirectory>
</build>
</project>
答案 2 :(得分:3)
我知道这样做的最简单方法是使用Templating Maven Plugin。
将插件声明添加到您的pom:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>templating-maven-plugin</artifactId>
<version>1.0.0</version>
<executions>
<execution>
<id>filter-src</id>
<goals>
<goal>filter-sources</goal><!-- add this if you filter main sources -->
<goal>filter-test-sources</goal><!-- add this if you filter test sources -->
</goals>
</execution>
</executions>
</plugin>
如果您要过滤主要来源:
src/main/java-templates
src/main
。如果你也过滤了测试来源:
src/test/java-templates
src/test
。假设您的来源包含有效的占位符,例如:
package some.company;
public class SomeVersion {
public static String getVersion() {
return "${project.version}"
}
}
现在当您compile
或test
您的项目时,这些占位符应该已经被重视了。
希望它有所帮助。
答案 3 :(得分:1)
如果您正在使用Spring,则可以注入属性。步骤是:
<profile>
<id>dev</id>
<properties>
<some.version>Dev Value</some.version>
</properties>
</profile>
custom.some.version = $ {some.version}
<context:property-placeholder location="classpath*:/META-INF/*.properties"/>
...
<bean id="customConfig" class="com.brand.CustomConfig">
<property name="someVersion" value="${custom.some.version}" />
</bean>
...
package com.brand; public class CustomConfig { private String someVersion; public getSomeVersion() { return this.someVersion; } public setSomeVersion(String someVersion) { this.someVersion = someVersion; } }
package com.brand.sub public class YourLogicClass { @Autowired private CustomConfig customConfig; // ... your code }
在最终编译中,您拥有正确的值。