Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dependency now works with empty maven projects #4006

Merged
merged 1 commit into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public Tree visit(@Nullable Tree tree, ExecutionContext ctx) {

@Override
public TreeVisitor<?, ExecutionContext> getVisitor(Scanned acc) {
return Preconditions.check(acc.usingType && !acc.scopeByProject.isEmpty(), new MavenVisitor<ExecutionContext>() {
return Preconditions.check(onlyIfUsing == null || acc.usingType && !acc.scopeByProject.isEmpty(), new MavenVisitor<ExecutionContext>() {
@Nullable
final Pattern familyPatternCompiled = familyPattern == null ? null : Pattern.compile(familyPattern.replace("*", ".*"));

Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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("""
<project>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
</project>""",
"""
<project>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>29.0-jre</version>
</dependency>
</dependencies>
</project>"""
)
));
}

@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("""
<project>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
</project>""",
"""
<project>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>29.0-jre</version>
</dependency>
</dependencies>
</project>"""
)
);
}

private AddDependency addDependency(String gav, String onlyIfUsing) {
return addDependency(gav, onlyIfUsing, null, null);
}
Expand Down