diff --git a/subprojects/language-java/src/main/java/org/gradle/api/tasks/compile/JavaCompile.java b/subprojects/language-java/src/main/java/org/gradle/api/tasks/compile/JavaCompile.java index b05556bec2d1..6193838b824e 100644 --- a/subprojects/language-java/src/main/java/org/gradle/api/tasks/compile/JavaCompile.java +++ b/subprojects/language-java/src/main/java/org/gradle/api/tasks/compile/JavaCompile.java @@ -75,8 +75,6 @@ import java.util.List; import java.util.concurrent.Callable; -import static com.google.common.base.Preconditions.checkState; - /** * Compiles Java source files. * @@ -261,23 +259,23 @@ private void validateForkOptionsMatchToolchain() { ForkOptions forkOptions = getOptions().getForkOptions(); File customJavaHome = forkOptions.getJavaHome(); - checkState( - customJavaHome == null || customJavaHome.equals(toolchainJavaHome), - "Toolchain from `javaHome` property on `ForkOptions` does not match toolchain from `javaCompiler` property" - ); + if (customJavaHome != null) { + JavaExecutableUtils.validateMatchingFiles( + customJavaHome, "Toolchain from `javaHome` property on `ForkOptions`", + toolchainJavaHome, "toolchain from `javaCompiler` property" + ); + } String customExecutablePath = forkOptions.getExecutable(); - if (customExecutablePath == null) { - return; + if (customExecutablePath != null) { + // We do not match the custom executable against the compiler executable from the toolchain (javac), + // because the custom executable can be set to the path of another tool in the toolchain such as a launcher (java). + File customExecutableJavaHome = JavaExecutableUtils.resolveJavaHomeOfExecutable(customExecutablePath); + JavaExecutableUtils.validateMatchingFiles( + customExecutableJavaHome, "Toolchain from `executable` property on `ForkOptions`", + toolchainJavaHome, "toolchain from `javaCompiler` property" + ); } - - // We do not match the custom executable against the compiler executable from the toolchain (javac), - // because the custom executable can be set to the path of another tool in the toolchain such as a launcher (java). - File customExecutableJavaHome = JavaExecutableUtils.resolveJavaHomeOfExecutable(customExecutablePath); - checkState( - customExecutableJavaHome.equals(toolchainJavaHome), - "Toolchain from `executable` property on `ForkOptions` does not match toolchain from `javaCompiler` property" - ); } private boolean isToolchainCompatibleWithJava8() { diff --git a/subprojects/platform-jvm/src/main/java/org/gradle/jvm/toolchain/internal/JavaExecutableUtils.java b/subprojects/platform-jvm/src/main/java/org/gradle/jvm/toolchain/internal/JavaExecutableUtils.java index 0fdff895e068..a3a72b7826d7 100644 --- a/subprojects/platform-jvm/src/main/java/org/gradle/jvm/toolchain/internal/JavaExecutableUtils.java +++ b/subprojects/platform-jvm/src/main/java/org/gradle/jvm/toolchain/internal/JavaExecutableUtils.java @@ -61,17 +61,21 @@ public static void validateExecutable(@Nullable String executable, String execut } File executableFile = resolveExecutable(executable); - if (executableFile.equals(referenceFile)) { + validateMatchingFiles(executableFile, executableDescription, referenceFile, referenceDescription); + } + + public static void validateMatchingFiles(File customFile, String customDescription, File referenceFile, String referenceDescription) { + if (customFile.equals(referenceFile)) { return; } - File canonicalExecutableFile = canonicalFile(executableFile); + File canonicalCustomFile = canonicalFile(customFile); File canonicalReferenceFile = canonicalFile(referenceFile); - if (canonicalExecutableFile.equals(canonicalReferenceFile)) { + if (canonicalCustomFile.equals(canonicalReferenceFile)) { return; } - throw new IllegalStateException(executableDescription + " does not match " + referenceDescription + "."); + throw new IllegalStateException(customDescription + " does not match " + referenceDescription + "."); } private static File canonicalFile(File file) {