Reading Spring profiles from a @controller

carles xuriguera :

I want to link the maven profiles to spring profiles. The project is build in a war file. The application is a Spring application, not a SpringBoot, deployed in a Weblogic Server.

I have this pom.xml file

<profile>
    <id>debug</id>
    <activation>
        <activeByDefault>true</activeByDefault>
    </activation>
    <properties>
        <spring.profiles.active>debug</spring.profiles.active>
    </properties>
</profile>
..

and in the application @Controller:

  @Autowired
  private Environment env;

  final List<String> activeProfiles = Arrays.asList(env.getActiveProfiles());

but activeProfiles is empty.

I got the same result using

<activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <spring.profiles.to.activate>local</spring.profiles.to.activate>
            </properties>
Karol Dowbecki :

You are merely defining spring.profiles.active property value with a Maven profile. The property is available for the Maven build e.g. for resource filtering.

The value still has to be passed to a JVM running Spring Boot, e.g. JUnit tests are usually run with Maven Surefire Plugin which forks the JVM and won't see the properties if you don't use argLine configuration:

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-surefire-plugin</artifactId>
      <configuration>
        <argLine>-Dspring.profiles.active=${spring.profiles.active}</argLine>
      </configuration>
    </plugin>
  </plugins>
</build>

Above is usually true for any Maven plugin used to start Spring Boot application.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=133522&siteId=1