Search code examples
mavenmaven-profiles

Why isn't the child pom picking the right profile?


I've stripped down this project to be as simple as possible. I'm using the maven-echo-plugin1. All this project does is to echo a particular statement depending whether or not the deluxe profile is active. If I am not using the deluxe profile, the project prints out This is the base configuration. If I am using the deluxe profile, the project prints out This is the deluxe configuration.

I have the following parent pom:

<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>name.weintraub</groupId>
    <artifactId>parent</artifactId>
    <packaging>pom</packaging>
    <version>1.0.0</version>

    <profiles>
        <profile>
            <id>deluxe</id>
            <activation>
                <property>
                    <name>deluxe</name>
                    <value>true</value>
                </property>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <groupId>com.soebes.maven.plugins</groupId>
                        <artifactId>maven-echo-plugin</artifactId>
                        <version>0.1</version>
                        <executions>
                            <execution>
                                <goals>
                                    <goal>echo</goal>
                                </goals>
                                <phase>validate</phase>
                                <configuration>
                                    <echos>
                                        <echo>This is the deluxe configuration</echo>
                                    </echos>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
    <build>
        <plugins>
            <plugin>
                <groupId>com.soebes.maven.plugins</groupId>
                <artifactId>maven-echo-plugin</artifactId>
                <version>0.1</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>echo</goal>
                        </goals>
                        <phase>validate</phase>
                        <configuration>
                            <echos>
                                <echo>This is the base configuration</echo>
                            </echos>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

If I run:

$ mvn validate

This will print out This is the base configuration.

If I run:

$ mvn -Pdeluxe validate

This will print out This is the deluxe configuration

So far so good:

I now create a child pom:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <properties>
        <deluxe>true</deluxe>
    </properties>
    <parent>
        <groupId>name.weintraub</groupId>
        <artifactId>parent</artifactId>
        <version>1.0.0</version>
    </parent>
    <groupId>name.weintraub</groupId>
    <artifactId>foo</artifactId>
    <version>1.0.0</version>
</project>

Note that I've set the property deluxe to true in the beginning of the pom.

When I run:

$ mvn validate

It prints out This is the base configuration.

If I run it like this:

mvn -Ddeluxe=true validate

It then prints This is the deluxe configuration. If I do

mvn -Pdeluxe validate

It prints out This is the deluxe configuration.

So, I can see that the child pom is picking up the parent profiles, and if I activate the profile on the command line either directly, or by using a property. However, even though I set the property in the pom itself, my project doesn't seem to pick up the parent's profile.

Why?


1 I'm using version 0.1 because that's the latest version in Maven Central, and this it was called maven-echo-plugin back in version 1.0. The documentation otherwise is the same.


Solution

  • Profiles can only be activated using system properties (i.e. commandline) / environment variables as well as JDK and OS, not by using pom properties. (see Introduction to Build Profiles: "Currently, this detection is limited to prefix-matching of the JDK version, the presence of a system property or the value of a system property.")

    So, your approach can't work that way. You might want to take a look at asymmetric profiles as an alternative.