简体   繁体   English

如何触发 Maven 插件的特定执行?

[英]How to trigger a specific execution of a Maven plugin?

I am trying to figure out whether it is possible to define executions of a Maven plugin in a <pluginManagement> section of a parent pom and pick a specific execution, and only that one, in a child project.我试图弄清楚是否可以在父 pom 的<pluginManagement>部分定义 Maven 插件的执行,并在子项目中选择一个特定的执行,并且只有那个执行。

To be more specific, I have several multi-module projects that inherit from our company-wide parent pom.更具体地说,我有几个从我们公司范围内的父 pom 继承的多模块项目。 In the <pluginManagement> section of the parent pom, I have several executions of maven-resources-plugin using the copy-resources goal, all bound to phase validate but using different configurations:在父 pom 的<pluginManagement>部分,我使用copy-resources目标执行了几次 maven-resources-plugin,都绑定到阶段验证但使用不同的配置:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <inherited>true</inherited>
    <executions>
        <execution>
            <id>copy-codedeploy</id>
            <phase>validate</phase>
            ...
        </execution>
        <execution>
            <id>copy-settings</id>
            <phase>validate</phase>
            ...
        </execution>

        <execution>
            <id>copy-logback</id>
            <phase>validate</phase>
            ...
        </execution>
    </executions>
<plugin>

After experimenting many hours on a child project, I would like to:在一个子项目上试验了很多小时后,我想:

  1. Avoid having needless executions of copy-logback in all modules, which happens when I define execution copy-logback in the parent POM and has the drawback of creating the destination folder hierarchy in all modules.避免在所有模块中不必要地执行copy-logback ,当我在父 POM 中定义执行copy-logback时会发生这种情况,并且具有在所有模块中创建目标文件夹层次结构的缺点。
  2. Avoid, in case that execution is not defined in the parent POM, defining it in multiple cloned copies in all modules where it is needed.避免在父 POM 中未定义执行的情况下,在需要它的所有模块中在多个克隆副本中定义它。

An example child project has these modules, where the first one is the only module where copy-logback is really needed:一个示例子项目有这些模块,其中第一个是唯一真正需要copy-logback 的模块:

  • webapp网络应用程序
  • rest休息
  • services服务
  • persistence坚持

The best I have arrived at now is to leave common executions in the parent POM, which gives me item #1 in the first list above, but still leaves me cloning the exact same plugin configuration in multiple modules.我现在最好的方法是在父 POM 中保留常见的执行,这给了我上面第一个列表中的第 1 项,但仍然让我在多个模块中克隆完全相同的插件配置。 The configuration that I'm cloning is this:我克隆的配置是这样的:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <executions>
        <!-- Copy logback.xml from the parent to the main resources folder, filtering the application name used in log file names. -->
        <execution>
            <id>copy-logback-file</id>
            <phase>validate</phase>
            <goals>
                <goal>copy-resources</goal>
            </goals>
            <configuration>
                <outputDirectory>${project.basedir}/src/main/resources</outputDirectory>
                <resources>
                    <resource>
                        <directory>${project.basedir}/../logback-templates</directory>
                        <filtering>true</filtering>
                        <includes>
                            <include>logback.xml</include>
                        </includes>
                    </resource>
                </resources>
            </configuration>
        </execution>
    </executions>
</plugin>

This unfortunately diminishes the usefulness of a <pluginManagement> section in the parent POM as the identical configuration cannot be defined only once.不幸的是,这降低了父 POM 中<pluginManagement>部分的用处,因为相同的配置不能只定义一次。 I have to define it in each module of our Portfolio that uses logback, which amounts to tens of times.我必须在我们 Portfolio 的每个使用 logback 的模块中定义它,这相当于数十倍。

To solve my duplication problem, I am thinking that maybe, it's possible to define an execution of a plugin that can be selectively triggered where it's needed.为了解决我的重复问题,我想也许可以定义一个插件的执行,该插件可以在需要时选择性地触发。 Is it possible?是否可以?

You can add a skip parameter to the configuration of each execution like:您可以在每次执行的配置中添加一个跳过参数,例如:

<skip>${copy.logback.skip}</skip>

And then you can choose in each module whether to use or skip the execution by setting the property <copy.logback.skip> to true or false .然后您可以通过将属性<copy.logback.skip>truefalse在每个模块中选择是使用还是跳过执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM