From 1bb1e74a9debc146016ccd936fa7c3eb39ce3623 Mon Sep 17 00:00:00 2001 From: Rob Winch Date: Mon, 16 Dec 2019 10:45:15 -0600 Subject: [PATCH] Add Gradle Lock Plugin Issue gh-7788 --- build.gradle | 1 + buildSrc/build.gradle | 4 ++ .../src/main/java/lock/GlobalLockPlugin.java | 16 ++++++++ .../src/main/java/lock/GlobalLockTask.java | 40 +++++++++++++++++++ gradle/dependency-management.gradle | 4 ++ 5 files changed, 65 insertions(+) create mode 100644 buildSrc/src/main/java/lock/GlobalLockPlugin.java create mode 100644 buildSrc/src/main/java/lock/GlobalLockTask.java diff --git a/build.gradle b/build.gradle index c1b838851d..ff9c247ff5 100644 --- a/build.gradle +++ b/build.gradle @@ -11,6 +11,7 @@ buildscript { } } apply plugin: 'io.spring.nohttp' +apply plugin: 'locks' apply plugin: 'io.spring.convention.root' group = 'org.springframework.security' diff --git a/buildSrc/build.gradle b/buildSrc/build.gradle index f5cd325b45..d3eeb801df 100644 --- a/buildSrc/build.gradle +++ b/buildSrc/build.gradle @@ -10,6 +10,10 @@ gradlePlugin { id = "trang" implementationClass = "trang.TrangPlugin" } + locks { + id = "locks" + implementationClass = "lock.GlobalLockPlugin" + } } } diff --git a/buildSrc/src/main/java/lock/GlobalLockPlugin.java b/buildSrc/src/main/java/lock/GlobalLockPlugin.java new file mode 100644 index 0000000000..955e73e681 --- /dev/null +++ b/buildSrc/src/main/java/lock/GlobalLockPlugin.java @@ -0,0 +1,16 @@ +package lock; + +import org.gradle.api.Plugin; +import org.gradle.api.Project; + +/** + * @author Rob Winch + */ +public class GlobalLockPlugin implements Plugin { + @Override + public void apply(Project project) { + project.getTasks().register("writeLocks", GlobalLockTask.class, writeAll -> { + writeAll.setDescription("Writes the locks for all projects"); + }); + } +} diff --git a/buildSrc/src/main/java/lock/GlobalLockTask.java b/buildSrc/src/main/java/lock/GlobalLockTask.java new file mode 100644 index 0000000000..20a8f8f18b --- /dev/null +++ b/buildSrc/src/main/java/lock/GlobalLockTask.java @@ -0,0 +1,40 @@ +package lock; + +import org.gradle.api.Action; +import org.gradle.api.DefaultTask; +import org.gradle.api.Project; +import org.gradle.api.artifacts.Configuration; +import org.gradle.api.tasks.TaskAction; + +import java.util.function.Consumer; + +/** + * @author Rob Winch + */ +public class GlobalLockTask extends DefaultTask { + @TaskAction + public void lock() { + Project taskProject = getProject(); + if (!taskProject.getGradle().getStartParameter().isWriteDependencyLocks()) { + throw new IllegalStateException("You just specify --write-locks argument"); + } + writeLocksFor(taskProject); + taskProject.getSubprojects().forEach(new Consumer() { + @Override + public void accept(Project subproject) { + writeLocksFor(subproject); + } + }); + } + + private void writeLocksFor(Project project) { + project.getConfigurations().configureEach(new Action() { + @Override + public void execute(Configuration configuration) { + if (configuration.isCanBeResolved()) { + configuration.resolve(); + } + } + }); + } +} diff --git a/gradle/dependency-management.gradle b/gradle/dependency-management.gradle index aa3b13c1c0..10f9001697 100644 --- a/gradle/dependency-management.gradle +++ b/gradle/dependency-management.gradle @@ -117,3 +117,7 @@ dependencies { management "org.thymeleaf:thymeleaf-spring5:3.0.11.RELEASE" } } + +dependencyLocking { + lockAllConfigurations() +}