I just want to use maven placeholder in my Java class at compile time in order to reduce duplication.
Something like that:
pom.xml
Even though it's not a very nice solution it is possible with the default maven resource plugin.
First you need to specify the resource plugin.
<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>
Afterwards you will need to change the 'default' configuration of the compiler plugin.
<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>
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();
p.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" );
}
The simplest way I know of doing that is to use Templating Maven Plugin.
Add plugin declaration to your 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>
If you're filtering main sources:
src/main/java-templates
src/main
.If you're filtering tests sources too:
src/test/java-templates
src/test
.Assuming that your sources contain valid placeholders like:
package some.company;
public class SomeVersion {
public static String getVersion() {
return "${project.version}"
}
}
Now when you compile
or test
your project, those placeholders should be already valued.
Hope it helps.
If you are working with Spring, you can inject a property. The steps are:
<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 }
On the final compilation, you have the correct values.