Maven: How to print the current profile on the console?

Alessandro C picture Alessandro C · Dec 13, 2016 · Viewed 12.1k times · Source

I'm trying to print the current profile that is active running a build of a Maven Project.

I'm using the maven-antrun-plugin in order to print messages on the console, in combination with a property that refers to the current profile.

I have tried the following properties:

${project.activeProfiles[0].id}

${project.profiles[0].id}

But in both cases it prints the "string" as it is written, without resolving the variable.

This is my test:

<build>
    <plugins>
        <plugin>
            <artifactId>maven-antrun-plugin</artifactId>
            <executions>
                <execution>
                    <phase>generate-resources</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                    <configuration>
                        <tasks>
                            <echo>current active profile: ${project.activeProfiles[0].id}</echo>
                        </tasks>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

But this is the result that I obtain:

main:
 [echo] current active profile: ${project.activeProfiles[0].id}

Any suggestion will be appreciated.

Thanks.

Answer

FrVaBe picture FrVaBe · Dec 13, 2016

The maven-help-plugin offers what you need. It has an active-profiles goal.

You can add it to your pom or even call it from the command line (include it in your maven build call). The How can I tell which profiles are in effect during a build? section of the Maven profile introduction page will show you how. In short:

mvn help:active-profiles

As this does not work for you (see comments) here is another solution:

I think the active profiles (there can be more than one!) are not propagated as available variables - but properties are.

So set a custom property in the profile section and use that, like

<profiles>
    <profile>
        <id>default</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <myProfile>default</myProfile>
        </properties>
    </profile>
    <profile>
        <id>debug</id>
        <activation>
            <property>
                <name>debug</name>
            </property>
        </activation>
        <properties>
            <myProfile>debug</myProfile>
        </properties>
    </profile>
</profiles>

<build>
    <plugins>
        <plugin>
            <artifactId>maven-antrun-plugin</artifactId>
            <executions>
                <execution>
                    <phase>generate-resources</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                    <configuration>
                        <tasks>
                            <echo>current active profile: ${myProfile}</echo>
                        </tasks>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>