Require Java Version

This rule enforces certain Java JDK versions. The rule uses the Enforcer version range syntax to define allowed versions.

The following parameters are supported by this rule:

  • message - an optional message to the user if the rule fails.
  • version - range of allowed JDKs.
  • display - flag to display the normalized JDK version.

    For JDK 1.8 you can also use simple 8 as version - it will be internally changed to 1.8

The JDK version is retrieved and the following processing occurs before being checked:

  1. Drop all non-numeric characters preceding the first number. (build 1.5.0_07-b03 becomes 1.5.0_07-b03)
  2. Replace all '_' and '-' with '.' (1.5.0_07-b03 becomes 1.5.0.07.b03)
  3. Remove all non digit characters "[^0-9] and convert each section using Integer.parseInt() (1.5.0_07-b03 becomes 1.5.0.7.3)
  4. Split the string on '.' and take the first 3 sections, separated by '.' and add '-' followed by the fourth section (1.5.0.7.3 becomes 1.5.0-7)

This preprocessing normalizes various JDK version strings into a standard x.y.z-b version number. Your required range should therefore use the x.y.z-b format for comparison. There is an easy way to see how your current JDK string will be normalized, you can enable display option.

Sample Plugin Configuration:

  1. <project>
  2. [...]
  3. <build>
  4. <plugins>
  5. <plugin>
  6. <groupId>org.apache.maven.plugins</groupId>
  7. <artifactId>maven-enforcer-plugin</artifactId>
  8. <version>3.5.0</version>
  9. <executions>
  10. <execution>
  11. <id>enforce-java</id>
  12. <goals>
  13. <goal>enforce</goal>
  14. </goals>
  15. <configuration>
  16. <rules>
  17. <requireJavaVersion>
  18. <version>1.6.0</version>
  19. </requireJavaVersion>
  20. </rules>
  21. </configuration>
  22. </execution>
  23. </executions>
  24. </plugin>
  25. </plugins>
  26. </build>
  27. [...]
  28. </project>

If you would like to enforce a certain vendor string for the JDK, you would use the RequireJavaVendor rule