|
| 1 | +/* |
| 2 | + * Copyright 2012-2025 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.build.cycledetection; |
| 18 | + |
| 19 | +import java.util.HashMap; |
| 20 | +import java.util.LinkedHashSet; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Map; |
| 23 | +import java.util.Set; |
| 24 | + |
| 25 | +import org.gradle.api.GradleException; |
| 26 | +import org.gradle.api.Plugin; |
| 27 | +import org.gradle.api.Project; |
| 28 | +import org.gradle.api.Task; |
| 29 | +import org.gradle.api.execution.TaskExecutionGraph; |
| 30 | +import org.gradle.api.initialization.Settings; |
| 31 | +import org.jgrapht.Graph; |
| 32 | +import org.jgrapht.alg.cycle.TarjanSimpleCycles; |
| 33 | +import org.jgrapht.graph.DefaultDirectedGraph; |
| 34 | +import org.jgrapht.graph.DefaultEdge; |
| 35 | + |
| 36 | +/** |
| 37 | + * A {@link Settings} {@link Plugin plugin} to detect cycles between a build's projects. |
| 38 | + * |
| 39 | + * @author Andy Wilkinson |
| 40 | + */ |
| 41 | +public class CycleDetectionPlugin implements Plugin<Settings> { |
| 42 | + |
| 43 | + @Override |
| 44 | + public void apply(Settings settings) { |
| 45 | + settings.getGradle().getTaskGraph().whenReady(this::detectCycles); |
| 46 | + } |
| 47 | + |
| 48 | + private void detectCycles(TaskExecutionGraph taskGraph) { |
| 49 | + Map<Project, Set<Project>> dependenciesByProject = getProjectsAndDependencies(taskGraph); |
| 50 | + Graph<String, DefaultEdge> graph = createGraph(dependenciesByProject); |
| 51 | + List<List<String>> cycles = findCycles(graph); |
| 52 | + if (!cycles.isEmpty()) { |
| 53 | + StringBuilder message = new StringBuilder("Cycles detected:\n"); |
| 54 | + for (List<String> cycle : cycles) { |
| 55 | + cycle.add(cycle.get(0)); |
| 56 | + message.append(" " + String.join(" -> ", cycle) + "\n"); |
| 57 | + } |
| 58 | + throw new GradleException(message.toString()); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + private Map<Project, Set<Project>> getProjectsAndDependencies(TaskExecutionGraph taskGraph) { |
| 63 | + Map<Project, Set<Project>> dependenciesByProject = new HashMap<>(); |
| 64 | + for (Task task : taskGraph.getAllTasks()) { |
| 65 | + Project project = task.getProject(); |
| 66 | + Set<Project> dependencies = dependenciesByProject.computeIfAbsent(project, (p) -> new LinkedHashSet<>()); |
| 67 | + taskGraph.getDependencies(task) |
| 68 | + .stream() |
| 69 | + .map(Task::getProject) |
| 70 | + .filter((taskProject) -> !taskProject.equals(project)) |
| 71 | + .forEach(dependencies::add); |
| 72 | + } |
| 73 | + return dependenciesByProject; |
| 74 | + } |
| 75 | + |
| 76 | + private Graph<String, DefaultEdge> createGraph(Map<Project, Set<Project>> dependenciesByProject) { |
| 77 | + Graph<String, DefaultEdge> graph = new DefaultDirectedGraph<>(DefaultEdge.class); |
| 78 | + dependenciesByProject.keySet().forEach((project) -> graph.addVertex(project.getName())); |
| 79 | + dependenciesByProject.forEach((project, dependencies) -> dependencies |
| 80 | + .forEach((dependency) -> graph.addEdge(project.getName(), dependency.getName()))); |
| 81 | + return graph; |
| 82 | + } |
| 83 | + |
| 84 | + private List<List<String>> findCycles(Graph<String, DefaultEdge> graph) { |
| 85 | + TarjanSimpleCycles<String, DefaultEdge> simpleCycles = new TarjanSimpleCycles<>(graph); |
| 86 | + List<List<String>> cycles = simpleCycles.findSimpleCycles(); |
| 87 | + return cycles; |
| 88 | + } |
| 89 | + |
| 90 | +} |
0 commit comments