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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1625492/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 17:58:06  来源:igfitidea点击:

Execute Maven plugin goal on child modules, but not on parent

javamaven-2

提问by Lóránt Pintér

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:

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

[...] However, this only configures plugins that are actually referenced within the plugins element in the children. (POM Reference)

[...] 但是,这仅配置在子项的 plugins 元素中实际引用的插件。POM 参考

Any other way to achieve this?

任何其他方式来实现这一目标?

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

更新:我根据 Pascal 的建议尝试过这个:

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

This will still generate a .jar for the parent project, even though the jargoal is bound to the integration-testphase.

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

回答by Pascal Thivent

According to the Default Lifecycle Bindings, the bindings for a packaging pomare:

根据Default Lifecycle Bindings,包装的绑定pom是:

Default Lifecycle Bindings - Packaging pom

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

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

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

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 Referencefor a comprehensive list), they won't be executed during the build of the parent POM.

因此,如果您的父 POM 有一个<packaging>pom<packaging>(这应该是评论中指出的情况)并且如果您将插件绑定到上述阶段以外的其他阶段(请参阅生命周期参考以获得完整列表),它们将不会在父 POM 的构建。

(EDIT: 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 Bindingsdon'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.)

(编辑:我最初的答案是错误的。如果您将插件目标绑定到特定阶段,它将在该阶段触发,无论项目的打包如何。默认生命周期绑定与此无关,它们只是默认的生命周期绑定。重要的是插件绑定的阶段是否是构建生命周期的一部分。)

As you pointed out, you can use the pluginManagementin the parent pom for the configuration of the plugin but if you really want to execute a plugin goal in children modules and notin 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 pompackaging that doesn't have any content), you'll have to reference plugins in the pluginselement in the children.

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

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

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

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

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

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

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

回答by Mikhail

The described solution with plugin management is certainly correct, but in certain cases it does not fit. Suppose you would like to run several jar:jar goals in the child module each configured with its own settings (configuration) per execution. Or in general, when you don't want to force child poms to explicitly trigger the plugin(s).

所描述的带有插件管理的解决方案当然是正确的,但在某些情况下它不适合。假设您想在子模块中运行多个 jar:jar 目标,每个目标在每次执行时都配置有自己的设置(配置)。或者一般来说,当您不想强制子 poms 显式触发插件时。

In this case the solution that worked for me was to define the executions in the parent pom under a specific profile, and have it activated only in child poms for example by checking for existence of some file or property:

在这种情况下,对我有用的解决方案是在特定配置文件下定义父 pom 中的执行,并使其仅在子 pom 中激活,例如通过检查某些文件或属性的存在:

<profile>
    <id>generate-dc</id>
    <activation>
        <file>
            <exists>src/main/assembly/some.xml</exists>
        </file>
    </activation>

Then plugins won't be executed in the parent, but will be executed in all children if those contain the file, or set some property.

然后插件不会在父级中执行,但如果包含文件或设置某些属性,则会在所有子级中执行。

回答by Rafeeq

I had a similar requirement to run some plugins in the child but not the parent POM. i achieved this by stating <skip>true</skip>in the parent POM.

我有一个类似的要求在子级而不是父级 POM 中运行一些插件。我通过<skip>true</skip>在父 POM 中声明来实现这一点。

The parent pom entry is below:

父 pom 条目如下:

<plugin>
    <groupId>eviware</groupId>
    <artifactId>maven-soapui-plugin</artifactId>
    <version>4.0.0</version>
    <inherited>false</inherited>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.8.2</version>
        </dependency>
    </dependencies> 
    <configuration>
        <skip>true</skip>
    </configuration>
</plugin> 

The child project pom entry is below

子项目 pom 条目如下

<plugins>
    <plugin>
        <groupId>eviware</groupId>
        <artifactId>maven-soapui-plugin</artifactId>
        <version>4.0.0</version>
        <configuration>
            <settingsFile>site-service-web/src/test/soapui/soapui-settings.xml</settingsFile>
            <projectFile>site-service-web/src/test/soapui/PodifiSite-soapui-project.xml</projectFile>
            <outputFolder>site-service-web/target/surefire-reports</outputFolder>
            <junitReport>true</junitReport>
            <exportwAll>true</exportwAll>
            <printReport>true</printReport>
        </configuration>
    </plugin>
</plugins>

回答by Michael Siegel

I tried the answer from Pascal but it did not work for me. The plugins referenced in the child pom did not execute, I'm assuming because they did not have a build phase binding.

我尝试了 Pascal 的答案,但对我不起作用。子 pom 中引用的插件没有执行,我假设是因为它们没有构建阶段绑定。

The post here describes a solution that works by binding the plugins to execution ids and build phases: How to override default binding to phase of a Maven plugin

此处的帖子描述了通过将插件绑定到执行 ID 和构建阶段来工作的解决方案:如何覆盖默认绑定到 Maven 插件的阶段

I'd recommend that to anyone else trying to get this working.

我会向其他任何试图让它发挥作用的人推荐它。

回答by Y L

Use <inherited>false</inherited>under the plugins section in the parent project.

<inherited>false</inherited>在父项目的插件部分下使用。

Plese refer to this pagefor more information.

请参阅此页面了解更多信息。

回答by lakshmanas

This below config worked for me. Add the plugin in both the parent and child pom.

下面的配置对我有用。在父和子 pom.xml 中添加插件。

Parent :

家长:

<build>
  <plugins>
    <plugin>
      <artifactId>maven-jar-plugin</artifactId>
      <inherited>true</inherited>
      <executions>
        <execution>
        <phase>integration-test</phase>
        <goals>
          <goal>jar</goal>
        </goals>
        </execution>
      </executions>
      <configuration>
         <skip>true</skip>
      </configuration>
    </plugin>
  </plugins>
</build>

Child

孩子

<build>
  <plugins>
    <plugin>
      <artifactId>maven-jar-plugin</artifactId>
      <inherited>false</inherited>
      <executions>
        <execution>
        <phase>integration-test</phase>
        <goals>
          <goal>jar</goal>
        </goals>
        </execution>
      </executions>
      <configuration>
         <skip>false</skip>
      </configuration>
    </plugin>
  </plugins>
</build>