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:

is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.



Add comment

   


Comments 

#1Satyam2020-08-12 02:57
Excluding test classes from a certain package is not working. Can you check?


com.acme.*.*

I'm using the command 'mvn clean test'
Quote