Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven profiles and application yml file in Spring

I have a Spring project that I build using the following command

mvn clean install -Pdevelopment

which worked perfectly by selecting the appropriate properties file based on maven profile

Our current application is now updated to have both application.yml file and properties file

Yml file provides the ability to create properties based on spring profiles

#DEV
spring:
    profiles: profile1
environment:
    property1: AAA
    property2: BBB
---
#PROD
spring:
    profiles: profile2
environment:
    property1: CCC
    property2: DDD
---

This works fine with spring profiles using -Dspring.profiles.active=profile1

Is there a way to read maven profiles (instead of spring profiles) and set properties accordingly?

like image 384
Dexter Avatar asked Jan 05 '16 15:01

Dexter


1 Answers

Since you no longer want to use spring profiles, you only need 1 key of each in the application.yml. Taken from your example it could look like so:

environment:
  property1: @property1@
  property2: @property2@

Then make profiles in your pom.xml or settings.xml

<profiles>
    <profile>
        <id>dev</id>
        <properties>
            <property1>AAA</property1>
            <property2>BBB</property2>
        </properties>
    </profile>
    <profile>
        <id>prod</id>
        <properties>
            <property1>CCC</property1>
            <property2>DDD</property2>
        </properties>
    </profile>
</profiles>

Used in my application class like so:

@Value("${environment.property1}")
private String profileProperty;

@Value("${environment.property2}")
private String settingsProperty;
like image 151
Martin Hansen Avatar answered Sep 24 '22 19:09

Martin Hansen