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}"
}
}
If you are working with Spring, you can inject a property. The steps are:
On the final compilation, you have the correct values.
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.
Afterwards you will need to change the 'default' configuration of the compiler plugin.
The simplest way I know of doing that is to use Templating Maven Plugin.
Add plugin declaration to your pom:
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:
Now when you
compile
ortest
your project, those placeholders should be already valued.Hope it helps.
simply create file app.properties in src/main/resources with content like this
then enable maven filtering like this
That's all - in app code just read properties file
and provide method like this