Search code examples
javajunitpom.xmlmaven-surefire-pluginparent-pom

pom config inheritance - maven surefire plugin


I have a java project the has 3 "levels" in it. The pom in my top maven directory is looks like this:

<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 (second level) pom is:

<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>

and finally, (3rd level) the pom the of the project that actually has test classes in it:

<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>

My question is - does this structure of poms allow for configuration inheritance from the top to the bottom pom? I mean, if I have the parallel configuration in the first pom in the maven-sirefure-plugin - will it take affect in test classes under first_module and under my_project ?


Solution

  • Use pluginManagement tag in the parent's pom. Basically, <pluginManagement/> defines the settings for plugins that will be inherited by modules in your build:

    <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>
    

    Then, in the children's pom:

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

    You don't need to specify the version, since it's inherited from the parent with all the configuration.