Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 550
Alessandro C Avatar asked Dec 13 '16 09:12

Alessandro C


2 Answers

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>
like image 114
FrVaBe Avatar answered Oct 23 '22 02:10

FrVaBe


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

like image 21
wiggin200 Avatar answered Oct 23 '22 03:10

wiggin200