[英]Execute Maven plugin goal on child modules, but not on parent
在多模块项目中,如何指定要在所有子模块中而不是在父项目中执行插件目标? 有<pluginManagement>
,但它只定义了执行的配置——子模块仍然需要引用插件来执行目标:
[...] 但是,这仅配置在子项的 plugins 元素中实际引用的插件。 ( POM 参考)
任何其他方式来实现这一目标?
更新:我根据 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>
<!-- ... -->
这仍然会为父项目生成一个 .jar,即使jar
目标绑定到integration-test
阶段。
根据
Default Lifecycle Bindings ,包装
pom
的绑定是:
默认生命周期绑定 - 打包 pom\n包站点:附加描述符\n安装安装:安装\n部署部署:部署\n
因此,如果您的父 POM 有一个
<packaging>pom<packaging>
(这应该是评论中指出的情况),并且如果您将插件绑定到上述阶段以外的其他阶段(请参阅
生命周期参考以获取完整列表),它们不会在父 POM 的构建期间执行。
(编辑:我最初的答案是错误的。如果您将插件目标绑定到特定阶段,它将在该阶段触发,无论项目的打包如何。 默认生命周期绑定与此无关,它们只是默认的生命周期绑定。重要的是插件绑定的阶段是否是构建生命周期的一部分。)
正如您所指出的,您可以在父 pom 中使用pluginManagement
来配置插件,但是如果您真的想在子模块中而不是在父模块中执行插件目标(您可能有充分的理由这样做,但大多数时间,插件不会对带有没有任何内容的pom
包装的模块产生太大影响),您必须在子组件的plugins
元素中引用插件。
应用于您的示例,父 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>
在每个子pom.xml
,只需要以下内容:
<project>
...
<build>
...
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
</plugin>
</plugins>
...
</build>
</project>
所描述的带有插件管理的解决方案当然是正确的,但在某些情况下它不适合。 假设您想在子模块中运行多个 jar:jar 目标,每个目标在每次执行时都配置有自己的设置(配置)。 或者一般来说,当您不想强制子 poms 显式触发插件时。
在这种情况下,对我有用的解决方案是在特定配置文件下定义父 pom 中的执行,并使其仅在子 pom 中激活,例如通过检查某些文件或属性的存在:
<profile>
<id>generate-dc</id>
<activation>
<file>
<exists>src/main/assembly/some.xml</exists>
</file>
</activation>
然后插件不会在父级中执行,但如果包含文件或设置某些属性,则会在所有子级中执行。
我有一个类似的要求在子级而不是父级 POM 中运行一些插件。 我通过在父 POM 中声明<skip>true</skip>
来实现这一点。
父 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>
子项目 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>
下面的配置对我有用。 在父和子 pom.xml 中添加插件。
家长:
<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>
孩子
<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>
我尝试了 Pascal 的答案,但对我不起作用。 子 pom 中引用的插件没有执行,我假设是因为它们没有构建阶段绑定。
此处的帖子描述了通过将插件绑定到执行 ID 和构建阶段来工作的解决方案: 如何覆盖默认绑定到 Maven 插件的阶段
我会向其他任何试图让它发挥作用的人推荐它。
在父项目的插件部分下使用<inherited>false</inherited>
。
请参阅此页面了解更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.