A logo showing the text blog.marcnuri.com
Español
Home»Java»How to skip tests on specific JDK versions with Maven Compiler Plugin

Recent Posts

  • Fabric8 Kubernetes Client 7.2 is now available!
  • Connecting to an MCP Server from JavaScript using AI SDK
  • Connecting to an MCP Server from JavaScript using LangChain.js
  • The Future of Developer Tools: Adapting to Machine-Based Developers
  • Connecting to a Model Context Protocol (MCP) Server from Java using LangChain4j

Categories

  • Artificial Intelligence
  • Front-end
  • Go
  • Industry and business
  • Java
  • JavaScript
  • Legacy
  • Operations
  • Personal
  • Pet projects
  • Tools

Archives

  • May 2025
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • August 2024
  • June 2024
  • May 2024
  • April 2024
  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • September 2023
  • August 2023
  • July 2023
  • June 2023
  • May 2023
  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
  • February 2020
  • January 2020
  • December 2019
  • October 2019
  • September 2019
  • July 2019
  • March 2019
  • November 2018
  • July 2018
  • June 2018
  • May 2018
  • April 2018
  • March 2018
  • February 2018
  • December 2017
  • July 2017
  • January 2017
  • December 2015
  • November 2015
  • December 2014
  • March 2014
  • February 2011
  • November 2008
  • June 2008
  • May 2008
  • April 2008
  • January 2008
  • November 2007
  • September 2007
  • August 2007
  • July 2007
  • June 2007
  • May 2007
  • April 2007
  • March 2007

How to skip tests on specific JDK versions with Maven Compiler Plugin

2025-01-14 in Java tagged Java / Maven / Testing / Test-Driven Development (TDD) by Marc Nuri | Last updated: 2025-01-14
Versión en Español

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:

pom.xml
<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:

pom.xml
<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.

Twitter iconFacebook iconLinkedIn iconPinterest iconEmail icon

Post navigation
Eclipse JKube 1.18 is now available!Docker: How to initialize docker-credential-pass
© 2007 - 2025 Marc Nuri