From c143c1787cbdd831bfd8fd67b507176e61c229d4 Mon Sep 17 00:00:00 2001 From: Roberto Perez Alcolea Date: Wed, 21 Jul 2021 10:05:47 -0700 Subject: [PATCH] InfoJavaPlugin: add toolchain support for Build-Java-Version --- .github/workflows/nebula.yml | 4 ++ .../plugin/info/java/InfoJavaPlugin.groovy | 20 +++++- .../info/InfoPluginIntegrationSpec.groovy | 2 - .../InfoPluginIntegrationTestKitSpec.groovy | 68 +++++++++++++++++++ 4 files changed, 90 insertions(+), 4 deletions(-) create mode 100644 src/test/groovy/nebula/plugin/info/InfoPluginIntegrationTestKitSpec.groovy diff --git a/.github/workflows/nebula.yml b/.github/workflows/nebula.yml index c0bad5b..32c5d8c 100644 --- a/.github/workflows/nebula.yml +++ b/.github/workflows/nebula.yml @@ -26,6 +26,10 @@ jobs: name: Gradle Build without Publish steps: - uses: actions/checkout@v1 + - name: Set up JDK 16 + uses: actions/setup-java@v1 + with: + java-version: 16 - name: Setup jdk uses: actions/setup-java@v1 with: diff --git a/src/main/groovy/nebula/plugin/info/java/InfoJavaPlugin.groovy b/src/main/groovy/nebula/plugin/info/java/InfoJavaPlugin.groovy index 37acd4b..2d6c7b5 100644 --- a/src/main/groovy/nebula/plugin/info/java/InfoJavaPlugin.groovy +++ b/src/main/groovy/nebula/plugin/info/java/InfoJavaPlugin.groovy @@ -22,7 +22,11 @@ import org.gradle.api.Plugin import org.gradle.api.Project import org.gradle.api.plugins.JavaBasePlugin import org.gradle.api.plugins.JavaPluginConvention +import org.gradle.api.plugins.JavaPluginExtension +import org.gradle.api.provider.Provider import org.gradle.api.provider.ProviderFactory +import org.gradle.jvm.toolchain.JavaLauncher +import org.gradle.jvm.toolchain.JavaToolchainService import javax.inject.Inject @@ -52,10 +56,8 @@ class InfoJavaPlugin implements Plugin, InfoCollectorPlugin { project.plugins.withType(InfoBrokerPlugin) { InfoBrokerPlugin manifestPlugin -> String javaRuntimeVersion = providers.systemProperty("java.runtime.version").forUseAtConfigurationTime().get() String javaVmVendor = providers.systemProperty("java.vm.vendor").forUseAtConfigurationTime().get() - String javaVersion = providers.systemProperty("java.version").forUseAtConfigurationTime().get() manifestPlugin.add(CREATED_PROPERTY, "$javaRuntimeVersion ($javaVmVendor)") - manifestPlugin.add(JDK_PROPERTY, javaVersion) } // After-evaluating, because we need to give user a chance to effect the extension @@ -66,8 +68,22 @@ class InfoJavaPlugin implements Plugin, InfoCollectorPlugin { project.plugins.withType(InfoBrokerPlugin) { InfoBrokerPlugin manifestPlugin -> manifestPlugin.add(TARGET_PROPERTY, { javaConvention.targetCompatibility } ) manifestPlugin.add(SOURCE_PROPERTY, { javaConvention.sourceCompatibility } ) + Provider javaLauncher = getJavaLauncher(project) + if(javaLauncher.isPresent()) { + manifestPlugin.add(JDK_PROPERTY, javaLauncher.get().metadata.languageVersion.toString()) + } else { + String javaVersionFromSystemProperty = providers.systemProperty("java.version").forUseAtConfigurationTime().get() + manifestPlugin.add(JDK_PROPERTY, javaVersionFromSystemProperty) + } } } } } + + private Provider getJavaLauncher(Project project) { + def toolchain = project.getExtensions().getByType(JavaPluginExtension.class).toolchain + JavaToolchainService service = project.getExtensions().getByType(JavaToolchainService.class) + Provider launcher = service.launcherFor(toolchain) + return launcher + } } diff --git a/src/test/groovy/nebula/plugin/info/InfoPluginIntegrationSpec.groovy b/src/test/groovy/nebula/plugin/info/InfoPluginIntegrationSpec.groovy index 47f7d62..c53a3fe 100644 --- a/src/test/groovy/nebula/plugin/info/InfoPluginIntegrationSpec.groovy +++ b/src/test/groovy/nebula/plugin/info/InfoPluginIntegrationSpec.groovy @@ -255,6 +255,4 @@ class InfoPluginIntegrationSpec extends IntegrationSpec { then: result.success } - - } diff --git a/src/test/groovy/nebula/plugin/info/InfoPluginIntegrationTestKitSpec.groovy b/src/test/groovy/nebula/plugin/info/InfoPluginIntegrationTestKitSpec.groovy new file mode 100644 index 0000000..f74e54e --- /dev/null +++ b/src/test/groovy/nebula/plugin/info/InfoPluginIntegrationTestKitSpec.groovy @@ -0,0 +1,68 @@ +/* + * Copyright 2021 Netflix, Inc. + * + * 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 nebula.plugin.info + +import nebula.test.IntegrationTestKitSpec + +class InfoPluginIntegrationTestKitSpec extends IntegrationTestKitSpec { + def 'it returns manifest reports at the end of the build - toolchains'() { + given: + buildFile << """ + buildscript { + repositories { + mavenCentral() + } + dependencies { + classpath "com.google.guava:guava:21.0" + } + } + + plugins { + id 'java' + id 'nebula.info' + } + + java { + toolchain { + languageVersion = JavaLanguageVersion.of(16) + } + } + + repositories { mavenCentral() } + dependencies { + implementation 'com.google.guava:guava:18.0' + } + def broker = project.plugins.getPlugin(${InfoBrokerPlugin.name}) + + gradle.buildFinished { + println broker.buildManifest() + } + """.stripIndent() + + settingsFile << """ + rootProject.name='buildscript-singlemodule-test' + """ + this.writeHelloWorld('com.nebula.test') + + when: + def result = runTasks('assemble') + + then: + println result.output + result.output.contains('Build-Java-Version=16') + } + +}