Skip to content

Commit

Permalink
Merge pull request #89 from nebula-plugins/toolchain-support
Browse files Browse the repository at this point in the history
InfoJavaPlugin: add toolchain support for Build-Java-Version
  • Loading branch information
rpalcolea authored Jul 23, 2021
2 parents df91c52 + c143c17 commit d404d81
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 4 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/nebula.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
20 changes: 18 additions & 2 deletions src/main/groovy/nebula/plugin/info/java/InfoJavaPlugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -52,10 +56,8 @@ class InfoJavaPlugin implements Plugin<Project>, 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
Expand All @@ -66,8 +68,22 @@ class InfoJavaPlugin implements Plugin<Project>, InfoCollectorPlugin {
project.plugins.withType(InfoBrokerPlugin) { InfoBrokerPlugin manifestPlugin ->
manifestPlugin.add(TARGET_PROPERTY, { javaConvention.targetCompatibility } )
manifestPlugin.add(SOURCE_PROPERTY, { javaConvention.sourceCompatibility } )
Provider<JavaLauncher> 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<JavaLauncher> getJavaLauncher(Project project) {
def toolchain = project.getExtensions().getByType(JavaPluginExtension.class).toolchain
JavaToolchainService service = project.getExtensions().getByType(JavaToolchainService.class)
Provider<JavaLauncher> launcher = service.launcherFor(toolchain)
return launcher
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,4 @@ class InfoPluginIntegrationSpec extends IntegrationSpec {
then:
result.success
}


}
Original file line number Diff line number Diff line change
@@ -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')
}

}

0 comments on commit d404d81

Please sign in to comment.