mavenjunit5maven-surefire-pluginsonatypemaven-central

run additional tests during maven deploy phase


In my project I have a set of a very fast unit-tests and small integration-tests as well a set of slower integration, end2end and performance tests (10mintes+). I use junit5 tags to classify them and in my pom.xml I configured maven-surefire-plugin to run only the fast ones with <groups>fast</groups> (I have other projects based on this one, that I'm working on in parallel, so I don't want to wait 10minutes after each tiny change to the base project).
However when I deploy to central using nexus-staging-maven-plugin I definitely do want to run the slow ones also. Currently I do it by manually overriding groups system property on the command line: mvn -Dgroups="fast|slow" clean deploy. This is however prone to mistakes and I wonder if there's a way to automate it: is it for example possible to automatically use different configuration for maven-surefire-plugin depending whether I do install or deploy?


Solution

  • to summarize all the comments to SiKing's answer:

    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.1.2</version>
            <executions>
                <execution>
                    <!-- reconfigure the default execution from test phase -->
                    <id>default-test</id>
                    <configuration>
                        <excludedGroups>slow</excludedGroups>
                    </configuration>
                    <goals>
                        <goal>test</goal>
                    </goals>
                </execution>
                <execution>
                    <!-- additional execution at the beginning of deploy phase -->
                    <id>pre-deploy-test</id>
                    <phase>deploy</phase>
                    <configuration>
                        <groups>slow</groups>
                    </configuration>
                    <goals>
                        <goal>test</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <!-- nexus-staging must be placed *AFTER* surefire -->
            <groupId>org.sonatype.plugins</groupId>
            <artifactId>nexus-staging-maven-plugin</artifactId>
            <!-- nexus staging config here... -->
        </plugin>
        <!-- other plugins here... -->
    </plugins>
    

    update: as per this comment, some may consider the above an abuse of surefire, hence below is a version that uses maven-failsafe-plugin for pre-deploy tests, so there's no need to "hack" into default-test execution of surefire.

    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.1.2</version>
            <configuration>
                <excludedGroups>slow</excludedGroups>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>3.1.2</version>
            <executions>
                <execution>
                    <id>pre-deploy-tests</id>
                    <phase>deploy</phase>
                    <configuration>
                        <includes>
                            <include>**/*Test.java</include>
                            <include>**/*Tests.java</include>
                        </includes>
                        <groups>slow</groups>
                    </configuration>
                    <goals>
                        <goal>integration-test</goal>
                        <goal>verify</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <!-- nexus-staging must be placed *AFTER* failsafe -->
            <groupId>org.sonatype.plugins</groupId>
            <artifactId>nexus-staging-maven-plugin</artifactId>
            <!-- nexus staging config here... -->
        </plugin>
        <!-- other plugins here... -->
    </plugins>