在Maven中自动激活父插件

在Maven中自动激活父插件,maven,inheritance,Maven,Inheritance,是否有可能在停用的父POM中定义一个插件,并且当子POM继承该插件时,它会自动激活?据我所知,没有通用的解决方案。至少目前 一个想法(我没有尝试过,但可能会奏效)是在父pom.xml中定义一个不存在的执行目标,例如: <executions> <execution> <goals> <goal>noGoal</goal> </goals> </ex

是否有可能在停用的父POM中定义一个插件,并且当子POM继承该插件时,它会自动激活?

据我所知,没有通用的解决方案。至少目前

一个想法(我没有尝试过,但可能会奏效)是在父pom.xml中定义一个不存在的执行目标,例如:

<executions>
    <execution>
        <goals>
            <goal>noGoal</goal>
        </goals>
    </execution>
</executions>

诺戈尔
在每个孩子身上,你都重新定义了一个正确的目标


这个解决方案的问题(当然,如果可行的话)是您必须为每个子级重新定义插件配置。否则,它将不会被执行。

您可以在顶级pom声明一个插件,并告诉它跳过,然后告诉它不要在子级跳过。它不是完全自动的,但是覆盖详细程度非常低

父Pom,禁用插件,但声明所有配置:

<plugin>
    <groupid>org.apache.maven.plugins</groupid>
    <artifactid>maven-surefire-plugin</artifactid>
    <configuration>
        <skip>true</skip>
        ...lots more config...
        ...lots more config...
        ...lots more config...
    </configuration>
</plugin>

org.apache.maven.plugins
maven surefire插件
真的
…更多配置。。。
…更多配置。。。
…更多配置。。。
子Pom,启用插件:

<plugin>
    <groupid>org.apache.maven.plugins</groupid>
    <artifactid>maven-surefire-plugin</artifactid>
    <configuration>
        <skip>false</skip>
    </configuration>
</plugin>

org.apache.maven.plugins
maven surefire插件
假的

我猜您希望在父pom中配置插件,但只在继承的项目中使用它。Maven对此有一节-在pluginManagement中配置插件,但在需要时将其绑定到某个阶段,例如,在pluginManagement中省略阶段标记,但在继承的pom中指定它。

这并不完全是您想要的,但我认为它对您来说已经足够好了

如果在父级中的pluginManagement标记中声明插件,则该配置将由声明该插件的所有子项目继承

例如,在父级中,声明编译插件使用Java5进行测试编译

<pluginManagement>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <executions>
        <execution>
          <id>test-compile</id>
          <goals>
            <goal>testCompile</goal>
          </goals>
          <configuration>
            <source>1.5</source>
            <target>1.5</target>
          </configuration>
        </execution>
      </executions>
    </plugin>
  </plugins>
</pluginManagement>

org.apache.maven.plugins
maven编译器插件
测试编译
测试编译
1.5
1.5
然后在子级中,您只需声明编译器插件,即可继承父级的配置:

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

org.apache.maven.plugins
maven编译器插件

所以“siddhadev”是完全正确的。您可以使用给定id在父pom中定义插件配置:

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <artifactId>maven-antrun-plugin</artifactId>
                <executions>
                    <execution>
                        <id>child-caller</id>
                        <!-- 'phase' omitted -->
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <tasks>
                                <echo message="called from child!" />
                            </tasks>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

maven antrun插件
子呼叫者
跑
并且,在子POM中,您可以显式列出应该调用它的阶段:

<build>
    <plugins>
        <plugin>
            <artifactId>maven-antrun-plugin</artifactId>
            <executions>
                <execution>
                    <id>child-caller</id>
                    <phase>compile</phase>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

maven antrun插件
子呼叫者
编译

我用它来瞄准各种JRE。不幸的是,因为你不能使用Maven编译器插件,不同的目的目录(我认为插件中有一个bug),你必须使用Ant。< /P> < P>我用下面的解决方案:

  • 在pluginManagement部分的父pom中配置插件。将插件绑定到现有阶段

  • 通过将父pom的插件绑定到不存在的阶段来停用该插件:覆盖插件部分中的阶段

  • 通过将插件包含在插件部分,激活每个子pom中的插件

示例父pom:

<defaultGoal>install</defaultGoal>
    <pluginManagement>
        <plugins>
            ...
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-install-plugin</artifactId>
                <executions>
                    <execution>
                        <id>install-ejb-client</id>
                        <phase>install</phase>
                        <goals>
                        <goal>install-file</goal>
                        </goals>
                        <configuration>
                            <file>${ejb-client-file}</file>
                            <groupId>${project.groupId}</groupId>
                            <artifactId>${project.artifactId}</artifactId>
                            <version>${project.version}</version>
                            <packaging>jar</packaging>
                            <classifier>client</classifier>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
                    ...                 
        </plugins>
    </pluginManagement>
    <plugins>
            <plugin>
                <!-- deactivate the plugin for this project, only child-projects do generate ejb-clients -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-install-plugin</artifactId>
                <inherited>false</inherited>
                <executions>
                    <execution>
                        <id>install-ejb-client</id>
                        <phase>none</phase>
                    </execution>
                </executions>
            </plugin>
            ...
    </plugins>
</build>
安装
...
org.apache.maven.plugins
maven安装插件
安装ejb客户端
安装
安装文件
${ejb客户端文件}
${project.groupId}
${project.artifactId}
${project.version}
罐子
客户
...                 
org.apache.maven.plugins
maven安装插件
假的
安装ejb客户端
没有一个
...
示例子pom:

<build>
    <plugins>
            ...
        <plugin>
            <!-- Install the generated client-jar. Property 'ejb-client-file' has to be set! Plugin configuration is in the parent pom -->
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-install-plugin</artifactId>
        </plugin>       
            ... 
    </plugins>
</build>

...
org.apache.maven.plugins
maven安装插件
... 

是的,我也考虑过类似的事情,但正如你所说的,这种方法的缩小规模是我必须在每个孩子身上配置它,这是我不想做的。不幸的是,只有当相应的插件提供“跳过”作为配置时,这才有效。不是所有的插件都可以!这比Rich Steller的答案()好多少?与仅使用pluginManagement部分相比,这种方法有什么好处?@JimHurne如果您想选择哪些执行,则需要在子pom中指定阶段,如果您想始终这样做,则应该在父pom中指定阶段。