maven-enforcer-plugin和child pom

Jmi*_*ini 7 maven maven-enforcer-plugin parent-pom

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

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

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


完整的例子:

EXAMPLE
|   pom.xml <1> (parent pom)
|
\---child
        pom.xml <2> (child pom)
Run Code Online (Sandbox Code Playgroud)

父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>
Run Code Online (Sandbox Code Playgroud)

儿童pom文件<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>
Run Code Online (Sandbox Code Playgroud)

现在我跑:

EXAMPLE>cd child
EXAMPLE\child>mvn compile -Psecond
Run Code Online (Sandbox Code Playgroud)

输出看起来像这样:

[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
Run Code Online (Sandbox Code Playgroud)

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

Dan*_*iel 11

Maven Profiles有点令人困惑,但我会根据自己的经验冒险解释.

概要文件定义不是继承的,但它们的作用是.

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

部分输出:

...
<properties>
  <my-name>Alice</my-name>
</properties>
...
Run Code Online (Sandbox Code Playgroud)

但你不会在那里看到配置文件定义.

坏消息是您的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>
Run Code Online (Sandbox Code Playgroud)

  • 使用`requireProperty`正好是我解决这个问题的方法.感谢您确认并解释为什么`requireActiveProfile`在我的情况下不可用. (3认同)