-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathInfoJavaPlugin.groovy
More file actions
122 lines (103 loc) · 5.13 KB
/
InfoJavaPlugin.groovy
File metadata and controls
122 lines (103 loc) · 5.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
* Copyright 2014-2019 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.java
import groovy.transform.Canonical
import groovy.transform.CompileDynamic
import nebula.plugin.info.InfoBrokerPlugin
import nebula.plugin.info.InfoCollectorPlugin
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.plugins.JavaBasePlugin
import org.gradle.api.plugins.JavaPluginExtension
import org.gradle.api.provider.Provider
import org.gradle.api.provider.ProviderFactory
import org.gradle.api.tasks.SourceSet
import org.gradle.api.tasks.compile.AbstractCompile
import org.gradle.jvm.toolchain.JavaLauncher
import org.gradle.jvm.toolchain.JavaToolchainService
import javax.inject.Inject
/**
* Collect Java relevant fields.
*/
class InfoJavaPlugin implements Plugin<Project>, InfoCollectorPlugin {
// Apache Commons has a standard convention for these variables
// http://commons.apache.org/releases/prepare.html
static final String CREATED_PROPERTY = 'Created-By' // E.g. Created-By: 1.5.0_13-119 (Apple Inc.)
static final String JDK_PROPERTY = 'Build-Java-Version'
static final String SOURCE_PROPERTY = 'X-Compile-Source-JDK'
static final String TARGET_PROPERTY = 'X-Compile-Target-JDK'
private final ProviderFactory providers
private static final List<String> supportedLanguages = ['java', 'groovy', 'scala', 'kotlin']
@Inject
InfoJavaPlugin(ProviderFactory providerFactory) {
this.providers = providerFactory
}
void apply(Project project) {
// This can't change, so we can commit it early
project.plugins.withType(InfoBrokerPlugin) { InfoBrokerPlugin manifestPlugin ->
String javaRuntimeVersion = providers.systemProperty("java.runtime.version").getOrElse("unknown")
String javaVmVendor = providers.systemProperty("java.vm.vendor").getOrElse("unknown")
manifestPlugin.add(CREATED_PROPERTY, "$javaRuntimeVersion ($javaVmVendor)")
}
// After-evaluating, because we need to give user a chance to effect the extension
project.afterEvaluate {
project.plugins.withType(JavaBasePlugin) {
project.plugins.withType(InfoBrokerPlugin) { InfoBrokerPlugin manifestPlugin ->
JavaCompatibility sourceAndTargetCompatibility = findSourceAndTargetCompatibility(project)
manifestPlugin.add(TARGET_PROPERTY, sourceAndTargetCompatibility.targetCompatibility)
manifestPlugin.add(SOURCE_PROPERTY, sourceAndTargetCompatibility.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").getOrElse("unknown")
manifestPlugin.add(JDK_PROPERTY, javaVersionFromSystemProperty)
}
}
}
}
}
@CompileDynamic
private static JavaCompatibility findSourceAndTargetCompatibility(Project project) {
JavaPluginExtension javaPluginExtension = project.extensions.getByType(JavaPluginExtension)
SourceSet mainSourceSet = javaPluginExtension.sourceSets.findByName("main")
if(!mainSourceSet) {
return new JavaCompatibility(javaPluginExtension.sourceCompatibility.toString(), javaPluginExtension.targetCompatibility.toString())
}
List<String> compileTaskNames = supportedLanguages.collect { mainSourceSet.getCompileTaskName(it)}
List<AbstractCompile> compileTasks = project.tasks.withType(AbstractCompile).matching {
it.name in compileTaskNames
}.toList()
if(!compileTasks) {
return JavaCompatibility.UNKNOWN
}
return new JavaCompatibility(
compileTasks.sourceCompatibility.sort().last(), compileTasks.targetCompatibility.sort().last()
)
}
@Canonical
private static class JavaCompatibility {
String sourceCompatibility
String targetCompatibility
static UNKNOWN = new JavaCompatibility("unknown", "unknown")
}
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
}
}