在子模块上执行Maven插件目标,但不在父模块上执行 [英] Execute Maven plugin goal on child modules, but not on parent

查看:186
本文介绍了在子模块上执行Maven插件目标,但不在父模块上执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在多模块项目中,如何指定要在所有子模块中执行插件目标,而不是在父项目中执行?有< pluginManagement> ,但这只定义了执行的配置 - 子模块仍然需要引用插件才能执行目标:

In a multi-module project, how can you specify that you want to execute a plugin goal in all the child-modules, but not on the parent project? There is <pluginManagement>, but that only defines the configuration for the execution -- the child modules would still need to reference the plugin to get the goal executed:


[...]但是,这只配置了子节点中plugins元素中实际引用的插件。 POM参考

任何其他方法可以实现这一目标吗?

Any other way to achieve this?

更新:我试过了根据Pascal的建议:

UPDATE: I've tried this according to Pascal's advice:

<!-- ... -->
<packaging>pom</packaging>
<modules>
  <module>child</module>
</modules>

<build>
  <plugins>
    <plugin>
      <artifactId>maven-jar-plugin</artifactId>
      <executions>
        <execution>
        <phase>integration-test</phase>
        <goals>
          <goal>jar</goal>
        </goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>
<!-- ... -->

这仍将为父项目生成.jar,即使 jar 目标绑定到 integration-test 阶段。

This will still generate a .jar for the parent project, even though the jar goal is bound to the integration-test phase.

推荐答案


根据默认生命周期绑定,包装 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 包装的模块没有多少效果) ,你必须在子元素中的 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>

这篇关于在子模块上执行Maven插件目标,但不在父模块上执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆