Skip to content

Commit

Permalink
Only add spring dependency management plugin explicitly if features f…
Browse files Browse the repository at this point in the history
…rom it are in use
  • Loading branch information
sambsnyd committed Jan 22, 2025
1 parent 2276dd9 commit f05e1f8
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@
import org.openrewrite.gradle.IsBuildGradle;
import org.openrewrite.gradle.marker.GradleProject;
import org.openrewrite.gradle.plugins.AddBuildPlugin;
import org.openrewrite.groovy.GroovyIsoVisitor;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaSourceFile;
import org.openrewrite.marker.SearchResult;

import java.util.Optional;
import java.util.concurrent.atomic.AtomicBoolean;

@Value
@EqualsAndHashCode(callSuper = false)
Expand All @@ -51,7 +53,7 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
new IsBuildGradle<>(),
new UsesSpringDependencyManagement()
),
new AddBuildPlugin("io.spring.dependency-management", "1.0.6.RELEASE", null, null).getVisitor()
new AddBuildPlugin("io.spring.dependency-management", "1.0.6.RELEASE", null, null, false).getVisitor()
);
}

Expand All @@ -66,11 +68,38 @@ private static class UsesSpringDependencyManagement extends JavaIsoVisitor<Execu
}
GradleProject gp = maybeGp.get();
if (gp.getPlugins().stream().anyMatch(plugin -> "io.spring.dependency-management".equals(plugin.getId()) ||
"io.spring.gradle.dependencymanagement.DependencyManagementPlugin".equals(plugin.getFullyQualifiedClassName()))) {
"io.spring.gradle.dependencymanagement.DependencyManagementPlugin".equals(plugin.getFullyQualifiedClassName()))
&& usesDependencyManagementDsl(cu)
) {
return SearchResult.found(cu);
}
}
return super.visit(tree, ctx);
}
}

private static boolean usesDependencyManagementDsl(JavaSourceFile cu) {
AtomicBoolean found = new AtomicBoolean(false);
new UsesDependencyManagementDslVisitor().visit(cu, found);
return found.get();
}

private static class UsesDependencyManagementDslVisitor extends GroovyIsoVisitor<AtomicBoolean> {
@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, AtomicBoolean found) {
if ("dependencyManagement".equals(method.getSimpleName())) {
found.set(true);
return method;
}
return super.visitMethodInvocation(method, found);
}

@Override
public @Nullable J visit(@Nullable Tree tree, AtomicBoolean found) {
if (found.get()) {
return (J) tree;
}
return super.visit(tree, found);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,10 @@ void addIfPresent() {
id "java"
id "org.springframework.boot" version "1.5.22.RELEASE"
}
repositories {
mavenCentral()
}
dependencyManagement { }
dependencies {
compile "org.springframework.boot:spring-boot-starter-web"
}
Expand All @@ -56,11 +55,10 @@ void addIfPresent() {
id "org.springframework.boot" version "1.5.22.RELEASE"
id "io.spring.dependency-management" version "1.0.6.RELEASE"
}
repositories {
mavenCentral()
}
dependencyManagement { }
dependencies {
compile "org.springframework.boot:spring-boot-starter-web"
}
Expand All @@ -81,11 +79,10 @@ void dontAddIfNotUsing() {
id "java"
id "org.springframework.boot" version "2.6.15"
}
repositories {
mavenCentral()
}
dependencies {
implementation platform("org.springframework.boot:spring-boot-starter-dependencies:2.6.15")
implementation "org.springframework.boot:spring-boot-starter-web"
Expand Down

0 comments on commit f05e1f8

Please sign in to comment.