Maven profiles and application yml file in Spring

15,585

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;
Share:
15,585
Dexter
Author by

Dexter

Developer

Updated on June 18, 2022

Comments

  • Dexter
    Dexter almost 2 years

    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?