在父pom中定义Maven插件,但只调用子项目中的插件 [英] Define Maven plugins in parent pom, but only invoke plugins in child projects

查看:37
本文介绍了在父pom中定义Maven插件,但只调用子项目中的插件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组项目,它们都需要在构建过程中运行相同系列的 Maven 插件执行.我想避免在每个项目中重新声明所有这些配置,所以我让它们都从父 pom模板"项目继承,该项目只包含那些插件执行(8 个不同的 mojo).但我希望这些插件执行只在子项目上运行,而不是在 Maven 构建期间在父项目上运行.

I have a set of projects which all need to run the same series of Maven plugin executions during their builds. I'd like to avoid re-declaring all of this configuration in every project, so I made them all inherit from a parent pom "template" project which only contains those plugin executions (8 different mojos). But I want those plugin executions to only run on the child projects and not on the parent project during Maven builds.

我尝试通过四种不同的方式来完成这件事,每一种都有我不喜欢的副作用.

I've tried to accomplish this four different ways, each with a side-effect I don't like.

  1. 在父 pom 的 build/plugins 元素中声明插件执行,并使用 properties-maven-plugin 来打开其他插件的skip属性 在父项目中.这不起作用,因为插件目标之一 (maven-dependency-plugin:build-classpath) 没有 skip 属性.

  1. Declare the plugin executions in the parent pom's build/plugins element and use properties-maven-plugin to turn on the skip properties on other plugins in the parent project. This didn't work because one of the plugin goals (maven-dependency-plugin:build-classpath) doesn't have a skip property.

在父 pom 的 build/pluginManagement 元素中声明插件执行.不幸的是,这需要我重新声明每个子项目 pom 的 build/plugins 元素中的八个插件中的每一个,例如:

Declare the plugin executions in the parent pom's build/pluginManagement element. Unfortunately this requires me to redeclare each of the eight plugins in the build/plugins element of every child project's pom like:

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
</plugin>
...

这太重复了,如果我需要更改模板 pom 中的插件,就会出现问题.

This is too repetitive, and problematic if I ever need to change the plugins in the template pom.

在父 pom 的配置文件中声明插件执行,该文件由 nobuild.txt 文件(它确实存在于父 pom 中)的缺失激活pom,所以插件不会在那里执行):

Declare the plugin executions in a profile in the parent pom which is activated by the lack of a nobuild.txt file (which does exist in the parent pom, so the plugins don't execute there):

<profiles>
    <profile>
        <activation>
            <file>
                <missing>nobuild.txt</missing>
            </file>
        </activation>
        <build>
            ....
        </build>
    </profile>
</profiles>

这在大多数情况下都有效,除了missing 元素中的文件路径似乎基于当前工作目录而不是项目 basedir.这打破了我希望能够做的一些多模块构建.澄清一下,父模板"项目本身实际上是多模块项目中的一个模块,当我尝试执行mvn install 在根上.项目结构如下:

This works for the most part, except that the file path in the missing element seems to be based on the current working directory instead of the project basedir. This breaks some of the multimodule builds I'd like to be able to do. to clarify, the parent "template" project is actually itself a module in a multimodule project, and the build breaks when I try, for instance, to do a mvn install on the root. The project structure looks like:

+ job
|- job-core
|- job-template
|- job1                   inherits from job-template
|- job2                   inherits from job-template

  • 设置自定义生命周期和包装.这似乎允许我将插件绑定到生命周期阶段,但不指定任何配置.

    那么,是否有另一种方法来指定一组可在多个项目中重复使用的 Maven 插件执行(在每个项目的 poms 中重复最少)?

    So, is there another way to specify a bunch of Maven plugin executions that can be reused across several projects (with minimal repetition in each of those projects' poms)?

    推荐答案

    我最终编写了自己的插件,该插件利用了 mojo-executor 调用其他 mojo.这使我能够 1) 集中构建配置,以及 2) 最大限度地减少在每个子项目中重复的配置量.

    I ended up writing my own plugin which utilizes mojo-executor to invoke other mojos. This allows me to 1) centralize the build configuration and 2) minimize the amount of configuration that gets duplicated in each of the child projects.

    (如果你对这一切的原因感到好奇:每个子项目都是一个将从命令行执行的作业.构建设置一个调用程序 shell 脚本并将其附加到构建中它会被检入我们的工件存储库.稍后部署脚本会将它们拉到它们将在其上运行的机器上.)

    模板项目pom的相关部分:

    Relevant parts of the template project's pom:

    <project ...>
        <parent> ... </parent>
        <artifactId>job-template</artifactId>
        <packaging>pom</packaging>
        <name>job project template</name>
        <build>
            <pluginManagement>
                <plugin>
                    <groupId>...</groupId>
                    <artifactId>job-maven-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>generate-sources-step</id>
                            <goals><goal>job-generate-sources</goal></goals>
                        </execution>
                        <execution>
                            <id>package-step</id>
                            <goals><goal>job-package</goal></goals>
                        </execution>
                        ... (a couple more executions) ...
                    </executions>
                </plugin>
            </pluginManagement>
        </build>
    </project>
    

    必须创建一个新的 maven-plugin 项目(job-maven-plugin).Pom 看起来像:

    Had to create a new maven-plugin project (job-maven-plugin). Pom looks like:

    <project ...>
        <parent> ... </parent>
        <artifactId>job-maven-plugin</artifactId>
        <packaging>maven-plugin</packaging>
        <name>job maven executor plugin</name>
        <dependencies>
            <dependency>
                <groupId>org.twdata.maven</groupId>
                <artifactId>mojo-executor</artifactId>
                <!-- version 1.5 supports Maven 2, while version 2.0 only supports Maven 3 -->
                <version>1.5</version>
            </dependency>
        </dependencies>
    </project>
    

    正如您从模板项目中看到的,我的插件中有多个 mojo(每个阶段一个需要发生的事情).例如,job-package mojo 绑定到 package 阶段,并使用 mojo-executor 库运行另外两个 mojo(只附加了一些构建工件):

    As you can see from the template project, there were multiple mojos in my plugin (one per phase that needed stuff to happen). As an example, the job-package mojo is bound to the package phase and uses the mojo-executor library to run two other mojos (which just attach some build artifacts):

    /**
     * @goal job-package
     * @phase package
     */
    public class PackageMojo extends AbstractMojo {
        /**
         * @parameter expression="${project}"
         * @required
         * @readonly
         */
        protected MavenProject project;
        /**
         * @parameter expression="${session}"
         * @required
         * @readonly
         */
        protected MavenSession session;
        /**
         * @component
         * @required
         */
        protected PluginManager pluginManager;
    
        @Override
        public void execute() throws MojoExecutionException, MojoFailureException {
            ExecutionEnvironment environment = executionEnvironment(project, session, pluginManager);
    
            // Attach script as a build artifact
            executeMojo(
                plugin(
                    groupId("org.codehaus.mojo"),
                    artifactId("build-helper-maven-plugin"),
                    version("1.7")
                ),
                goal("attach-artifact"),
                configuration(
                    element("artifacts",
                        element("artifact",
                            element("file", "${project.build.directory}/script.shl"),
                            element("type", "shl")
                        )
                    )
                ),
                environment
            );
    
            // Zip up the jar and script as another build artifact
            executeMojo(
                plugin(
                    groupId("org.apache.maven.plugins"),
                    artifactId("maven-assembly-plugin"),
                    version("2.3")
                ),
                goal("single"),
                configuration(
                    element("descriptors",
                        element("descriptor", "${project.build.directory}/job/descriptor.xml")
                    )
                ),
                environment
            );
        }
    }
    

    然后,在子项目中,我只需要引用一次插件.在我看来,这比在每个子项目中重复每个幕后插件要好得多(这不可接受地增加了 poms 之间的耦合).如果我以后想在构建过程中添加一个mojo执行,我只需要修改一个地方并增加一个版本号.子项目的 pom 看起来像:

    Then, in the child projects, I just have to refer to the plugin once. In my opinion, this is greatly preferable over reiterating each of the behind-the-scenes plugins in every child project (which unacceptably increases the coupling between poms). If I want to add a mojo execution to the build procedure in the future, I only have to modify one place and bump a version number. Child project's pom looks like:

    <project ...>
        <parent>
            <groupId> ... </groupId>
            <artifactId>job-template</artifactId>
            <version> ... </version>
            <relativePath>../job-template</relativePath>
        </parent>
        <artifactId>job-testjob</artifactId>
        <name>test job</name>
        <build>
            <plugins>
                <plugin>
                    <groupId> ... </groupId>
                    <artifactId>job-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    </project>
    

    此外,整个多模块目录结构现在如下所示:

    Also, the entire multimodule directory structure now looks like this:

    +- job
      +- job-core
      +- job-maven-plugin
      +- job-template
      +- job-testjob1               (inherits from job-template)
      +- job-testjob2               (inherits from job-template)
    

    在我看来,这个解决方案并不完全是最优的,因为我现在将插件配置嵌入到一系列 mojo 而不是 pom 中,但它满足了我的目标,即集中配置并最大限度地减少子项目 pom 之间的重复.

    In my opinion this solution is not entirely optimal, since I now have plugin configuration embedded in a series of mojos instead of a pom, but it meets my goals for centralizing the configuration and minimizing the duplication among child project poms.

    (最后一点:我刚刚发现了 maven-aggregate-plugin 这似乎允许在 pom 中对多个插件执行进行分组.这可能以一种更理想的方式解决了问题,但我没有心情重做最后几个小时的工作.不过可能对其他人有益.)

    (One last note: I just discovered maven-aggregate-plugin which seems to allow grouping multiple plugin executions in the pom. This might have solved the problem in a slightly more desirable way, but I'm not in the mood to redo the last few hours of work. Might be beneficial to someone else though.)

    这篇关于在父pom中定义Maven插件,但只调用子项目中的插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆