Fireburn
Fireburn

Reputation: 1021

Set application configuration based on selected Maven profile

I have a maven project that I need to have its app config file content based on the selected maven profile.

The current config file is located:

src/main/java/public/config.json

It's a JSON config file:

{
  "serverUrl" : "http://localhost:8080/api"
}

I want to have a "development" and "production" maven profile that sets the value of the serverUrl depending on the selected profile.

So if it's "production" the content of the JSON file would be:

{
  "serverUrl" : "https://the-prod-server-url.com/api"
}

Upvotes: 0

Views: 635

Answers (1)

Kaj Hejer
Kaj Hejer

Reputation: 1040

You could use filtering in pom.xml:

<profiles>
   <profile>
       <id>development</id>
      <properties>
         <serverUrl>http://localhost:8080/api</serverUrl>
      </properties>
   </profile>
   
   <profile>
       <id>production</id>
      <properties>
         <serverUrl>https://the-prod-server-url.com/api</serverUrl>
      </properties>
   </profile>
</profiles>
<build>
   <resources>
       <resource>
           <directory>src/main/resources</directory>
           <filtering>true</filtering>
       </resource>
   </resources>
</build>

and then put config.json in src/main/resources:

{
  "serverUrl" : "${serverUrl}"
}

Upvotes: 2

Related Questions