26

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?

Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
Vincent Ramdhanie
  • 102,349
  • 23
  • 137
  • 192

1 Answers1

52

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.

Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
  • can you please share complete pom.xml which plugin is needed for this ? I could get it working, please review https://stackoverflow.com/questions/51186963/value-from-maven-profile-properties-is-not-getting-used-in-properties-file – vikramvi Jul 05 '18 at 13:25