maven-enforcer-plugin和child pom

时间:2015-10-30 18:03:13

标签: maven parent-pom maven-enforcer-plugin

我正在尝试使用maven-enforcer-plugin来确保激活一个配置文件(requireActiveProfile规则)。请参阅此页面上的第二个示例:Require Active Profile

适用于单个pom文件。当我尝试将它与2个pom文件(父母和孩子)一起使用时,当我尝试构建子模块时,这不再起作用了。

我知道为什么它不起作用?

完整示例:

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

父pom文件<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>

儿童邮件文件<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>

现在我跑:

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

输出看起来像那样:

[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

当我在父pom上运行maven时,它可以工作。我错过了什么?

1 个答案:

答案 0 :(得分:10)

Maven Profiles有点令人困惑,但我会根据自己的经验冒险尝试。

配置文件 definitions 不是继承的,但它们的效果是。

如果您跳转到child目录并运行mvn -Pfirst help:active-profiles,您会看到父母个人资料的效果确实存在:< / p>

部分输出:

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

但您在那里看不到配置文件 definition

坏消息是,您的enforcer规则在这种情况下不起作用,除非每个孩子都定义了所需的配置文件。

好消息是,拥有这些新知识,您可以使用另一个 enforcer规则(requireProperty)来实现您的目标:

   <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>