Skip to content

Commit 814c67b

Browse files
committed
Add settings plugin to detect cycles between projects
1 parent fbd4b68 commit 814c67b

File tree

5 files changed

+143
-0
lines changed

5 files changed

+143
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0"?>
2+
<!DOCTYPE module PUBLIC
3+
"-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
4+
"https://checkstyle.org/dtds/configuration_1_3.dtd">
5+
<module name="com.puppycrawl.tools.checkstyle.Checker">
6+
<module name="io.spring.javaformat.checkstyle.SpringChecks">
7+
<property name="excludes" value="com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocPackageCheck" />
8+
</module>
9+
</module>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
plugins {
2+
id 'java-gradle-plugin'
3+
id "checkstyle"
4+
id "io.spring.javaformat" version "$javaFormatVersion"
5+
6+
}
7+
8+
repositories {
9+
mavenCentral()
10+
}
11+
12+
checkstyle {
13+
toolVersion = "${checkstyleToolVersion}"
14+
}
15+
16+
dependencies {
17+
checkstyle("com.puppycrawl.tools:checkstyle:${checkstyle.toolVersion}")
18+
checkstyle("io.spring.javaformat:spring-javaformat-checkstyle:${javaFormatVersion}")
19+
20+
implementation("org.jgrapht:jgrapht-core:1.5.2")
21+
}
22+
23+
gradlePlugin {
24+
plugins {
25+
cycleDetectionPlugin {
26+
id = "org.springframework.boot.cycle-detection"
27+
implementationClass = "org.springframework.boot.build.cycledetection.CycleDetectionPlugin"
28+
}
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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+
}

gradle/plugins/settings.gradle

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
pluginManagement {
2+
new File(rootDir.parentFile.parentFile, "gradle.properties").withInputStream {
3+
def properties = new Properties()
4+
properties.load(it)
5+
properties.forEach(settings.ext::set)
6+
gradle.rootProject {
7+
properties.forEach(project.ext::set)
8+
}
9+
}
10+
}
11+
12+
include 'cycle-detection-plugin'

settings.gradle

+2
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@ pluginManagement {
1515
}
1616
}
1717
}
18+
includeBuild("gradle/plugins")
1819
}
1920

2021
plugins {
2122
id "io.spring.develocity.conventions" version "0.0.22"
23+
id "org.springframework.boot.cycle-detection"
2224
}
2325

2426
rootProject.name="spring-boot-build"

0 commit comments

Comments
 (0)