【问题标题】:pom config inheritance - maven surefire pluginpom 配置继承 - maven surefire 插件
【发布时间】:2019-06-07 15:05:23
【问题描述】:

我有一个 java 项目,其中有 3 个“级别”。 我的顶级 maven 目录中的 pom 如下所示:

<groupId>com.my_app</groupId>
<artifactId>my_app</artifactId>
<version>LATEST-SNAPSHOT</version>

<modules>
    <module>first_module</module>
    <module>second_module</module>
</modules>
...
<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.22.1</version>
        <configuration>
            <parallel>classes</parallel>
            <threadCount>10</threadCount>
        </configuration>
    </plugin>
</plugins>

first_module(第二级)pom是:

<parent>
    <groupId>com.my_app</groupId>
    <artifactId>my_app</artifactId>
    <version>LATEST-SNAPSHOT</version>
</parent>

<groupId>com.my_app.first_module</groupId>
<artifactId>first_module</artifactId>
<version>LATEST-SNAPSHOT</version>
...
<plugins>
    <plugin>
        <version>2.22.1</version>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
    </plugin>
</plugins>

最后,(3rd level)项目中实际包含测试类的 pom:

<parent>
    <artifactId>first_module</artifactId>
    <groupId>com.my_app.first_module</groupId>
    <version>LATEST-SNAPSHOT</version>
</parent>

<artifactId>my_project</artifactId>
...
<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.22.1</version>
    </plugin>
</plugins>

我的问题是 - 这种 pom 结构是否允许从顶部到底部 pom 的配置继承?我的意思是,如果我在 ma​​ven-sirefure-plugin 的第一个 pom 中有 parallel 配置 - 它会在 first_module 下的测试类中生效> 并在 my_project 下?

【问题讨论】:

    标签: java junit pom.xml maven-surefire-plugin parent-pom


    【解决方案1】:

    在父母的 pom 中使用 pluginManagement 标签。基本上,&lt;pluginManagement/&gt; 定义了将由构建中的模块继承的插件设置:

    <pluginManagement>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.1</version>
            <configuration>
                <parallel>classes</parallel>
                <threadCount>10</threadCount>
            </configuration>
        </plugin>
    </plugins>
    </pluginManagement>
    

    然后,在孩子们的 pom 中:

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

    您不需要指定版本,因为它是从具有所有配置的父代继承的。

    【讨论】:

    • 非常感谢!如果我想拥有项目特定的配置,我可以将它添加到子 pom 中?它将包括来自父母和孩子的配置?
    • @OfekAgmon 是的,您可以覆盖子插件中的配置。
    • 但是例如 - 假设我在父 pom 中有并行配置(classes),我可以在子 pom 中添加另一个不同的配置,它们都可以吗?或者一旦我在子 pom 中有配置标签 - 它会覆盖父级的整个配置?
    • 它会先应用父配置,然后应用子配置,因此它不会覆盖父配置的整个配置。
    猜你喜欢
    • 2014-03-05
    • 1970-01-01
    • 2011-11-26
    • 2010-11-19
    • 2014-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-27
    相关资源
    最近更新 更多