Skip to content

Commit

Permalink
Set gradle.enterprise.externally-applied before applying GE plugin (#…
Browse files Browse the repository at this point in the history
…287)

* Set `gradle.enterprise.externally-applied` before applying GE plugin

In order for the Gradle Enterprise plugin to deactivate any potentially
build-changing features (at the time of writing that means all of its
testing features) when applied via the init script rather than directly
in the build, the `gradle.enterprise.externally-applied` system property
is now set before applying it and reset afterwards.

* Move above static methods

* Extract variable for property name to remove duplication

* Update build-scan-init.gradle

Rename local variable

* Add entry to the release notes

* Polish release notes

* Polish the release notes some more

---------

Co-authored-by: Etienne Studer <[email protected]>
  • Loading branch information
marcphilipp and etiennestuder authored Jul 27, 2023
1 parent 45b2352 commit 2467ced
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
20 changes: 17 additions & 3 deletions agent/src/main/resources/build-scan-init.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ if (GradleVersion.current() < GradleVersion.version('6.0')) {
if (!scanPluginComponent) {
logger.quiet("Applying $BUILD_SCAN_PLUGIN_CLASS via init script")
logger.quiet("Connection to Gradle Enterprise: $geUrl, allowUntrustedServer: $geAllowUntrustedServer")
pluginManager.apply(initscript.classLoader.loadClass(BUILD_SCAN_PLUGIN_CLASS))
applyPluginExternally(pluginManager, BUILD_SCAN_PLUGIN_CLASS)
buildScan.server = geUrl
buildScan.allowUntrustedServer = geAllowUntrustedServer
buildScan.publishAlways()
Expand Down Expand Up @@ -161,7 +161,7 @@ if (GradleVersion.current() < GradleVersion.version('6.0')) {
if (!settings.pluginManager.hasPlugin(GRADLE_ENTERPRISE_PLUGIN_ID)) {
logger.quiet("Applying $GRADLE_ENTERPRISE_PLUGIN_CLASS via init script")
logger.quiet("Connection to Gradle Enterprise: $geUrl, allowUntrustedServer: $geAllowUntrustedServer")
settings.pluginManager.apply(initscript.classLoader.loadClass(GRADLE_ENTERPRISE_PLUGIN_CLASS))
applyPluginExternally(settings.pluginManager, GRADLE_ENTERPRISE_PLUGIN_CLASS)
extensionsWithPublicType(settings, GRADLE_ENTERPRISE_EXTENSION_CLASS).collect { settings[it.name] }.each { ext ->
ext.server = geUrl
ext.allowUntrustedServer = geAllowUntrustedServer
Expand Down Expand Up @@ -193,6 +193,21 @@ if (GradleVersion.current() < GradleVersion.version('6.0')) {
}
}

void applyPluginExternally(PluginManager pluginManager, String pluginClassName) {
def externallyApplied = 'gradle.enterprise.externally-applied'
def oldValue = System.getProperty(externallyApplied)
System.setProperty(externallyApplied, 'true')
try {
pluginManager.apply(initscript.classLoader.loadClass(pluginClassName))
} finally {
if (oldValue == null) {
System.clearProperty(externallyApplied)
} else {
System.setProperty(externallyApplied, oldValue)
}
}
}

static def extensionsWithPublicType(def container, String publicType) {
container.extensions.extensionsSchema.elements.findAll { it.publicType.concreteClass.name == publicType }
}
Expand Down Expand Up @@ -235,4 +250,3 @@ class BuildScanLifeCycleLogger {
}

}

2 changes: 1 addition & 1 deletion release/changes.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
- TBD
- Informs the Gradle Enterprise Gradle plugin that it is being applied externally.

0 comments on commit 2467ced

Please sign in to comment.