Skip to content

Commit

Permalink
Error out if JDKs are not configured (#99)
Browse files Browse the repository at this point in the history
If a JDK for a particular Java major version is not configured, error out instead of falling back to the Gradle auto-provisioning behaviour.
  • Loading branch information
CRogers authored Aug 24, 2022
1 parent 187ae11 commit e71ac16
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
6 changes: 6 additions & 0 deletions changelog/@unreleased/pr-99.v2.yml
Original file line number Diff line number Diff line change
@@ -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
18 changes: 14 additions & 4 deletions gradle-jdks/src/main/java/com/palantir/gradle/jdks/JdksPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit e71ac16

Please sign in to comment.