From c132787a3a8603e00a9ec6dd44ce9a593f1e4520 Mon Sep 17 00:00:00 2001 From: Adriano Machado <60320+ammachado@users.noreply.github.com> Date: Wed, 14 Feb 2024 19:14:13 -0500 Subject: [PATCH] Add dependency now works with empty maven projects --- .../org/openrewrite/maven/AddDependency.java | 12 ++--- .../openrewrite/maven/AddDependencyTest.java | 54 +++++++++++++++++++ 2 files changed, 60 insertions(+), 6 deletions(-) diff --git a/rewrite-maven/src/main/java/org/openrewrite/maven/AddDependency.java b/rewrite-maven/src/main/java/org/openrewrite/maven/AddDependency.java index 7d1ce19542d..059e61a1aa7 100644 --- a/rewrite-maven/src/main/java/org/openrewrite/maven/AddDependency.java +++ b/rewrite-maven/src/main/java/org/openrewrite/maven/AddDependency.java @@ -197,7 +197,7 @@ public Tree visit(@Nullable Tree tree, ExecutionContext ctx) { @Override public TreeVisitor getVisitor(Scanned acc) { - return Preconditions.check(acc.usingType && !acc.scopeByProject.isEmpty(), new MavenVisitor() { + return Preconditions.check(onlyIfUsing == null || acc.usingType && !acc.scopeByProject.isEmpty(), new MavenVisitor() { @Nullable final Pattern familyPatternCompiled = familyPattern == null ? null : Pattern.compile(familyPattern.replace("*", ".*")); @@ -207,13 +207,13 @@ public Xml visitDocument(Xml.Document document, ExecutionContext ctx) { JavaProject javaProject = document.getMarkers().findFirst(JavaProject.class).orElse(null); String maybeScope = javaProject == null ? null : acc.scopeByProject.get(javaProject); - if (maybeScope == null) { + if (maybeScope == null && !acc.scopeByProject.isEmpty()) { return maven; } // If the dependency is already in compile scope it will be available everywhere, no need to continue for (ResolvedDependency d : getResolutionResult().getDependencies().get(Scope.Compile)) { - if (hasAcceptableTransitivity(d) + if (hasAcceptableTransitivity(d, acc) && groupId.equals(d.getGroupId()) && artifactId.equals(d.getArtifactId())) { return maven; } @@ -223,7 +223,7 @@ public Xml visitDocument(Xml.Document document, ExecutionContext ctx) { Scope resolvedScopeEnum = Scope.fromName(resolvedScope); if (resolvedScopeEnum == Scope.Provided || resolvedScopeEnum == Scope.Test) { for (ResolvedDependency d : getResolutionResult().getDependencies().get(resolvedScopeEnum)) { - if (hasAcceptableTransitivity(d) + if (hasAcceptableTransitivity(d, acc) && groupId.equals(d.getGroupId()) && artifactId.equals(d.getArtifactId())) { return maven; } @@ -237,7 +237,7 @@ public Xml visitDocument(Xml.Document document, ExecutionContext ctx) { }); } - private boolean hasAcceptableTransitivity(ResolvedDependency d) { - return d.isDirect() || Boolean.TRUE.equals(acceptTransitive); + private boolean hasAcceptableTransitivity(ResolvedDependency d, Scanned acc) { + return d.isDirect() || Boolean.TRUE.equals(acceptTransitive) && !acc.scopeByProject.isEmpty(); } } diff --git a/rewrite-maven/src/test/java/org/openrewrite/maven/AddDependencyTest.java b/rewrite-maven/src/test/java/org/openrewrite/maven/AddDependencyTest.java index 85fc4450acb..191b384eb19 100644 --- a/rewrite-maven/src/test/java/org/openrewrite/maven/AddDependencyTest.java +++ b/rewrite-maven/src/test/java/org/openrewrite/maven/AddDependencyTest.java @@ -1050,6 +1050,60 @@ void noCompileScopeDependency() { ); } + @Test + void addDependenciesOnEmptyProjectWithMavenProject() { + rewriteRun( + spec -> spec.recipe(new AddDependency("com.google.guava", "guava", "29.0-jre", null, null, true, null, null, null, null, null, null)), + mavenProject("my-app", pomXml(""" + + com.mycompany.app + my-app + 1 + """, + """ + + com.mycompany.app + my-app + 1 + + + com.google.guava + guava + 29.0-jre + + + """ + ) + )); + } + + @Test + void addDependenciesOnEmptyProject() { + rewriteRun( + spec -> spec.recipe(new AddDependency("com.google.guava", "guava", "29.0-jre", null, null, true, null, null, null, null, null, null)), + pomXml(""" + + com.mycompany.app + my-app + 1 + """, + """ + + com.mycompany.app + my-app + 1 + + + com.google.guava + guava + 29.0-jre + + + """ + ) + ); + } + private AddDependency addDependency(String gav, String onlyIfUsing) { return addDependency(gav, onlyIfUsing, null, null); }