Skip to content

Instantly share code, notes, and snippets.

@pavankjadda
Last active December 18, 2023 13:07
Show Gist options
  • Save pavankjadda/a9e684c7db699a050d87be4a8c391e4c to your computer and use it in GitHub Desktop.
Save pavankjadda/a9e684c7db699a050d87be4a8c391e4c to your computer and use it in GitHub Desktop.
How to select Spring boot profile from maven.md

How to select Spring boot profile from maven?

  1. Define following profiles in pom.xml
<profiles>
   <profile>
        <id>dev</id>
        <properties>
            <activatedProperties>dev</activatedProperties>
        </properties>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>
    <profile>
        <id>test</id>
        <properties>
            <activatedProperties>test</activatedProperties>
        </properties>
    </profile>
    <profile>
        <id>prod</id>
        <properties>
            <activatedProperties>prod</activatedProperties>
        </properties>
    </profile>
  </profiles>

  1. Using following command will activate dev profile, remember activatedProperties value should match in the following command after -D like -Ddev, -Dtest or -Dprod
$ mvn clean package -Ddev
$ mvn test -Dtest
  1. Create application.properties or application.yml file with the following content
spring:
   profiles:
      active: @activatedProperties@
  1. Create application-dev.yml, application-test.yml and application-prod.yml
  2. When you run the build, the Maven Resources Plugin will replace the activatedProperties placeholder in application.properties with the property from the currently active Maven profile, which will select spring boot profile
@RamenSayami
Copy link

Step 3:
im yml file we cannot write like that should it be:

spring:
profiles:
active: @activatedProperties@

still it does not work

@pavankjadda
Copy link
Author

Step 3:
im yml file we cannot write like that should it be:

spring:
profiles:
active: @activatedProperties@

still it does not work

Correct, it has be of format

spring:
   profiles:
      active: @activatedProperties@

@RamenSayami
Copy link

yes sorry, the tab spaces didnt show up.. but i did with tab spaces.
turns out we need to single quote the @activatedProperties@ in case of using application.yml
this worked for me:

spring:
   profiles:
      active: '@activatedProperties@'
      

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment