maven-enforcer-plugin and child pom
Asked Answered
Z

1

11

I am trying to use the maven-enforcer-plugin to ensure that one profile is activated (requireActiveProfile rule). See the second example on this page: Require Active Profile

It works well with a single pom file. When I try to use it with 2 pom files (a parent and a child) this do not work anymore when I try to build the child module.

I have any idea why it doesn’t work?


Complete example:

EXAMPLE
|   pom.xml <1> (parent pom)
|
\---child
        pom.xml <2> (child pom)

Parent pom file <1>:

<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.mycompany.app</groupId>
  <artifactId>my-app.parent</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>pom</packaging>

  <profiles>
    <profile>
      <id>first</id>
      <properties>
        <my-name>Alice</my-name>
      </properties>
    </profile>
    <profile>
      <id>second</id>
      <properties>
        <my-name>Bob</my-name>
      </properties>
    </profile>
  </profiles>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-enforcer-plugin</artifactId>
        <version>1.4.1</version>
        <executions>
          <execution>
            <id>enforce-first-or-second-profile-is-active</id>
            <goals>
              <goal>enforce</goal>
            </goals>
            <configuration>
              <rules>
                <requireActiveProfile>
                  <profiles>first,second</profiles>
                  <all>false</all>
                </requireActiveProfile>
              </rules>
              <fail>true</fail>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-antrun-plugin</artifactId>
        <executions>
          <execution>
            <phase>compile</phase>
            <goals>
              <goal>run</goal>
            </goals>
            <configuration>
              <tasks>
                <echo>Hello ${my-name}!</echo>
              </tasks>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

Child pom file <2>:

<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <parent>
    <groupId>com.mycompany.app</groupId>
    <artifactId>my-app.parent</artifactId>
    <version>1.0-SNAPSHOT</version>
  </parent>

  <artifactId>my-app</artifactId>
  <packaging>pom</packaging>

</project>

Now I run:

EXAMPLE>cd child
EXAMPLE\child>mvn compile -Psecond

And the output looks like that:

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building my-app 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-enforcer-plugin:1.4.1:enforce (enforce-first-or-second-profile-is-active) @ my-app ---
[WARNING] Rule 0: org.apache.maven.plugins.enforcer.RequireActiveProfile failed with message:
Profile "first" is not activated.
Profile "second" is not activated.

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.178 s
[INFO] Finished at: 2015-10-30T18:03:50+01:00
[INFO] Final Memory: 24M/989M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-enforcer-plugin:1.4.1:enforce (enforce-first-or-second-profile-is-active) on project my-app: Some Enforcer rules have failed. Look above for specific messages explaining why the rule failed. -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

When I run maven on the parent pom, it works. What did I miss?

Zalea answered 30/10, 2015 at 18:3 Comment(2)
Wiithin a multi module you should call the child by changing into it's directory...just call it from root via mvn -pl child compile -PsecondNicolette
The problem isn't the multi-module setup, my problem is about maven-enforcer-plugin using the requireActiveProfile rule and child POM.Zalea
A
15

Maven Profiles are a bit confusing, but I'll venture an explanation based on my experiences.

The profile definitions is not inherited, but the effects of them are.

If you jump to the child directory and run mvn -Pfirst help:active-profiles you'll see that the effect of the parent's profile is indeed there:

Part of the output:

...
<properties>
  <my-name>Alice</my-name>
</properties>
...

but you won't see the profiles definition in there.

The bad news is that your enforcer rule won't work in this scenario, unless every child also defines the required profiles.

The good news is that, armed with this new knowledge, you can use another enforcer rule (requireProperty) to achieve what you want:

   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-enforcer-plugin</artifactId>
    <version>1.4.1</version>
    <executions>
      <execution>
        <id>enforce-property</id>
        <goals>
          <goal>enforce</goal>
        </goals>
        <configuration>
          <rules>
            <requireProperty>
              <property>my-name</property>
              <message>You must set a my-name property! Did you activate the right profile?</message>
            </requireProperty>
          </rules>
          <fail>true</fail>
        </configuration>
      </execution>
    </executions>
  </plugin>
Arguseyed answered 30/10, 2015 at 19:58 Comment(2)
Using requireProperty is exactly already my workaround for this problem. Thank you for confirming it and explaining why requireActiveProfile is not usable in my case.Zalea
Thanks for this... this workaround is the best option I found... I've declared my "dev-env" profile with "<activeByDefault>true</activeByDefault>" and the other profiles ("dev-test" and "dev-prod") with a property called "<deployable>true</deployable>"... now I can execute my build with "mvn clean install -P dev-test" and it will work correctly... it's important to note that now I can't execute "mvn clean install" for the default "env-dev" profile... but that's no problem as I do not need it to run my project locally... in that case, I can just comment the plugin... thanks!Tibold

© 2022 - 2024 — McMap. All rights reserved.