0

I'm having trouble to make @Profile work with maven and without Spring Boot. In pom.xml I have defined maven profiles ("env" and "dev" which is also default).

Unfortunetly whenever I try to build project with:

mvn clean install -Dspring.profiles.active=env 

The "default" profile is always applied (in Spring - maven applies "env" for maven purpose). I have also tried to make it work with System.getProperty("spring.profiles.active") and System.getenv("spring.profiles.active") but those always returned null. I think it is also worth to mention that this is non-web application.

Beans (to read proper properties):

    @Bean
    @Profile({"default"})
    public static PropertySourcesPlaceholderConfigurer defaultProperties() {
        return getPropertySourcesPlaceholderConfigurer("db.yml");
    }

    @Bean
    @Profile({"env"})
    public static PropertySourcesPlaceholderConfigurer envProperties() {
        return getPropertySourcesPlaceholderConfigurer("db-env.yml");
    }

    @Bean
    @Profile({"test"})
    public static PropertySourcesPlaceholderConfigurer devProperties() {
        return getPropertySourcesPlaceholderConfigurer("db-test.yml");
    }

    private static PropertySourcesPlaceholderConfigurer getPropertySourcesPlaceholderConfigurer(String resource) {
        PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
        YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
        yaml.setResources(new ClassPathResource(resource));
        final Properties object = yaml.getObject();
        if (object != null) {
            propertySourcesPlaceholderConfigurer.setProperties(object);
        }
        return propertySourcesPlaceholderConfigurer;
    }

Pom.xml:

<profiles>
    <profile>
      <id>dev</id>
      <activation>
        <activeByDefault>true</activeByDefault>
        <property>
          <name>spring.profiles.active</name>
          <value>dev</value>
        </property>
      </activation>
    </profile>
    <profile>
      <id>env</id>
      <activation>
        <property>
          <name>spring.profiles.active</name>
          <value>env</value>
        </property>
      </activation>
    </profile>
  </profiles>
lukaszrys
  • 1,656
  • 4
  • 19
  • 33
  • 4
    `mvn clean install -Dspring.profiles.active=env ` this pass java properties to maven NOT to your application. – M. Deinum Apr 24 '18 at 13:18
  • Try with `--spring.profiles.active=env` – Vincz777 Apr 24 '18 at 13:21
  • @lukaszrys May I know how you solved this one? I am facing same issue in my no-spring boot testing project, and trying to run like: mvn clean test -Dspring.profiles.active=p1 mvn clean test -Pp1 (with maven profile) but I see no profiles are taking effect when running. – Shan C May 19 '22 at 23:30

2 Answers2

2

The Spring profiles are for running your appplication, not building. Pass the -Dspring.profiles.active=env when you execute your application. In your example you are doing a mvn install which does not execute your application.

Jose Martinez
  • 11,452
  • 7
  • 53
  • 68
  • Your statement is mostly true, but there are cases when you want to specify the spring profile in the build. For example, if you are packaging an application into a war file for deployment to a container, e.g. Tomcat, you may want to specify the active profiles in the config file packaged inside the war. – Erik Pearson Apr 24 '18 at 14:16
1

You need to specify the Maven profile (not the Spring profile) when running the command:

mvn clean install -Penv

See: http://maven.apache.org/guides/introduction/introduction-to-profiles.html

Erik Pearson
  • 1,363
  • 1
  • 11
  • 20
  • the maven profile activates due to ` spring.profiles.active env `. The spring profile does not activate – lukaszrys Apr 24 '18 at 13:29
  • Try ` env env ` You can specify a default activation, but beyond that you need to tell Maven which profiles are active. – Erik Pearson Apr 24 '18 at 13:32
  • This is the syntax I use when specifying a default: ` dev true dev `, which is a little different than how you are using it, though I don't pretend to know all the different ways you can do things in Maven. But try my command above, specifying the Maven profile explicitly and see what that does for you. – Erik Pearson Apr 24 '18 at 13:40