0

I have the following maven profile configuration

<profiles>
    <profile>
        <id>dev</id>
        <activation>
            <activeByDefault>true</activeByDefault>
            <property>
                <name>spring.profiles.active</name>
                <value>dev</value>
            </property>
        </activation>
    </profile>
    <profile>
        <id>test</id>
        <activation>
            <property>
                <name>spring.profiles.active</name>
                <value>test</value>
            </property>
        </activation>
    </profile>
</profiles>


I have this in application.properties

spring.profiles.active=dev

So dev is my default for both spring and maven profiles.

I then have Windows system variable SPRING_PROFILES_ACTIVE set with a value of test to explicitly use the test profile.
enter image description here

which according to the documentation this should override the dev value in my properties file

http://docs.spring.io/spring-boot/docs/current/reference/html/howto-properties-and-configuration.html

69.5 Set the active Spring profiles

The Spring Environment has an API for this, but normally you would set a System property (spring.profiles.active) or an OS environment variable (SPRING_PROFILES_ACTIVE). E.g. launch your application with a -D argument (remember to put it before the main class or jar archive):

$ java -jar -Dspring.profiles.active=production demo-0.0.1-SNAPSHOT.jar

In Spring Boot you can also set the active profile in application.properties, e.g.

spring.profiles.active=production

A value set this way is replaced by the System property or environment variable setting, but not by the SpringApplicationBuilder.profiles() method. Thus the latter Java API can be used to augment the profiles without changing the defaults.

See Chapter 25, Profiles in the ‘Spring Boot features’ section for more information.

Now when I run my Spring application it IS using the test profile in Spring, however the maven profile activation does not pick up on this and is still using the dev profile.

I have tried setting SPRING_PROFILES_ACTIVE as the name in the <property> element to make sure it wasn't a issue with the lowercase . version and the uppercase _ version but that didn't help.

It DOES work if I supply the variable when I run maven with -Dspring.profiles.active=test.

Any help is greatly appreciated.

edit: apparently this also doesn't work when I deploy a war to tomcat where catalina.properties contains spring.profiles.active=test. The same thing, Spring uses test but Maven is still stuck on dev.

secondbreakfast
  • 4,194
  • 5
  • 47
  • 101
  • maven profile is activated using -P like we inject environment variable with -D. Command be like: `mvn clean install -Pdev`, which activates `dev` maven profile which in turns activate `dev` spring profile. – Yogen Rai Feb 08 '18 at 21:41
  • @YogenRai I am looking for a solution that will activate the corresponding maven profile of whatever the spring profile is set as – secondbreakfast Feb 09 '18 at 01:15
  • Maven controls spring’s profile.. idk if that is achievable .. but good goal :) – Yogen Rai Feb 09 '18 at 01:17

2 Answers2

1

How about activating the profile by using environment variable ?

<profiles>
    <profile>
        <id>dev</id>
        <activation>
            <activeByDefault>true</activeByDefault>
            <property>
                <name>spring.profiles.active</name>
                <value>dev</value>
            </property>
        </activation>
    </profile>
    <profile>
        <id>test</id>
        <activation>
            <property>
                <name>env.SPRING_PROFILES_ACTIVE</name>
                <value>test</value>
            </property>
        </activation>
    </profile>
</profiles>
suenda
  • 773
  • 1
  • 8
  • 21
0

Unfortunately, what you want to do is not possible. Profiles can only be activated from the command line or in some cases from the settings.xml. See this answer. This one includes links to JIRA issues discussing the challenges and what others have tried to make it work.

System properties are set on the command line.

user944849
  • 14,524
  • 2
  • 61
  • 83
  • But in the documentation it says it can use a system property `Profiles can be automatically triggered based on the detected state of the build environment. ...... Currently, this detection is limited to prefix-matching of the JDK version, the presence of a system property or the value of a system property.` http://maven.apache.org/guides/introduction/introduction-to-profiles.html – secondbreakfast Feb 08 '18 at 20:35
  • Properties that activate profiles may come from the `settings.xml` and from the command line, not from files within the app as you're attempting. Expanded on the answer. – user944849 Feb 09 '18 at 14:55