Issue
I want to put together an environment in Maven, where I want to activate multiple spring profiles cumulatively depending on what Maven profiles are active.
Currently the relevant part of my pom.xml looks like this:
<profiles>
<profile>
<id>local-db-development</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<dependencies>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
<properties>
<spring.profiles.active>local-db</spring.profiles.active>
</properties>
</profile>
<profile>
<id>live-db-development</id>
<dependencies>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
<properties>
<spring.profiles.active>live-db</spring.profiles.active>
</properties>
</profile>
<profile>
<!--
If active, the user authentication will be through LDAP and AD,
Database auth will be used otherwise
-->
<id>ldap-authentication</id>
<properties>
<spring.profiles.active>ldap-authentication</spring.profiles.active>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-ldap</artifactId>
</dependency>
<dependency>
<groupId>org.apache.directory.server</groupId>
<artifactId>apacheds-server-jndi</artifactId>
<version>1.5.5</version>
</dependency>
</dependencies>
</profile>
</profiles>
My problem is that when I activate ldap-authentication profile, only the relevant spring profile will be active, not taking into account any of the *-db profiles.
I'd like to make it so that I can activate the ldap-authentication and local-db maven profiles and then both relevant spring profiles should be activated, or when I activate let's say ldap-authentication and live-db in maven, then those 2 spring profiles will be active.
I haven't really found a way to concat the spring profiles, so if you know better, let me know.
Thanks in advance
Solution
Okay, I found one solution: You need a groovy plugin, which looks like this:
<plugin>
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
<version>1.6</version>
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>2.4.9</version>
<scope>runtime</scope>
</dependency>
</dependencies>
<executions>
<execution>
<id>concatMavenProfiles</id>
<phase>initialize</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<scripts>
<script><![CDATA[
def value = ""
(project.activeProfiles).each{ profile -> value += profile.properties.springProfiles + "," }
project.properties.setProperty('spring.profiles.active', value.substring(0, value.length() - 1))
]]>
</script>
</scripts>
</configuration>
</execution>
</executions>
</plugin>
This scripts gets all <springProfiles> properties from the active profiles and concatenates them into one string. It then sets the <spring.active.profiles> property in the main <properties> tag.
I switched <spring.profiles.active> tag in the profiles to <springProfiles>, so that they have 0 chance of clashing and overwriteing one another.
In my experience, you also need to define an empty <spring.profiles.active> tag in the project <properties> tag.
Answered By - László Stahorszki
Answer Checked By - Marilyn (JavaFixing Volunteer)