diff --git a/.gitignore b/.gitignore index ff10e03fd..74d2ac9d6 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,5 @@ out/ docs/node_modules/ generated_src generated_testSrc +generated +generated_tests diff --git a/changelog/@unreleased/pr-2193.v2.yml b/changelog/@unreleased/pr-2193.v2.yml new file mode 100644 index 000000000..f94f7a90f --- /dev/null +++ b/changelog/@unreleased/pr-2193.v2.yml @@ -0,0 +1,6 @@ +type: feature +feature: + description: Add a configuration point for providing your own JDKs to all java requiring + tasks. + links: + - https://github.com/palantir/gradle-baseline/pull/2193 diff --git a/gradle-baseline-java/src/main/groovy/com/palantir/baseline/extensions/BaselineJavaVersionsExtension.java b/gradle-baseline-java/src/main/groovy/com/palantir/baseline/extensions/BaselineJavaVersionsExtension.java index c82d8246d..bdb4aca4b 100644 --- a/gradle-baseline-java/src/main/groovy/com/palantir/baseline/extensions/BaselineJavaVersionsExtension.java +++ b/gradle-baseline-java/src/main/groovy/com/palantir/baseline/extensions/BaselineJavaVersionsExtension.java @@ -18,7 +18,9 @@ import javax.inject.Inject; import org.gradle.api.Project; +import org.gradle.api.provider.MapProperty; import org.gradle.api.provider.Property; +import org.gradle.jvm.toolchain.JavaInstallationMetadata; import org.gradle.jvm.toolchain.JavaLanguageVersion; /** @@ -26,10 +28,10 @@ * with consistent java toolchains. */ public class BaselineJavaVersionsExtension { - private final Property libraryTarget; private final Property distributionTarget; private final Property runtime; + private final MapProperty jdks; @Inject public BaselineJavaVersionsExtension(Project project) { @@ -43,6 +45,9 @@ public BaselineJavaVersionsExtension(Project project) { libraryTarget.finalizeValueOnRead(); distributionTarget.finalizeValueOnRead(); runtime.finalizeValueOnRead(); + + jdks = project.getObjects().mapProperty(JavaLanguageVersion.class, JavaInstallationMetadata.class); + jdks.finalizeValueOnRead(); } /** Target {@link JavaLanguageVersion} for compilation of libraries that are published. */ @@ -74,4 +79,8 @@ public final Property runtime() { public final void setRuntime(int value) { runtime.set(JavaLanguageVersion.of(value)); } + + public final MapProperty getJdks() { + return jdks; + } } diff --git a/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/BaselineJavaVersion.java b/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/BaselineJavaVersion.java index 32335370f..30c47036c 100644 --- a/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/BaselineJavaVersion.java +++ b/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/BaselineJavaVersion.java @@ -17,6 +17,9 @@ package com.palantir.baseline.plugins; import com.palantir.baseline.extensions.BaselineJavaVersionExtension; +import com.palantir.baseline.extensions.BaselineJavaVersionsExtension; +import com.palantir.baseline.plugins.javaversions.BaselineJavaToolchain; +import com.palantir.baseline.plugins.javaversions.JavaToolchains; import javax.inject.Inject; import org.gradle.api.Action; import org.gradle.api.DefaultTask; @@ -38,7 +41,6 @@ import org.gradle.api.tasks.scala.ScalaDoc; import org.gradle.api.tasks.testing.Test; import org.gradle.jvm.toolchain.JavaLanguageVersion; -import org.gradle.jvm.toolchain.JavaToolchainService; import org.gradle.jvm.toolchain.JavaToolchainSpec; public final class BaselineJavaVersion implements Plugin { @@ -49,6 +51,7 @@ public final class BaselineJavaVersion implements Plugin { public void apply(Project project) { BaselineJavaVersionExtension extension = project.getExtensions().create(EXTENSION_NAME, BaselineJavaVersionExtension.class, project); + project.getPluginManager().withPlugin("java", unused -> { JavaPluginExtension javaPluginExtension = project.getExtensions().getByType(JavaPluginExtension.class); @@ -62,11 +65,14 @@ public void execute(JavaToolchainSpec javaToolchainSpec) { } }); + JavaToolchains javaToolchains = new JavaToolchains( + project, project.getRootProject().getExtensions().getByType(BaselineJavaVersionsExtension.class)); + // Compilation tasks (using target version) - configureCompilationTasks(project, extension.target()); + configureCompilationTasks(project, extension.target(), javaToolchains.forVersion(extension.target())); // Execution tasks (using the runtime version) - configureExecutionTasks(project, extension.runtime()); + configureExecutionTasks(project, javaToolchains.forVersion(extension.runtime())); // Validation project.getTasks() @@ -82,18 +88,13 @@ public void execute(CheckJavaVersionsTask task) { } private static void configureCompilationTasks( - Project project, Provider targetVersionProvider) { - JavaToolchainService javaToolchainService = project.getExtensions().getByType(JavaToolchainService.class); - + Project project, + Provider targetVersionProvider, + Provider javaToolchain) { project.getTasks().withType(JavaCompile.class, new Action() { @Override public void execute(JavaCompile javaCompile) { - javaCompile.getJavaCompiler().set(javaToolchainService.compilerFor(new Action() { - @Override - public void execute(JavaToolchainSpec javaToolchainSpec) { - javaToolchainSpec.getLanguageVersion().set(targetVersionProvider); - } - })); + javaCompile.getJavaCompiler().set(javaToolchain.flatMap(BaselineJavaToolchain::javaCompiler)); // Set sourceCompatibility to opt out of '-release', allowing opens/exports to be used. javaCompile.doFirst(new Action() { @Override @@ -109,24 +110,14 @@ public void execute(Task task) { project.getTasks().withType(Javadoc.class, new Action() { @Override public void execute(Javadoc javadoc) { - javadoc.getJavadocTool().set(javaToolchainService.javadocToolFor(new Action() { - @Override - public void execute(JavaToolchainSpec javaToolchainSpec) { - javaToolchainSpec.getLanguageVersion().set(targetVersionProvider); - } - })); + javadoc.getJavadocTool().set(javaToolchain.flatMap(BaselineJavaToolchain::javadocTool)); } }); project.getTasks().withType(GroovyCompile.class, new Action() { @Override public void execute(GroovyCompile groovyCompile) { - groovyCompile.getJavaLauncher().set(javaToolchainService.launcherFor(new Action() { - @Override - public void execute(JavaToolchainSpec javaToolchainSpec) { - javaToolchainSpec.getLanguageVersion().set(targetVersionProvider); - } - })); + groovyCompile.getJavaLauncher().set(javaToolchain.flatMap(BaselineJavaToolchain::javaLauncher)); // Set sourceCompatibility to opt out of '-release', allowing opens/exports to be used. groovyCompile.doFirst(new Action() { @Override @@ -142,12 +133,7 @@ public void execute(Task task) { project.getTasks().withType(ScalaCompile.class, new Action() { @Override public void execute(ScalaCompile scalaCompile) { - scalaCompile.getJavaLauncher().set(javaToolchainService.launcherFor(new Action() { - @Override - public void execute(JavaToolchainSpec javaToolchainSpec) { - javaToolchainSpec.getLanguageVersion().set(targetVersionProvider); - } - })); + scalaCompile.getJavaLauncher().set(javaToolchain.flatMap(BaselineJavaToolchain::javaLauncher)); // Set sourceCompatibility to opt out of '-release', allowing opens/exports to be used. scalaCompile.doFirst(new Action() { @Override @@ -163,39 +149,23 @@ public void execute(Task task) { project.getTasks().withType(ScalaDoc.class, new Action() { @Override public void execute(ScalaDoc scalaDoc) { - scalaDoc.getJavaLauncher().set(javaToolchainService.launcherFor(new Action() { - @Override - public void execute(JavaToolchainSpec javaToolchainSpec) { - javaToolchainSpec.getLanguageVersion().set(targetVersionProvider); - } - })); + scalaDoc.getJavaLauncher().set(javaToolchain.flatMap(BaselineJavaToolchain::javaLauncher)); } }); } - private static void configureExecutionTasks(Project project, Provider runtimeVersionProvider) { - JavaToolchainService javaToolchainService = project.getExtensions().getByType(JavaToolchainService.class); + private static void configureExecutionTasks(Project project, Provider javaToolchain) { project.getTasks().withType(JavaExec.class, new Action() { @Override public void execute(JavaExec javaExec) { - javaExec.getJavaLauncher().set(javaToolchainService.launcherFor(new Action() { - @Override - public void execute(JavaToolchainSpec javaToolchainSpec) { - javaToolchainSpec.getLanguageVersion().set(runtimeVersionProvider); - } - })); + javaExec.getJavaLauncher().set(javaToolchain.flatMap(BaselineJavaToolchain::javaLauncher)); } }); project.getTasks().withType(Test.class, new Action() { @Override public void execute(Test test) { - test.getJavaLauncher().set(javaToolchainService.launcherFor(new Action() { - @Override - public void execute(JavaToolchainSpec javaToolchainSpec) { - javaToolchainSpec.getLanguageVersion().set(runtimeVersionProvider); - } - })); + test.getJavaLauncher().set(javaToolchain.flatMap(BaselineJavaToolchain::javaLauncher)); } }); } diff --git a/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/javaversions/BaselineJavaCompiler.java b/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/javaversions/BaselineJavaCompiler.java new file mode 100644 index 000000000..468af3631 --- /dev/null +++ b/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/javaversions/BaselineJavaCompiler.java @@ -0,0 +1,39 @@ +/* + * (c) Copyright 2022 Palantir Technologies Inc. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.palantir.baseline.plugins.javaversions; + +import org.gradle.api.file.RegularFile; +import org.gradle.jvm.toolchain.JavaCompiler; +import org.gradle.jvm.toolchain.JavaInstallationMetadata; + +final class BaselineJavaCompiler implements JavaCompiler { + private final JavaInstallationMetadata javaInstallationMetadata; + + BaselineJavaCompiler(JavaInstallationMetadata javaInstallationMetadata) { + this.javaInstallationMetadata = javaInstallationMetadata; + } + + @Override + public JavaInstallationMetadata getMetadata() { + return javaInstallationMetadata; + } + + @Override + public RegularFile getExecutablePath() { + return JavaInstallationMetadataUtils.findExecutable(javaInstallationMetadata, "javac"); + } +} diff --git a/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/javaversions/BaselineJavaLauncher.java b/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/javaversions/BaselineJavaLauncher.java new file mode 100644 index 000000000..d4df96a6d --- /dev/null +++ b/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/javaversions/BaselineJavaLauncher.java @@ -0,0 +1,39 @@ +/* + * (c) Copyright 2022 Palantir Technologies Inc. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.palantir.baseline.plugins.javaversions; + +import org.gradle.api.file.RegularFile; +import org.gradle.jvm.toolchain.JavaInstallationMetadata; +import org.gradle.jvm.toolchain.JavaLauncher; + +final class BaselineJavaLauncher implements JavaLauncher { + private final JavaInstallationMetadata javaInstallationMetadata; + + BaselineJavaLauncher(JavaInstallationMetadata javaInstallationMetadata) { + this.javaInstallationMetadata = javaInstallationMetadata; + } + + @Override + public JavaInstallationMetadata getMetadata() { + return javaInstallationMetadata; + } + + @Override + public RegularFile getExecutablePath() { + return JavaInstallationMetadataUtils.findExecutable(javaInstallationMetadata, "java"); + } +} diff --git a/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/javaversions/BaselineJavaToolchain.java b/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/javaversions/BaselineJavaToolchain.java new file mode 100644 index 000000000..8c4d0d0c5 --- /dev/null +++ b/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/javaversions/BaselineJavaToolchain.java @@ -0,0 +1,30 @@ +/* + * (c) Copyright 2022 Palantir Technologies Inc. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.palantir.baseline.plugins.javaversions; + +import org.gradle.api.provider.Provider; +import org.gradle.jvm.toolchain.JavaCompiler; +import org.gradle.jvm.toolchain.JavaLauncher; +import org.gradle.jvm.toolchain.JavadocTool; + +public interface BaselineJavaToolchain { + Provider javaCompiler(); + + Provider javadocTool(); + + Provider javaLauncher(); +} diff --git a/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/javaversions/BaselineJavadocTool.java b/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/javaversions/BaselineJavadocTool.java new file mode 100644 index 000000000..f55fe398e --- /dev/null +++ b/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/javaversions/BaselineJavadocTool.java @@ -0,0 +1,39 @@ +/* + * (c) Copyright 2022 Palantir Technologies Inc. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.palantir.baseline.plugins.javaversions; + +import org.gradle.api.file.RegularFile; +import org.gradle.jvm.toolchain.JavaInstallationMetadata; +import org.gradle.jvm.toolchain.JavadocTool; + +final class BaselineJavadocTool implements JavadocTool { + private final JavaInstallationMetadata javaInstallationMetadata; + + BaselineJavadocTool(JavaInstallationMetadata javaInstallationMetadata) { + this.javaInstallationMetadata = javaInstallationMetadata; + } + + @Override + public JavaInstallationMetadata getMetadata() { + return javaInstallationMetadata; + } + + @Override + public RegularFile getExecutablePath() { + return JavaInstallationMetadataUtils.findExecutable(javaInstallationMetadata, "javadoc"); + } +} diff --git a/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/javaversions/ConfiguredJavaToolchain.java b/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/javaversions/ConfiguredJavaToolchain.java new file mode 100644 index 000000000..43c631df0 --- /dev/null +++ b/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/javaversions/ConfiguredJavaToolchain.java @@ -0,0 +1,46 @@ +/* + * (c) Copyright 2022 Palantir Technologies Inc. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.palantir.baseline.plugins.javaversions; + +import org.gradle.api.provider.Provider; +import org.gradle.jvm.toolchain.JavaCompiler; +import org.gradle.jvm.toolchain.JavaInstallationMetadata; +import org.gradle.jvm.toolchain.JavaLauncher; +import org.gradle.jvm.toolchain.JavadocTool; + +final class ConfiguredJavaToolchain implements BaselineJavaToolchain { + private final Provider javaInstallationMetadata; + + ConfiguredJavaToolchain(Provider javaInstallationMetadata) { + this.javaInstallationMetadata = javaInstallationMetadata; + } + + @Override + public Provider javaCompiler() { + return javaInstallationMetadata.map(BaselineJavaCompiler::new); + } + + @Override + public Provider javadocTool() { + return javaInstallationMetadata.map(BaselineJavadocTool::new); + } + + @Override + public Provider javaLauncher() { + return javaInstallationMetadata.map(BaselineJavaLauncher::new); + } +} diff --git a/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/javaversions/FallbackGradleJavaToolchain.java b/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/javaversions/FallbackGradleJavaToolchain.java new file mode 100644 index 000000000..52a4dfd1d --- /dev/null +++ b/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/javaversions/FallbackGradleJavaToolchain.java @@ -0,0 +1,51 @@ +/* + * (c) Copyright 2022 Palantir Technologies Inc. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.palantir.baseline.plugins.javaversions; + +import org.gradle.api.Action; +import org.gradle.api.provider.Provider; +import org.gradle.jvm.toolchain.JavaCompiler; +import org.gradle.jvm.toolchain.JavaLauncher; +import org.gradle.jvm.toolchain.JavaToolchainService; +import org.gradle.jvm.toolchain.JavaToolchainSpec; +import org.gradle.jvm.toolchain.JavadocTool; + +public final class FallbackGradleJavaToolchain implements BaselineJavaToolchain { + private final JavaToolchainService javaToolchainService; + private final Action configureJavaToolchainSpec; + + public FallbackGradleJavaToolchain( + JavaToolchainService javaToolchainService, Action configureJavaToolchainSpec) { + this.javaToolchainService = javaToolchainService; + this.configureJavaToolchainSpec = configureJavaToolchainSpec; + } + + @Override + public Provider javaCompiler() { + return javaToolchainService.compilerFor(configureJavaToolchainSpec); + } + + @Override + public Provider javadocTool() { + return javaToolchainService.javadocToolFor(configureJavaToolchainSpec); + } + + @Override + public Provider javaLauncher() { + return javaToolchainService.launcherFor(configureJavaToolchainSpec); + } +} diff --git a/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/javaversions/JavaInstallationMetadataUtils.java b/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/javaversions/JavaInstallationMetadataUtils.java new file mode 100644 index 000000000..2dbaf2a95 --- /dev/null +++ b/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/javaversions/JavaInstallationMetadataUtils.java @@ -0,0 +1,28 @@ +/* + * (c) Copyright 2022 Palantir Technologies Inc. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.palantir.baseline.plugins.javaversions; + +import org.gradle.api.file.RegularFile; +import org.gradle.jvm.toolchain.JavaInstallationMetadata; + +final class JavaInstallationMetadataUtils { + public static RegularFile findExecutable(JavaInstallationMetadata javaInstallationMetadata, String executableName) { + return javaInstallationMetadata.getInstallationPath().dir("bin").file(executableName); + } + + private JavaInstallationMetadataUtils() {} +} diff --git a/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/javaversions/JavaToolchains.java b/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/javaversions/JavaToolchains.java new file mode 100644 index 000000000..93eda1cc6 --- /dev/null +++ b/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/javaversions/JavaToolchains.java @@ -0,0 +1,48 @@ +/* + * (c) Copyright 2022 Palantir Technologies Inc. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.palantir.baseline.plugins.javaversions; + +import com.palantir.baseline.extensions.BaselineJavaVersionsExtension; +import org.gradle.api.Project; +import org.gradle.api.provider.Provider; +import org.gradle.jvm.toolchain.JavaInstallationMetadata; +import org.gradle.jvm.toolchain.JavaLanguageVersion; +import org.gradle.jvm.toolchain.JavaToolchainService; + +public final class JavaToolchains { + private final Project project; + private final BaselineJavaVersionsExtension baselineJavaVersionsExtension; + + public JavaToolchains(Project project, BaselineJavaVersionsExtension baselineJavaVersionsExtension) { + this.project = project; + this.baselineJavaVersionsExtension = baselineJavaVersionsExtension; + } + + public Provider forVersion(Provider javaLanguageVersionProvider) { + return javaLanguageVersionProvider.flatMap(javaLanguageVersion -> { + Provider configuredJdk = + baselineJavaVersionsExtension.getJdks().getting(javaLanguageVersion); + + return configuredJdk + .map(_ignored -> new ConfiguredJavaToolchain(configuredJdk)) + .orElse(project.provider(() -> new FallbackGradleJavaToolchain( + project.getExtensions().getByType(JavaToolchainService.class), + javaToolchainSpec -> + javaToolchainSpec.getLanguageVersion().set(javaLanguageVersion)))); + }); + } +} diff --git a/gradle-baseline-java/src/test/groovy/com/palantir/baseline/BaselineJavaVersionIntegrationTest.groovy b/gradle-baseline-java/src/test/groovy/com/palantir/baseline/BaselineJavaVersionIntegrationTest.groovy index 1e8ea7c2e..740ce2660 100644 --- a/gradle-baseline-java/src/test/groovy/com/palantir/baseline/BaselineJavaVersionIntegrationTest.groovy +++ b/gradle-baseline-java/src/test/groovy/com/palantir/baseline/BaselineJavaVersionIntegrationTest.groovy @@ -16,10 +16,12 @@ package com.palantir.baseline - import nebula.test.IntegrationSpec import nebula.test.functional.ExecutionResult -import spock.lang.Unroll + +import java.nio.file.Files +import java.nio.file.Path +import java.nio.file.Paths class BaselineJavaVersionIntegrationTest extends IntegrationSpec { private static final int JAVA_8_BYTECODE = 52 @@ -71,7 +73,10 @@ class BaselineJavaVersionIntegrationTest extends IntegrationSpec { '''.stripIndent(true) def setup() { + // Fork needed or build fails on circleci with "SystemInfo is not supported on this operating system." + // Comment out locally in order to get debugging to work setFork(true) + buildFile << standardBuildFile } @@ -287,6 +292,50 @@ class BaselineJavaVersionIntegrationTest extends IntegrationSpec { runTasksSuccessfully('checkJavaVersions') } + def 'can configure a jdk path to be used'() { + Path newJavaHome = Files.createSymbolicLink( + projectDir.toPath().resolve("jdk"), + Paths.get(System.getProperty("java.home"))) + + // language=gradle + buildFile << """ + javaVersions { + libraryTarget = 11 + + jdks.put JavaLanguageVersion.of(11), new JavaInstallationMetadata() { + @Override + JavaLanguageVersion getLanguageVersion() { + return JavaLanguageVersion.of(11) + } + @Override + String getJavaRuntimeVersion() { + return '11.0.222' + } + @Override + String getJvmVersion() { + return '11.33.44' + } + @Override + String getVendor() { + return 'vendor' + } + @Override + Directory getInstallationPath() { + return layout.dir(provider { new File('$newJavaHome') }).get() + } + } + } + """.stripIndent(true) + + writeJavaSourceFile java11CompatibleCode + + when: + def stdout = runTasksSuccessfully('compileJava', '--stacktrace').standardOutput + + then: + stdout.contains(newJavaHome.toString()) + } + private static final int BYTECODE_IDENTIFIER = (int) 0xCAFEBABE // See http://illegalargumentexception.blogspot.com/2009/07/java-finding-class-versions.html