How to skip tests on specific JDK versions with Maven Compiler Plugin
Introduction
The faster Java release cadence has made it increasingly common for projects to target multiple JDK versions simultaneously. In addition, an increasing number of dependencies require newer JDK versions and no longer support older versions.
When these are test dependencies, it's common to have tests that can only run on specific JDK versions. To ensure successful compilation and testing, it might be necessary to compile and run tests only on the specific JDK versions they are compatible with.
You might be tempted to use JUnit's @DisabledForJreRange
annotation to skip tests on specific JDK versions.
However, this approach only skips the test execution and not the compilation of the test classes.
This can lead to compilation, particularly when test classes depend on APIs that are unavailable in the current JDK.
In this post, I'll show you how to configure the Maven Compiler Plugin and your Maven project to compile and run tests only on specific JDK versions.
Configuring the Maven Compiler Plugin
The Maven Compiler Plugin allows fine-grained control over the compilation process, including excluding specific files or directories.
To skip the compilation of test classes on specific JDK versions, you can use the testExcludes
configuration option.
For example, the following configuration will exclude all test classes that have a LegacyTest
suffix from the test compilation process:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<testExcludes>
<!-- Exclude test files with the suffix 'LegacyTest' -->
<testExclude>**/*LegacyTest.java</testExclude>
</testExcludes>
</configuration>
</plugin>
</plugins>
</build>
Excluding tests on specific JDK versions
We can leverage Maven's profiles to perform the exclusions only when we run Maven on specific JDK versions.
By using the activation
element in combination with the jdk
property, we can activate a profile only when Maven is running on a specific JDK version.
For example, the following configuration will exclude all test classes that have a LegacyTest
suffix when Maven is running on JDK 8:
<profiles>
<profile>
<id>exclude-legacy-tests</id>
<activation>
<jdk>1.8</jdk>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<testExcludes>
<testExclude>**/*LegacyTest.java</testExclude>
</testExcludes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
In the future, if you need to exclude test classes on JDK 11 or earlier, you can adjust the jdk
property to <jdk>(,11]</jdk>
.
You can check a real-world example of this configuration in the Eclipse JKube project's pom.xml.
Conclusion
In this post, we've seen how to configure the Maven Compiler Plugin to exclude test classes from the compilation process based on the JDK version.
By combining Maven profiles with the jdk
property, you can dynamically exclude incompatible test classes, ensuring smooth compilation and testing workflows.
This configuration pattern is flexible and can be adapted to various scenarios, making Maven a powerful tool for maintaining compatibility across diverse environments.