From 57651e2cbd8b46c624ae12c5364d426ad9d28dc8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Kautler?= Date: Tue, 20 Feb 2024 11:43:39 +0100 Subject: [PATCH] Replace taskGraph.afterTask with an operation completion listener in Gradle 6.1+ --- build.gradle | 6 +++ ...BuildEventsListenerRegistryProvider.groovy | 10 ++++ .../researchgate/release/ReleasePlugin.groovy | 54 ++++++++++++++----- 3 files changed, 57 insertions(+), 13 deletions(-) create mode 100644 src/main/groovy/net/researchgate/release/BuildEventsListenerRegistryProvider.groovy diff --git a/build.gradle b/build.gradle index bd33bc96..4ee9cf54 100644 --- a/build.gradle +++ b/build.gradle @@ -1,3 +1,5 @@ +import org.gradle.api.internal.classpath.ModuleRegistry + buildscript { repositories { maven { @@ -33,6 +35,10 @@ dependencies { testCompile "cglib:cglib-nodep:3.2.8" testImplementation gradleTestKit() testImplementation("org.junit.jupiter:junit-jupiter:5.7.1") + + // work-around for https://github.com/gradle/gradle/issues/16774 + testRuntimeOnly(files(project.services.get(ModuleRegistry).getModule('gradle-tooling-api-builders').classpath.asFiles.first())) + testRuntimeOnly('com.google.guava:guava:27.1-android') } gradlePlugin { diff --git a/src/main/groovy/net/researchgate/release/BuildEventsListenerRegistryProvider.groovy b/src/main/groovy/net/researchgate/release/BuildEventsListenerRegistryProvider.groovy new file mode 100644 index 00000000..abf8b5b9 --- /dev/null +++ b/src/main/groovy/net/researchgate/release/BuildEventsListenerRegistryProvider.groovy @@ -0,0 +1,10 @@ +package net.researchgate.release + +import org.gradle.build.event.BuildEventsListenerRegistry + +import javax.inject.Inject + +interface BuildEventsListenerRegistryProvider { + @Inject + BuildEventsListenerRegistry getBuildEventsListenerRegistry(); +} diff --git a/src/main/groovy/net/researchgate/release/ReleasePlugin.groovy b/src/main/groovy/net/researchgate/release/ReleasePlugin.groovy index 20c45b9b..03c9748b 100644 --- a/src/main/groovy/net/researchgate/release/ReleasePlugin.groovy +++ b/src/main/groovy/net/researchgate/release/ReleasePlugin.groovy @@ -28,13 +28,19 @@ import org.gradle.api.GradleException import org.gradle.api.Plugin import org.gradle.api.Project import org.gradle.api.Task +import org.gradle.api.model.ObjectFactory import org.gradle.api.plugins.BasePlugin +import org.gradle.api.provider.ProviderFactory import org.gradle.api.tasks.GradleBuild import org.gradle.api.tasks.TaskState +import org.gradle.build.event.BuildEventsListenerRegistry +import org.gradle.tooling.events.OperationCompletionListener +import org.gradle.tooling.events.task.TaskFailureResult import org.gradle.util.GradleVersion -class ReleasePlugin extends PluginHelper implements Plugin { +import javax.inject.Inject +abstract class ReleasePlugin extends PluginHelper implements Plugin { static final String RELEASE_GROUP = 'Release' private BaseScmAdapter scmAdapter @@ -74,7 +80,7 @@ class ReleasePlugin extends PluginHelper implements Plugin { "${p}updateVersion" as String, "${p}commitNewVersion" as String ] - + // Gradle 6 workaround (https://github.com/gradle/gradle/issues/12872) buildName = project.name + "-release" } @@ -119,7 +125,7 @@ class ReleasePlugin extends PluginHelper implements Plugin { "${p}afterReleaseBuild" as String ].flatten() } - + // Gradle 6 workaround (https://github.com/gradle/gradle/issues/12872) buildName = project.name + "-release" } @@ -161,18 +167,40 @@ class ReleasePlugin extends PluginHelper implements Plugin { } } - project.gradle.taskGraph.afterTask { Task task, TaskState state -> - if (state.failure && task.name == "release") { - try { - createScmAdapter() - } catch (Exception ignored) {} - if (scmAdapter && extension.revertOnFail && project.file(extension.versionPropertyFile)?.exists()) { - log.error('Release process failed, reverting back any changes made by Release Plugin.') - scmAdapter.revert() - } else { - log.error('Release process failed, please remember to revert any uncommitted changes made by the Release Plugin.') + if (GradleVersion.current() < GradleVersion.version('6.1')) { + project.gradle.taskGraph.afterTask { Task task, TaskState state -> + if (state.failure && task.name == 'release') { + revert() } } + } else { + objects.newInstance(BuildEventsListenerRegistryProvider) + .buildEventsListenerRegistry + .onTaskCompletion(providers.provider { + { finishEvent -> + if ((finishEvent.result instanceof TaskFailureResult) && finishEvent.descriptor.taskPath.endsWith(':release')) { + revert() + } + } as OperationCompletionListener + }) + } + } + + @Inject + abstract protected ObjectFactory getObjects(); + + @Inject + abstract protected ProviderFactory getProviders(); + + protected revert() { + try { + createScmAdapter() + } catch (Exception ignored) {} + if (scmAdapter && extension.revertOnFail.get() && project.file(extension.versionPropertyFile)?.exists()) { + log.error('Release process failed, reverting back any changes made by Release Plugin.') + scmAdapter.revert() + } else { + log.error('Release process failed, please remember to revert any uncommitted changes made by the Release Plugin.') } }