ChatGPT解决这个技术问题 Extra ChatGPT

禁用父 POM 中定义的 Maven 插件

我正在使用一个父 POM,它定义了一个我不想在子 POM 中运行的插件。如何完全禁用子 pom 中的插件?

约束:我无法更改父 POM 本身。


A
Alex

在子 POM 中禁用 Findbugs 时,以下对我有用:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>findbugs-maven-plugin</artifactId>
    <executions>
        <execution>
            <id>ID_AS_IN_PARENT</id> <!-- id is necessary sometimes -->
            <phase>none</phase>
        </execution>
    </executions>
</plugin>

注意:Findbugs 插件的完整定义在我们的父/超级 POM 中,所以它会继承版本等等。

在 Maven 3 中,您需要使用:

 <configuration>
      <skip>true</skip>
 </configuration>

对于插件。


虽然这是“正确的”,即它可以工作,但应该注意它是一个未指定(或至少未记录)的功能。没有称为“无”的官方阶段。所以,你不妨把'foo'放在那里。
对 Maven 3 中的我来说,这是行不通的。 true 喜欢 bmargulies 建议的作品
我必须添加父 POM 的 <id>…</id> 部分,然后它对我有用。
Maven 3 解决方案并没有真正禁用该插件,是吗?根据输出,插件仍在执行。然后它是否尊重跳过配置,以及它选择跳过的方式/内容,似乎取决于各个插件。
mirabilos 的评论是 Maven 3 的正确解决方案,并且可以跨所有插件移植。并非所有插件都有 <skip> 参数。
b
bmargulies

查看插件是否具有“跳过”配置参数。几乎所有人都这样做。如果是这样,只需将其添加到孩子的声明中:

<plugin>
   <groupId>group</groupId>
   <artifactId>artifact</artifactId>
   <configuration>
     <skip>true</skip>
   </configuration>
</plugin>

如果没有,那么使用:

<plugin>    
<groupId>group</groupId>   
 <artifactId>artifact</artifactId>    
<executions>
     <execution>
       <id>TheNameOfTheRelevantExecution</id>
       <phase>none</phase>
     </execution>    
</executions>  
</plugin>

如何命名您正在使用的插件,并运行 help:effective-pom 以查看您是否真的执行正确。
还要注意插件与插件管理。后者覆盖前者。
我正在使用 Cobertura 插件,我不想在子 pom 中运行它。
检查目标在 2.5 中有一个跳过。以前没有。 cobertura 目标没有。
对于那些使用 Tiles Maven 插件 并考虑使用第二个选项(通过 ID 覆盖执行)的人的重要说明:在 tile 内的插件 changes execution IDs 以反映 tile GAV 坐标。因此,为了使覆盖起作用,<configuration tiles-keep-id="true" /> 标记应添加到执行中以保留指定的执行 ID。
A
Alex

线程很旧,但也许有人仍然感兴趣。我发现的最短形式是对 λlex 和 bmargulies 示例的进一步改进。执行标签将如下所示:

<execution>
    <id>TheNameOfTheRelevantExecution</id>
    <phase/>
</execution>

我要强调的两点:

阶段设置为空,看起来没有“无”那么骇人听闻,尽管仍然是骇客。 id 必须与您要覆盖的执行相同。如果你没有指定 id 来执行,Maven 会隐式地执行它(以一种你不希望直观的方式)。

发布后发现它已经在stackoverflow中:In a Maven multi-module project, how can I disable a plugin in one child?


作为记录:默认执行 ID 遵循此答案中列出的简单规则:stackoverflow.com/a/34599117/7641
值得注意的是,这个解决方案实际上禁用了(如 OP 所要求的)插件(对于给定的执行 ID),而不是依赖于插件特定的“跳过”选项。
L
Liviu Ilea

我知道这个线程真的很老,但@Ivan Bondarenko 的解决方案在我的情况下帮助了我。

我的 pom.xml 中有以下内容。

<build>
    ...
    <plugins>
         <plugin>
                <groupId>com.consol.citrus</groupId>
                <artifactId>citrus-remote-maven-plugin</artifactId>
                <version>${citrus.version}</version>
                <executions>
                    <execution>
                        <id>generate-citrus-war</id>
                        <goals>
                            <goal>test-war</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
    </plugins>
</build>

我想要的是为特定配置文件禁用 generate-citrus-war 的执行,这就是解决方案:

<profile>
    <id>it</id>
    <build>
        <plugins>
            <plugin>
                <groupId>com.consol.citrus</groupId>
                <artifactId>citrus-remote-maven-plugin</artifactId>
                <version>${citrus.version}</version>
                <executions>
                    <!-- disable generating the war for this profile -->
                    <execution>
                        <id>generate-citrus-war</id>
                        <phase/>
                    </execution>

                    <!-- do something else -->
                    <execution>
                        ...
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</profile>