Class JdkSourceLevelSupport

java.lang.Object
org.apache.maven.impl.JdkSourceLevelSupport

public final class JdkSourceLevelSupport extends Object
Utility class for JDK source level compatibility checks.

Maps JDK major versions to their supported --source/--release levels, based on the javac retirement schedule defined in JEP 182 and subsequent JDK releases.

The retirement schedule follows these milestones:

  • JDK 9: removed --source 1 through 5, minimum is 6
  • JDK 12: removed --source 6, minimum is 7
  • JDK 21: removed --source 7, minimum is 8
  • Method Details

    • minimumSupportedSourceLevel

      public static int minimumSupportedSourceLevel(int jdkMajor)
      Returns the minimum --source level supported by a given JDK major version.
      Parameters:
      jdkMajor - the JDK major version (e.g., 17, 21)
      Returns:
      the minimum supported source level
    • supportsSourceLevel

      public static boolean supportsSourceLevel(int jdkMajor, int sourceLevel)
      Returns whether a given JDK version supports the specified --source level.
      Parameters:
      jdkMajor - the JDK major version
      sourceLevel - the desired source level
      Returns:
      true if the JDK supports the source level
    • normalizeSourceLevel

      public static int normalizeSourceLevel(String version)
      Normalizes a source level string to a major version number.

      Handles legacy formats:

      • "1.5"5
      • "1.8"8
      • "11"11
      • "21.0.1"21
      Parameters:
      version - the source level string
      Returns:
      the normalized major version, or -1 if the string cannot be parsed
    • latestJdkForSourceLevel

      public static int latestJdkForSourceLevel(int sourceLevel)
      Returns the latest JDK major version that still supports the given --source level.

      Based on the retirement schedule:

      • source 1–5 → last supported by JDK 8
      • source 6 → last supported by JDK 11
      • source 7 → last supported by JDK 20
      • source 8+ → still supported by current JDKs
      Parameters:
      sourceLevel - the source level
      Returns:
      the latest JDK major version that supports it, or -1 if the source level is still supported by all current JDKs
    • getRunningJdkMajor

      public static int getRunningJdkMajor()
      Returns the major version of the currently running JDK.
      Returns:
      the running JDK major version