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>