简体   繁体   中英

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"
}

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}"
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM