diff --git a/changelog/@unreleased/pr-99.v2.yml b/changelog/@unreleased/pr-99.v2.yml new file mode 100644 index 00000000..3951361b --- /dev/null +++ b/changelog/@unreleased/pr-99.v2.yml @@ -0,0 +1,6 @@ +type: fix +fix: + description: If a JDK for a particular Java major version is not configured, error + out instead of falling back to the Gradle auto-provisioning behaviour. + links: + - https://github.com/palantir/gradle-jdks/pull/99 diff --git a/gradle-jdks/src/main/java/com/palantir/gradle/jdks/JdksPlugin.java b/gradle-jdks/src/main/java/com/palantir/gradle/jdks/JdksPlugin.java index c255ec55..3e2d0e67 100644 --- a/gradle-jdks/src/main/java/com/palantir/gradle/jdks/JdksPlugin.java +++ b/gradle-jdks/src/main/java/com/palantir/gradle/jdks/JdksPlugin.java @@ -20,6 +20,7 @@ import com.palantir.baseline.plugins.javaversions.BaselineJavaVersionsExtension; import java.io.File; import java.util.Arrays; +import java.util.Optional; import org.gradle.api.Plugin; import org.gradle.api.Project; import org.gradle.jvm.toolchain.JavaLanguageVersion; @@ -43,10 +44,19 @@ public void apply(Project rootProject) { rootProject .getExtensions() .getByType(BaselineJavaVersionsExtension.class) - .jdks((javaLanguageVersion, project) -> jdksExtension - .jdkFor(javaLanguageVersion, project) - .map(jdkExtension -> javaInstallationForLanguageVersion( - project, jdksExtension, jdkExtension, jdkManager, javaLanguageVersion))); + .jdks((javaLanguageVersion, project) -> { + JdkExtension jdkExtension = jdksExtension + .jdkFor(javaLanguageVersion, project) + .orElseThrow(() -> new RuntimeException(String.format( + "Could not find a JDK with major version %s in project '%s'. " + + "Please ensure that you have configured JDKs properly for " + + "gradle-jdks as per the readme: " + + "https://github.com/palantir/gradle-jdks#usage", + javaLanguageVersion.toString(), project.getPath()))); + + return Optional.of(javaInstallationForLanguageVersion( + project, jdksExtension, jdkExtension, jdkManager, javaLanguageVersion)); + }); } private JdksExtension extension(Project rootProject, JdkDistributions jdkDistributions) { diff --git a/gradle-jdks/src/test/groovy/com/palantir/gradle/jdks/JdksPluginIntegrationSpec.groovy b/gradle-jdks/src/test/groovy/com/palantir/gradle/jdks/JdksPluginIntegrationSpec.groovy index 9ce2921c..7ec0cce8 100644 --- a/gradle-jdks/src/test/groovy/com/palantir/gradle/jdks/JdksPluginIntegrationSpec.groovy +++ b/gradle-jdks/src/test/groovy/com/palantir/gradle/jdks/JdksPluginIntegrationSpec.groovy @@ -169,6 +169,14 @@ class JdksPluginIntegrationSpec extends IntegrationSpec { stdout.contains amazonRootCa1Serial } + def 'throws exception if there is no JDK defined for a particular jdk major version'() { + when: + def error = runTasksWithFailure('printJavaVersion').failure.cause.cause.message + + then: + error.contains "Could not find a JDK with major version 11 in project ':subproject'" + } + @Override ExecutionResult runTasksSuccessfully(String... tasks) { def result = super.runTasks(tasks)