Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I change a .properties file in maven depending on my profile?

How can I change a .properties file in maven depending on my profile? Depending on whether the application is built to run on a workstation or the datacenter parts of the file my_config.properties change (but not all).

Currently I manually change the .properties file within the .war file after hudson builds each version.

like image 332
benstpierre Avatar asked Oct 05 '10 22:10

benstpierre


People also ask

How do I run a specific profile in Maven?

Open Maven settings. m2 directory where %USER_HOME% represents the user home directory. If settings. xml file is not there, then create a new one. Add test profile as an active profile using active Profiles node as shown below in example.

Where are profiles configured in Maven?

Profiles can be activated in the Maven settings, via the <activeProfiles> section. This section takes a list of <activeProfile> elements, each containing a profile-id inside. Profiles listed in the <activeProfiles> tag would be activated by default every time a project use it.


1 Answers

As often, there are several ways to implement this kind of things. But most of them are variations around the same features: profiles and filtering. I'll show the most simple approach.

First, enable filtering of resources:

<project>   ...   <build>     <resources>       <resource>         <directory>src/main/resources</directory>         <filtering>true</filtering>       </resource>     </resources>     ...   </build> </project> 

Then, declare a place holder in your src/main/resources/my_config.properties, for example:

myprop1 = somevalue myprop2 = ${foo.bar} 

Finally, declare properties and their values in a profile:

<project>   ...   <profiles>     <profile>       <id>env-dev</id>       <activation>         <property>           <name>env</name>           <value>dev</value>         </property>       </activation>       <properties>         <foo.bar>othervalue</foo.bar>       </properties>     </profile>     ...   </profiles> </project> 

And run maven with a given profile:

 $ mvn process-resources -Denv=dev [INFO] Scanning for projects... ... $ cat target/classes/my_config.properties  myprop1 = somevalue myprop2 = othervalue 

As I said, there are variation around this approach (e.g. you can place values to filter in files), but this will get you started.

References

  • Introduction to Build Profiles
  • Maven Resources Filtering

More resources

  • A Maven2 multi-environment filter setup
  • Maven project filtering
  • Using Maven profiles and resource filtering
  • Building For Different Environments with Maven 2
like image 80
Pascal Thivent Avatar answered Nov 05 '22 08:11

Pascal Thivent