Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven Change a value in a file based on profile

I have a properties file called ApplicationResources.properties in my application with a property that changes depending on the environment. Let us say the property is:

     resources.location=/home/username/resources 

and this value is different when the application is executed during development and when the application goes into production.

I know that I can use different profiles in Maven to perform different build tasks in different environments. What I want to do is somehow replace the value of the resources.location in the properties file based on the Maven profile in use. Is this even possible?

like image 345
Vincent Ramdhanie Avatar asked Feb 25 '10 16:02

Vincent Ramdhanie


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.

Can Maven profile be defined in POM xml?

A profile in Maven is an alternative set of configuration values which set or override default values. Using a profile, you can customize a build for different environments. Profiles are configured in the pom. xml and are given an identifier.

Why profiles are used in Maven?

Maven profiles can be used to create customized build configurations, like targeting a level of test granularity or a specific deployment environment. In this tutorial, we'll learn how to work with Maven profiles.


1 Answers

What I want to do is somehow replace the value of the resources.location in the properties file based on the Maven profile in use. Is this even possible?

Yes it is. Activate resources filtering and define the value to replace in each profile.

In your ApplicationResources.properties, declare a token to replace like this:

resources.location=${your.location} 

In your POM, add a <filtering> tag for the appropriate <resource> and set it to true like this:

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

Then, add a <your.location> element within the <properties> element inside each profile:

<project>   ...   <profiles>     <profile>       <id>my-profile</id>       ...       <properties>         <your.location>/home/username/resources</your.location>       </properties>       ...     </profile>     ...   </profiles> </project> 

More on filtering of resources here and here.

like image 101
Pascal Thivent Avatar answered Oct 04 '22 06:10

Pascal Thivent