|
1 | 1 | import org.gradle.util.GradleVersion
|
2 | 2 | import java.nio.charset.StandardCharsets;
|
3 | 3 |
|
4 |
| -// Don't run against the included builds (if the main build has any). |
| 4 | +if (GradleVersion.current() < GradleVersion.version("5.0")) { |
| 5 | + throw new IllegalStateException("Build validation not supported for Gradle ${GradleVersion.current()}. Upgrade your project's build to Gradle 5 or newer.") |
| 6 | +} |
| 7 | + |
| 8 | +// note that there is no mechanism to share code between the initscript{} block and the main script, so some logic is duplicated |
| 9 | + |
| 10 | +// conditionally apply the GE / Build Scan plugin to the classpath so it can be applied to the build further down in this script |
5 | 11 | initscript {
|
6 | 12 | def isTopLevelBuild = !gradle.parent
|
7 | 13 | if (!isTopLevelBuild) {
|
8 | 14 | return
|
9 | 15 | }
|
10 | 16 |
|
11 |
| - def enableGradleEnterprise = System.getProperty("com.gradle.enterprise.build_validation.enableGradleEnterprise") |
12 |
| - if (!enableGradleEnterprise) { |
13 |
| - return |
14 |
| - } |
| 17 | + def enableGradleEnterprise = Boolean.getBoolean("com.gradle.enterprise.build_validation.enableGradleEnterprise") |
15 | 18 |
|
16 |
| - def gradleEnterprisePluginVersion = "3.10.3" |
17 |
| - def commonCustomUserDataPluginVersion = "1.7.2" |
| 19 | + def gePluginVersion = enableGradleEnterprise ? "3.10.3" : null |
| 20 | + def ccudPluginVersion = enableGradleEnterprise ? "1.7.2" : null |
18 | 21 |
|
19 | 22 | repositories {
|
20 | 23 | gradlePluginPortal()
|
21 | 24 | }
|
22 | 25 |
|
23 | 26 | dependencies {
|
24 |
| - classpath("com.gradle:gradle-enterprise-gradle-plugin:${gradleEnterprisePluginVersion}") |
25 |
| - classpath("com.gradle:common-custom-user-data-gradle-plugin:${commonCustomUserDataPluginVersion}") |
26 |
| - } |
27 |
| -} |
| 27 | + if (gePluginVersion) { |
| 28 | + classpath "com.gradle:gradle-enterprise-gradle-plugin:$gePluginVersion" |
| 29 | + } |
28 | 30 |
|
29 |
| -// Don't run against the included builds (if the main build has any). |
30 |
| -def isTopLevelBuild = !gradle.parent |
31 |
| -if (!isTopLevelBuild) { |
32 |
| - return |
| 31 | + if (ccudPluginVersion) { |
| 32 | + classpath "com.gradle:common-custom-user-data-gradle-plugin:$ccudPluginVersion" |
| 33 | + } |
| 34 | + } |
33 | 35 | }
|
34 | 36 |
|
| 37 | +def BUILD_SCAN_PLUGIN_ID = 'com.gradle.build-scan' |
35 | 38 | def BUILD_SCAN_PLUGIN_CLASS = 'com.gradle.scan.plugin.BuildScanPlugin'
|
36 | 39 |
|
37 | 40 | def GRADLE_ENTERPRISE_PLUGIN_ID = 'com.gradle.enterprise'
|
38 | 41 | def GRADLE_ENTERPRISE_PLUGIN_CLASS = 'com.gradle.enterprise.gradleplugin.GradleEnterprisePlugin'
|
| 42 | +def GRADLE_ENTERPRISE_EXTENSION_CLASS = 'com.gradle.enterprise.gradleplugin.GradleEnterpriseExtension' |
39 | 43 |
|
40 | 44 | def CCUD_PLUGIN_ID = 'com.gradle.common-custom-user-data-gradle-plugin'
|
41 | 45 | def CCUD_PLUGIN_CLASS = 'com.gradle.CommonCustomUserDataGradlePlugin'
|
42 | 46 |
|
43 |
| -def enableGradleEnterprise = System.getProperty("com.gradle.enterprise.build_validation.enableGradleEnterprise") |
| 47 | +def isTopLevelBuild = !gradle.parent |
| 48 | +if (!isTopLevelBuild) { |
| 49 | + return |
| 50 | +} |
44 | 51 |
|
45 |
| -def version = GradleVersion.current().baseVersion |
46 |
| -def atLeastGradle5 = version >= GradleVersion.version("5.0") |
47 |
| -def atLeastGradle6 = version >= GradleVersion.version("6.0") |
| 52 | +def enableGradleEnterprise = Boolean.getBoolean("com.gradle.enterprise.build_validation.enableGradleEnterprise") |
48 | 53 |
|
49 | 54 | clearWarnings()
|
50 | 55 |
|
51 |
| -if (atLeastGradle6) { |
52 |
| - settingsEvaluated { settings -> |
53 |
| - if (!settings.pluginManager.hasPlugin(GRADLE_ENTERPRISE_PLUGIN_ID)) { |
54 |
| - if (enableGradleEnterprise) { |
55 |
| - logger.quiet("Applying $GRADLE_ENTERPRISE_PLUGIN_CLASS via init script") |
56 |
| - settings.pluginManager.apply(initscript.classLoader.loadClass(GRADLE_ENTERPRISE_PLUGIN_CLASS)) |
57 |
| - } else { |
58 |
| - throw new IllegalStateException("The com.gradle.enterprise plugin is missing from the project.\n" + |
59 |
| - "Either apply it directly (see https://docs.gradle.com/enterprise/gradle-plugin/#gradle_6_x_and_later),\n" + |
60 |
| - "or use `--enable-gradle-enterprise` when running the build validation script.") |
61 |
| - } |
| 56 | +// define a buildScanPublished listener that captures the build scan URL and sends it in a message to the server |
| 57 | +def buildScanPublishedAction = { def buildScan, rootProjectName -> |
| 58 | + if (buildScan.metaClass.respondsTo(buildScan, 'buildScanPublished', Action)) { |
| 59 | + def scanFile = new File(experimentDir, "build-scans.csv") |
| 60 | + buildScan.buildScanPublished { publishedBuildScan -> |
| 61 | + def buildScanUri = publishedBuildScan.buildScanUri |
| 62 | + def buildScanId = publishedBuildScan.buildScanId |
| 63 | + def port = (buildScanUri.port != -1) ? ":" + buildScanUri.port : "" |
| 64 | + def baseUrl = "${buildScanUri.scheme}://${buildScanUri.host}${port}" |
| 65 | + scanFile.append("${rootProjectName},${baseUrl},${buildScanUri},${buildScanId}\n") |
62 | 66 | }
|
63 |
| - if (!settings.pluginManager.hasPlugin(CCUD_PLUGIN_ID)) { |
64 |
| - if (enableGradleEnterprise) { |
65 |
| - logger.quiet("Applying $CCUD_PLUGIN_CLASS via init script") |
66 |
| - settings.pluginManager.apply(initscript.classLoader.loadClass(CCUD_PLUGIN_CLASS)) |
67 |
| - } else { |
68 |
| - logWarningMissingCommonCustomUserDataGradlePlugin() |
69 |
| - } |
| 67 | + def errorFile = new File(experimentDir, "build-scan-publish-error.txt") |
| 68 | + buildScan.onError { error -> |
| 69 | + errorFile.text = error |
70 | 70 | }
|
71 |
| - |
72 |
| - configureGradleEnterprise(settings.extensions["gradleEnterprise"], settings.rootProject.name) |
73 | 71 | }
|
74 |
| -} else if (atLeastGradle5) { |
| 72 | +} |
| 73 | + |
| 74 | +// register buildScanPublished listener and optionally apply the GE / Build Scan plugin |
| 75 | +if (GradleVersion.current() < GradleVersion.version('6.0')) { |
75 | 76 | rootProject {
|
76 | 77 | buildscript.configurations.getByName("classpath").incoming.afterResolve { ResolvableDependencies incoming ->
|
77 | 78 | def resolutionResult = incoming.resolutionResult
|
@@ -102,51 +103,65 @@ if (atLeastGradle6) {
|
102 | 103 | }
|
103 | 104 | }
|
104 | 105 | }
|
| 106 | + |
| 107 | + pluginManager.withPlugin(BUILD_SCAN_PLUGIN_ID) { |
| 108 | + buildScanPublishedAction(buildScan, gradle.rootProject.name) |
| 109 | + } |
105 | 110 | }
|
106 | 111 |
|
107 | 112 | projectsEvaluated { gradle ->
|
108 |
| - configureGradleEnterprise(gradle.rootProject.extensions["gradleEnterprise"], gradle.rootProject.name) |
| 113 | + configureGradleEnterprise(gradle.rootProject.extensions["gradleEnterprise"]) |
109 | 114 | }
|
110 | 115 | } else {
|
111 |
| - throw new IllegalStateException("Build validation not supported for Gradle ${GradleVersion.current()}. Upgrade your project's build to Gradle 5 or newer.") |
| 116 | + gradle.settingsEvaluated { settings -> |
| 117 | + if (!settings.pluginManager.hasPlugin(GRADLE_ENTERPRISE_PLUGIN_ID)) { |
| 118 | + if (enableGradleEnterprise) { |
| 119 | + logger.quiet("Applying $GRADLE_ENTERPRISE_PLUGIN_CLASS via init script") |
| 120 | + settings.pluginManager.apply(initscript.classLoader.loadClass(GRADLE_ENTERPRISE_PLUGIN_CLASS)) |
| 121 | + } else { |
| 122 | + throw new IllegalStateException("The com.gradle.enterprise plugin is missing from the project.\n" + |
| 123 | + "Either apply it directly (see https://docs.gradle.com/enterprise/gradle-plugin/#gradle_6_x_and_later),\n" + |
| 124 | + "or use `--enable-gradle-enterprise` when running the build validation script.") |
| 125 | + } |
| 126 | + } |
| 127 | + if (!settings.pluginManager.hasPlugin(CCUD_PLUGIN_ID)) { |
| 128 | + if (enableGradleEnterprise) { |
| 129 | + logger.quiet("Applying $CCUD_PLUGIN_CLASS via init script") |
| 130 | + settings.pluginManager.apply(initscript.classLoader.loadClass(CCUD_PLUGIN_CLASS)) |
| 131 | + } else { |
| 132 | + logWarningMissingCommonCustomUserDataGradlePlugin() |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + extensionsWithPublicType(settings, GRADLE_ENTERPRISE_EXTENSION_CLASS).collect { settings[it.name] }.each { ext -> |
| 137 | + configureGradleEnterprise(ext) |
| 138 | + buildScanPublishedAction(ext.buildScan, settings.rootProject.name) |
| 139 | + } |
| 140 | + } |
112 | 141 | }
|
113 | 142 |
|
114 |
| -void configureGradleEnterprise(gradleEnterprise, rootProjectName) { |
| 143 | +static def extensionsWithPublicType(def container, String publicType) { |
| 144 | + container.extensions.extensionsSchema.elements.findAll { it.publicType.concreteClass.name == publicType } |
| 145 | +} |
| 146 | + |
| 147 | +void configureGradleEnterprise(gradleEnterprise) { |
115 | 148 | gradleEnterprise.with {
|
116 |
| - buildScan { |
117 |
| - def serverOverride = System.getProperty("com.gradle.enterprise.build_validation.server") |
118 |
| - if (serverOverride) { |
119 |
| - server = serverOverride |
120 |
| - } else if (!server) { |
121 |
| - throw new IllegalStateException("A Gradle Enterprise server URL has not been configured.", null) |
122 |
| - } |
| 149 | + def serverOverride = System.getProperty("com.gradle.enterprise.build_validation.server") |
| 150 | + if (serverOverride) { |
| 151 | + server = serverOverride |
| 152 | + } else if (!server) { |
| 153 | + throw new IllegalStateException("A Gradle Enterprise server URL has not been configured.", null) |
| 154 | + } |
123 | 155 |
|
| 156 | + buildScan { |
124 | 157 | // captureTaskInputFiles = true (too late to be set here for Gradle 5, set via sys prop)
|
125 | 158 | uploadInBackground = false
|
126 | 159 | publishAlways()
|
127 | 160 | }
|
128 |
| - captureBuildScanLinks(buildScan, rootProjectName) |
129 | 161 | addCustomData(buildScan)
|
130 | 162 | }
|
131 | 163 | }
|
132 | 164 |
|
133 |
| -def captureBuildScanLinks(buildScan, rootProjectName) { |
134 |
| - buildScan.with { |
135 |
| - def scanFile = new File(experimentDir, "build-scans.csv") |
136 |
| - buildScanPublished { publishedBuildScan -> |
137 |
| - def buildScanUri = publishedBuildScan.buildScanUri |
138 |
| - def buildScanId = publishedBuildScan.buildScanId |
139 |
| - def port = (buildScanUri.port != -1) ? ":" + buildScanUri.port : "" |
140 |
| - def baseUrl = "${buildScanUri.scheme}://${buildScanUri.host}${port}" |
141 |
| - scanFile.append("${rootProjectName},${baseUrl},${buildScanUri},${buildScanId}\n") |
142 |
| - } |
143 |
| - def errorFile = new File(experimentDir, "build-scan-publish-error.txt") |
144 |
| - onError { error -> |
145 |
| - errorFile.text = error |
146 |
| - } |
147 |
| - } |
148 |
| -} |
149 |
| - |
150 | 165 | void addCustomData(buildScan) {
|
151 | 166 | def projectProperties = gradle.startParameter.projectProperties
|
152 | 167 |
|
|
0 commit comments