diff --git a/rewrite-gradle/src/main/java/org/openrewrite/gradle/AddDependency.java b/rewrite-gradle/src/main/java/org/openrewrite/gradle/AddDependency.java index 990a0606d65..59084bd7cec 100644 --- a/rewrite-gradle/src/main/java/org/openrewrite/gradle/AddDependency.java +++ b/rewrite-gradle/src/main/java/org/openrewrite/gradle/AddDependency.java @@ -34,6 +34,7 @@ import java.util.*; +import static java.util.Collections.*; import static java.util.Objects.requireNonNull; @Value @@ -171,14 +172,14 @@ private boolean usesType(SourceFile sourceFile, ExecutionContext ctx) { return tree; } SourceFile sourceFile = (SourceFile) tree; - sourceFile.getMarkers().findFirst(JavaProject.class).ifPresent(javaProject -> - sourceFile.getMarkers().findFirst(JavaSourceSet.class).ifPresent(sourceSet -> { - if (usesType(sourceFile, ctx)) { - acc.usingType = true; - } - Set configurations = acc.configurationsByProject.computeIfAbsent(javaProject, ignored -> new HashSet<>()); - configurations.add("main".equals(sourceSet.getName()) ? "implementation" : sourceSet.getName() + "Implementation"); - })); + sourceFile.getMarkers().findFirst(JavaProject.class).ifPresent(javaProject -> { + if (usesType(sourceFile, ctx)) { + acc.usingType = true; + } + Set configurations = acc.configurationsByProject.computeIfAbsent(javaProject, ignored -> new HashSet<>()); + sourceFile.getMarkers().findFirst(JavaSourceSet.class).ifPresent(sourceSet -> + configurations.add("main".equals(sourceSet.getName()) ? "implementation" : sourceSet.getName() + "Implementation")); + }); return tree; } }; @@ -216,7 +217,12 @@ public TreeVisitor getVisitor(Scanned acc) { GradleProject gp = maybeGp.get(); - Set resolvedConfigurations = StringUtils.isBlank(configuration) ? acc.configurationsByProject.get(jp) : new HashSet<>(Collections.singletonList(configuration)); + Set resolvedConfigurations = StringUtils.isBlank(configuration) ? + acc.configurationsByProject.getOrDefault(jp, new HashSet<>()) : + new HashSet<>(singletonList(configuration)); + if (resolvedConfigurations.isEmpty()) { + resolvedConfigurations.add("implementation"); + } Set tmpConfigurations = new HashSet<>(resolvedConfigurations); for (String tmpConfiguration : tmpConfigurations) { GradleDependencyConfiguration gdc = gp.getConfiguration(tmpConfiguration); diff --git a/rewrite-gradle/src/test/java/org/openrewrite/gradle/AddDependencyTest.java b/rewrite-gradle/src/test/java/org/openrewrite/gradle/AddDependencyTest.java index 6330f931018..cb1da2ffe4a 100644 --- a/rewrite-gradle/src/test/java/org/openrewrite/gradle/AddDependencyTest.java +++ b/rewrite-gradle/src/test/java/org/openrewrite/gradle/AddDependencyTest.java @@ -1317,9 +1317,6 @@ void addUnconditionally() { rewriteRun( spec -> spec.recipe(addDependency("org.apache.logging.log4j:log4j-core:2.22.1")), mavenProject("project", - srcMainJava( - java(usingGuavaIntMath) - ), buildGradle(""" plugins { id "java-library"