您可以考虑以下方法,而不是创建自己的 Maven 插件,这可能会降低项目的可移植性和可维护性:
一个简单的父 pom 如下所示:
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.sample</groupId>
<artifactId>sample-maven-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<path.to.myexec>path</path.to.myexec>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.4.0</version>
<executions>
<execution>
<id>myExec</id>
<goals>
<goal>exec</goal>
</goals>
<phase>generate-sources</phase>
<configuration>
<executable>${path.to.myexec}\myExec</executable>
<arguments>
<argument>--foo=${basedir}/src/test/resources/test.xml</argument>
<argument>--output-directory=${project.build.directory}/generated-sources/output</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.10</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${project.build.directory}/generated-sources/output</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>myexec-profile</id>
<build>
<plugins>
<!-- optionally move here the configuration above -->
</plugins>
</build>
</profile>
</profiles>
</project>
注意我添加的path.to.myexec 属性,如果需要,将在子项目中覆盖,以指向正确的相对路径。
然后,一旦安装在您的机器中(或部署在您公司的 Maven 存储库中),就可以在任何相关的 Maven 项目中引用如下:
<parent>
<groupId>com.sample</groupId>
<artifactId>sample-maven-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
无需重新声明上面的插件配置及其详细方法。它们将自动成为默认构建的一部分,作为从该父级继承的构建配置的一部分。
如果满足以下条件,分析方法也可以是一种解决方案:
- 您希望重用已由不需要此配置的项目使用的现有父 pom
- 您并不总是需要在构建中使用此行为,而是希望按需激活它
在这种情况下,您可以通过以下方式激活它,例如:
mvn clean package -Pmyexec-profile
鉴于您已经相应地设置了父级,并且您已将上述配置移至配置文件中。
这种方法的优点:
- 比编写新的 maven 插件(需要编写、测试、维护、分发等)更轻松的选择
- 消费者模块更容易定制某些东西:在任何时候他们都可以将父级的配置作为例外覆盖
- 不那么脆弱:想象一下,如果其中一个插件的另一个版本提供了对您很重要的错误修复,那么配置 XML 很容易,更改自定义的 maven 插件等更不容易。
- 配置保持集中、透明地访问和进一步治理的入口点
- 更容易排除故障:消费者模块可以随时运行
mvn help: effective-pom 并查看合并后的完整有效 pom(作为父 pom 和当前 pom 的聚合)并检查它实际运行的内容
如何跳过某些模块中的父插件执行
仅在某些模块中执行此插件的简单(并且经常使用)方法,同时使父模块与其他模块相同:
- 定义一个新属性,我们称之为
skip.script.generation,默认值为true,在父pom中定义。
- 在上述插件的
skip 配置条目中使用此跳过属性。
- 仅在相关模块中重新定义属性并将其设置为
false。这将是他们的 pom.xml 文件所需的唯一配置,因此减少到一行(保持非常低的详细程度)。
exec-maven-plugin 提供了这样的skip 选项,不幸的是build-helper-maven-plugin 没有。但这并没有阻止我们。我们仍然可以跳过使用phase 元素的两个执行,将其设置为不存在的阶段,如none 并因此跳过它们。这是合适的,因为这两个执行实际上已经附加到同一阶段,generate-sources。
对于这种方法,让我们将新属性重命名为 script.generation.phase。
举个例子:
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.sample</groupId>
<artifactId>sample-maven-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<path.to.myexec>path</path.to.myexec>
<script.generation.phase>none</script.generation.phase>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.4.0</version>
<executions>
<execution>
<id>myExec</id>
<goals>
<goal>exec</goal>
</goals>
<phase>${script.generation.phase}</phase>
<configuration>
<executable>${path.to.myexec}\myExec</executable>
<arguments>
<argument>--foo=${basedir}/src/test/resources/test.xml</argument>
<argument>--output-directory=${project.build.directory}/generated-sources/output</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.10</version>
<executions>
<execution>
<phase>${script.generation.phase}</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${project.build.directory}/generated-sources/output</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>myexec-profile</id>
<build>
<plugins>
<!-- optionally move here the configuration above -->
</plugins>
</build>
</profile>
</profiles>
</project>
注意两个插件的<phase>${script.generation.phase}</phase> 更改。默认值为none,此属性在默认情况下有效地禁用了它们的执行。
在另一个模块中,您将拥有以下内容:
<parent>
<groupId>com.sample</groupId>
<artifactId>sample-maven-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<properties>
<script.generation.phase>generate-sources</script.generation.phase>
</properties>
没有别的了。那是。 Maven 在构建期间会重新定义某个模块的属性,并在从其父模块继承的配置中自动替换它,从而再次启用上述两个执行。