luis.espinal
luis.espinal

Reputation: 10529

Force Maven to Fail if Java Runtime is Above A Given Version

I have a situation with a legacy project, where I need a project to fail if Maven is invoked with a JDK runtime other than 1.8 or 1.9. The reason being that the current project has dependencies that were part of the JDK once, but not anymore starting with JDK 11.

This is distinct from specifying source and target versions for the maven-plugin compiler (which are currently set at 8.)

The build already fails, complaining about unresolvable dependencies, but I would like it to fail with a message saying "you need to use a JDK 8/9 runtime" instead (clarity is always king.)

This is just a stop-gap measure to avoid people sending panic emails about things not compiling because they used JDK 11+, and where I cannot rely on everybody setting their own JDK's in their toolchains.xml.

Upvotes: 0

Views: 108

Answers (1)

Simon Martinelli
Simon Martinelli

Reputation: 36143

You can use the maven enforcer plugin:

<project>
  [...]
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-enforcer-plugin</artifactId>
        <version>3.0.0-M3</version>
        <executions>
          <execution>
            <id>enforce-java</id>
            <goals>
              <goal>enforce</goal>
            </goals>
            <configuration>
              <rules>
                <requireJavaVersion>
                  <version>[1.8,9]</version>
                </requireJavaVersion>
              </rules>    
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  [...]
</project>

CAUTION: I didn't test the range expression.

Source: https://maven.apache.org/enforcer/enforcer-rules/requireJavaVersion.html

Upvotes: 7

Related Questions