且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在子模块上执行Maven插件目标,但不在父模块上执行

更新时间:2022-02-25 01:37:19


根据默认生命周期绑定,包装 pom 的绑定是:


默认生命周期绑定 - 包装
pom


package       site:attach-descriptor  
install       install:install  
deploy        deploy:deploy


所以如果你的父POM有一个< packaging> pom< packaging> (这应该是评论中指出的情况)并且如果你将你的插件绑定到上面的其他阶段(参见生命周期参考全面列表),在父POM的构建过程中不会执行它们。

So if your parent POM has a <packaging>pom<packaging> (this should be the case as pointed out in a comment) and if you bind your plugins to other phases than those above (see the Lifecycle Reference for a comprehensive list), they won't be executed during the build of the parent POM.

(编辑:我的初步答案是错误的。如果您将插件目标绑定到特定阶段,它将在此期间触发阶段,无论项目的包装如何。默认生命周期绑定与它没有任何关系,它们只是默认的生命周期绑定。所有重要的是,插件绑定的阶段是 build lifecyle 。)

( My initial answer is just wrong. If you bind a plugin goal to a particular phase, it will be triggered during that phase, regardless of the packaging of the project. The Default Lifecycle Bindings don't have anything to do with that, they are just default lifecycle bindings. All what matters is if the phase to which the plugin is bound is part of the build lifecyle.)

正如您所指出的,您可以使用 pluginManagement 在父pom中使用来配置插件,但是如果你真的想在子模块中执行插件目标而在中不是父(你可能有充分的理由这样做,但大部分时间,插件对于没有任何内容的 pom 包装的模块没有多少效果) ,你必须在子元素中的 plugins 元素中引用插件。

As you pointed out, you can use the pluginManagement in the parent pom for the configuration of the plugin but if you really want to execute a plugin goal in children modules and not in the parent (you might have good reasons to do this but most of time, plugins won't have much effet on a module with a pom packaging that doesn't have any content), you'll have to reference plugins in the plugins element in the children.

应用于您的示例,父pom.xml可以定义以下规范:

Applied to your example, the parent pom.xml could define the following specifications:

<project>
  <packaging>pom</packaging>
  ...
  <modules>
    <module>child</module>
  </modules>
  ...
  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-jar-plugin</artifactId>
          <version>2.2</version>
          <executions>
            <execution>
              <id>my-execution-id</id>
              <phase>integration-test</phase>
              <goals>
                <goal>jar</goal>
              </goals>
            </execution>
          </executions>
        </plugin>
        ...
      </plugins>
    </pluginManagement>
  </build>
  ...
</project>

并且在每个孩子 pom.xml 中,只需要以下内容:

And in every child pom.xml, only the following is required:

<project>
  ...
  <build>
    ...
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
      </plugin>
    </plugins>
    ...
  </build>
</project>