Use maven profile properties in application.conf

543 Views Asked by At

In my pom.xml file, I have multiple profiles set up. I would like to use the current profile's values in my application.conf file. The Ninja Framework documentation only mentions mode configurations, however I can't find anything concerning profile configurations.

An example: The documentation mentions

database.name=database_production   # will be used when no mode is set (or prod)
%prod.database.name=database_prod   # will be used when running in prod mode
%dev.database.name=database_dev     # will be used when running in dev mode
%test.database.name=database_test   # will be used when running in test mode

How would I be able to set different database names depending on the currently used profile?

1

There are 1 best solutions below

2
ialex On BEST ANSWER

You could add whatever it is you want replaced as a property to your profile definition, and then enable maven resource filtering like this:

<build>
  ..
  <resources>
    <resource>
      <directory>src/main/resources</directory>
      <filtering>true</filtering>
    </resource>
  </resources>
  ..
</build>
<profiles>
  <profile>
    <id>developmentProfile</id>
    <properties>
      <dbName>database.name=database_dev</dbName>
    </properties>
    <activation>
      <activeByDefault>false</activeByDefault>
    </activation>
  </profile>
  <profile>
    <id>productionProfile</id>
    <properties>
      <dbName>database.name=database_prod</dbName>
    </properties>
    <activation>
      <activeByDefault>true</activeByDefault>
    </activation>
  </profile>
</profiles>

Be careful, if you use spring-boot, you should reference the properties in the application.properties file like this for it to work:

someOtherProp = something
@dbName@

After that, building the app should filter the property correctly:

mvn clean install -PdevelopmentProfile

Result:

someOtherProp=something
database.name=database_dev