1
votes

I am trying to get a list of all of the active profiles in maven and use them as a variable in resource filtering.

In particular, when I run something like

mvn package -Pprofile1,profile2

I want to expose the string "profile1,profile2" as a variable in resource filtering.

For example, in my pom.xml file I have the maven war plugin configured as follows to allow resource filtering on everything in the src/main/resources/META-INF directory, and I have a few profiles defined:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <configuration>
        <archive>
            <manifest>
                <addClasspath>true</addClasspath>
                <classpathPrefix>lib/</classpathPrefix>
            </manifest>
        </archive>
        <webResources>
            <resource>
                <directory>src/main/resources</directory>
                <!-- Only include the META-INF directory -->
                <includes>
                    <include>META-INF/*</include>
                </includes>
                <!-- replace placeholder values -->
                <filtering>true</filtering>
                    </resource>
                </webResources>
    </configuration>
</plugin>
...
<profiles>
    <profile>
        <id>profile1</id>
        <properties>
            <environment>profile1</environment>
        </properties>
    </profile>
    <profile>
        <id>profile2</id>
        <properties>
            <environment>profile2</environment>
        </properties>
    </profile>

Under the META-INF directory, I have a context.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<Context path="/platform">
    <Environment name="spring.profiles.active" value="${environment}" type="java.lang.String" />
</Context>

I tried setting the property "environment" under each profile but the resulting context.xml file only uses the last "environment" variable (e.g. value=profile2).

I also tried using this: value=${project.activeProfiles} but that returns some sort of list of objects. It would be great if I could iterate over this list and extract the ids from the profiles to build the string, but I couldn't find any documentation in the maven resources plugin on doing this.

What I'm ultimately trying to do is set the spring.profiles.active variable as an environment variable via the context.xml file, and that variable is the same as the maven build profiles.

1
One thought about your pom file. Why are defining the src/main/resources as webResources cause src/main/resources is a usual resource which is copied during resource phase and if you like to filter it define it like this...khmarbaise
I did that to place the context.xml file into the META-INF folder, I'm not sure if that's the proper way to do it, but it does work for me.KyoreG
Hey, any luck with this? I am also looking for a way to "I could iterate over this list and extract the ids from the profiles to build the string".TroJaN

1 Answers

0
votes

You can list the active profiles via:

mvn help:active-profiles