Compiling a Gradle Java project with Java 8 but running tests with Java 11

As the first part of a transition process of my projects, I would like to keep compile with JDK-8 compiler, but execute tests with JDK-11 runtime.

My projects are Gradle projects (6.+ if it matters), using the java plugin or java-library plugins.

I could not find a way to do it with Gradle options.

I tried to compile ( gradlew build ) in one terminal with JDK-8, and then switch to another terminal with JDK 11 and run the tests ( gradlew test ), but it re-compiled the code.

What is the correct way to do it?

If you just want to run your tests with a different version of Java, you just need to specify a different Java executable to use for Test tasks.

tasks.withType(Test) {
    executable = new File("${PATH_TO_TEST_JAVA_RUNTIME}", 'bin/java')
}

Note that ${PATH_TO_TEST_JAVA_RUNTIME} is nothing special. It is just a placeholder that you can replace that with any sort of property lookup/variable/path that you want.

Thanks for your comment, but I don’t see any property named executable in Task API

That’s correct. An executable is not universal to all tasks and therefore is not part of the generic Task interface. It is specifically part of an interface implemented by some Tasks, such as Test, where it is relevant.