Property File Processing during build time
-
01-07-2021 - |
Question
My requirments are as follow :
- we are having four environments(dev,uat,prod,stag)
- currently we are having 5 property files(log4j.properties for example) one for each
environment like : log4j_dev.properties, log4j_uat.properties, log4j_prod.properties, log4j_stag.properties
- all the above files have most of the things in common, just some 4 to 5 properties are different in each file.
my current requirments is that we have to put all the properites in a common file(log4j.properties for example ) and the environment specific file would contain only the specific 4 to 5 property . during build time these files should be merged and a new file should be created containing all these properties .
I tried maven-config-processor-plugin thats working fine with mavne 2 , but its not compatible with maven3 . Currently we are using maven3 so i need some alternative approach . Is there any alternative to maven config-processor or i need to write some new maven plugin for doing so .
Any suggestions plz .
Solution
Have you tried to use maven-resources-plugin and the filtering option on resources
Here is an example :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<filters>
<filter>src/main/filters/${target.filter}.properties</filter>
</filters>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/test/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
Then you have a directory src/main/filters. For each of your environment you add a filter_env.properties. In it, you put your key you want to variabilize, like this
ys.client.service.port=8511
ys.client.service.url=http://localhost/yoda-client/service/yoda
ys.env=dev
And finally, in your properties file, you add variables instead of real values. The plugin will replace values with the filters ones :
ys.client.service.port=${ys.client.service.port}
ys.client.service.url=${ys.client.service.url}
ys.environment=${ys.env}