- Details
- Written by Nam Ha Minh
- Last Updated on 21 February 2020   |   Print Email
In Java development with Maven build, a build lifecycle will fail if the tests code fails to compile or the tests run produces failures. But sometimes we need to continue the build lifecycle even the tests fail. For example, when packaging the project (
mvn package) – the tests code not included in the package – so it can be okay to continue the build without running the tests.To exclude tests from build in Maven, you need to configure Maven Surefire plugin in the
pom.xml file as below:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.0</version>
<configuration>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</configuration>
</plugin>
This will exclude all the test classes from the build lifecycle. The following code explains how such tests exclusion configuration relates in Maven’s
pom.xml file:
<project ...>
...
<dependencies>
...
</dependencies>
<build>
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.0</version>
<configuration>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</configuration>
</plugin>
...
</plugins>
</build>
</project>
You can exclude some certain test classes, for example:
<configuration>
<excludes>
<exclude>**/Test1.java</exclude>
<exclude>**/Test2.java</exclude>
</excludes>
</configuration>
This will exclude all test classes having DAO in their names:
<exclude>**/*DAO*.java</exclude>
You can also exclude all test classes from a certain package, for example:
<exclude>com.acme.*.*</exclude>
Or exclude test classes matching a naming pattern from a certain package, for example:
<exclude>net.codejava.*Test</exclude>
This will exclude all tests classes ending with
Test in the package
net.codejava.
Reference:About the Author:
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on
Facebook and watch
his Java videos you YouTube.