Maven: How to print the current profile on the console?
Asked Answered
L

2

12

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.

Laboratory answered 13/12, 2016 at 9:10 Comment(1)
Why do you want to do specifically, that depends on the profile?Tambac
R
15

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>
Refresher answered 13/12, 2016 at 9:32 Comment(3)
Yes, but this is not what I need. I want to print the profile accessing it by a variable, because I want to do some operations based on the profile.Laboratory
Exactly, this is what I need! Thanks!Laboratory
Where is myProfile coming from? Where is it set?Compilation
Y
6

you can add the maven-help-plugin in your pom to display always the active profile

<build>
    <plugins>
        <!-- display active profile in compile phase -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-help-plugin</artifactId>
            <version>3.1.0</version>
            <executions>
                <execution>
                    <id>show-profiles</id>
                    <phase>compile</phase>
                    <goals>
                        <goal>active-profiles</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

    </plugins>
</build>

source: https://www.mkyong.com/maven/maven-profiles-example

Yaakov answered 18/12, 2019 at 12:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.