3

I have a multi-module maven project where the parent contains a profile definition, activates that profile (using activeByDefault), and the child is supposed to take into account the activated profile and modify its build accordingly. I have this setup because I want to skip certain parts of the child build (for development) by deactivating the profile. The problem is that the activation of the profile in the parent doesn't seem to propagate to the child. In order to reproduce this, I created a small parent/child project, where the child project's build outputs a message if the activated-by-default profile is active. I'm using maven 3.5.0

This is my parent pom.xml:

<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>
<groupId>profiletest</groupId>
<artifactId>parent</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<name>parent</name>
<url>http://maven.apache.org</url>

<modules>
    <module>../child</module>
</modules>
<profiles>
    <profile>
        <id>activated-by-default</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>
</profiles>

child pom.xml:

<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>
<groupId>profiletest</groupId>
<artifactId>child</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>child</name>
<url>http://maven.apache.org</url>

<parent>
    <groupId>profiletest</groupId>
    <artifactId>parent</artifactId>
    <version>1.0-SNAPSHOT</version>
    <relativePath>../parent/pom.xml</relativePath>
</parent>

<profiles>
    <profile>
        <id>activated-by-default</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>com.github.ekryd.echo-maven-plugin</groupId>
                    <artifactId>echo-maven-plugin</artifactId>
                    <version>1.2.0</version>
                    <executions>
                        <execution>
                            <phase>package</phase>
                            <goals>
                                <goal>echo</goal>
                            </goals>
                            <configuration>
                                <message>building with activated-by-default profile</message>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

When I run mvn help:active-profiles package, even though the activated-by-default profile is shown as active, its corresponding build section is not executed:

$ mvn  help:active-profiles package
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO] 
[INFO] parent
[INFO] child
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] Building parent 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-help-plugin:2.2:active-profiles (default-cli) @ parent ---
[INFO] 
Active Profiles for Project 'profiletest:parent:pom:1.0-SNAPSHOT': 
The following profiles are active:
   - activated-by-default (source: profiletest:parent:1.0-SNAPSHOT)
Active Profiles for Project 'profiletest:child:jar:1.0-SNAPSHOT': 
The following profiles are active:
   - activated-by-default (source: profiletest:parent:1.0-SNAPSHOT)
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] Building parent 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] Building child 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ child ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /date/mt/dev/projects/mvn-active-by-default/child/src/main/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ child ---
[INFO] No sources to compile
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ child ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /date/mt/dev/projects/mvn-active-by-default/child/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ child ---
[INFO] No sources to compile
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ child ---
[INFO] No tests to run.
[INFO] 
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ child ---
[WARNING] JAR will be empty - no content was marked for inclusion!
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO] 
[INFO] parent ............................................. SUCCESS [  0.001 s]
[INFO] child .............................................. SUCCESS [  0.528 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.201 s
[INFO] Finished at: 2017-10-11T09:24:38+03:00
[INFO] Final Memory: 13M/208M
[INFO] ------------------------------------------------------------------------
Mihai Tudor
  • 422
  • 4
  • 15
  • 1
    It would seem that the profile in the child is separate from the one in the parent and thus only the parent's profile is active. Do you have any reason to expect that the child profile would be treated as an extension of the parent profile or is that just an assumption? – D.B. Oct 15 '17 at 01:22
  • Related: [Inherited profiles in Maven](https://stackoverflow.com/questions/18868772/inherited-profiles-in-maven) – sleske Jul 30 '20 at 13:40

1 Answers1

1

The problem is that the activation of the profile in the parent doesn't seem to propagate to the child.

Yes, this will not work. The reason is: Profiles are not inherited (only their effects are).

Rather, profiles are handled and resolved before handling inheritance. This means that profiles in parent and child POM do not interact at all, even if they have the same id.

This is nicely explained in the aptly titled blog post Maven Profile Inheritance (okay, DISinheritance):

See, profiles in parent poms can be triggered by the building of a child. When activated, they are applied directly to the parent pom, prior to that parent being used for inheritance into the child. So the effects of an active profile in a parent pom may be felt by the child.

Sometimes this indirect inheritance works well. Sometimes, it’s just crack-addled.

So in your case, Maven will see the profile "activated-by-default" in the parent POM and will activate it, which does nothing because it is empty. It will also see a profile with the same id in the child POM, which also does nothing because it is not activated (unless you use -P on the command line). Finally, it will merge parent and child POM according to inheritance rules to create the effective POM, which will contain nothing from either profile.

Also see Maven bug MNG-5127, which describes the same problem.

sleske
  • 81,358
  • 34
  • 189
  • 227