Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

maven-enforcer-plugin and child pom

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?

like image 541
Jmini Avatar asked Oct 30 '15 18:10

Jmini


People also ask

What is the purpose of Maven enforcer plugin?

The Enforcer plugin provides goals to control certain environmental constraints such as Maven version, JDK version and OS family along with many more built-in rules and user created rules.

How do I disable Enforcer plugin?

We can use the skip parameter to disable the plugin. If we take a look at the documentation for the maven-enforcer-plugin, we can see that it has a skip parameter that we can implement. Support for the skip parameter should be the first thing we check because it is the simplest solution and the most conventional.

What is a banned dependency?

This rule checks the dependencies and fails if any of the matching excludes are found. The following parameters are supported by this rule: searchTransitive - if transitive dependencies should be checked. Default is true.


1 Answers

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>
like image 71
Daniel Avatar answered Oct 23 '22 00:10

Daniel