Skip to content
Draft
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 @@ -229,10 +229,16 @@ public Xml visitDocument(Xml.Document document, ExecutionContext ctx) {
for (ResolvedDependency d : dependencies.get(Scope.Compile)) {
if (hasAcceptableTransitivity(d, acc) &&
groupId.equals(d.getGroupId()) &&
artifactId.equals(d.getArtifactId()) &&
(d.isTransitive() ||
(d.isDirect() && version.equals(d.getVersion())))
) {
artifactId.equals(d.getArtifactId())) {
if (d.isTransitive() || (d.isDirect() && version.equals(d.getVersion()))) {
return maven;
}
if (d.isDirect() && (scope == null || Scope.fromName(scope) == Scope.Compile)) {
// Direct compile-scope dependency at a different version — update keeping compile scope
return new AddDependencyVisitor(
groupId, artifactId, version, versionPattern, null, releasesOnly,
type, classifier, optional, familyPatternCompiled, metadataFailures).visitNonNull(document, ctx);
}
return maven;
}
}
Expand All @@ -243,7 +249,9 @@ public Xml visitDocument(Xml.Document document, ExecutionContext ctx) {
if ((resolvedScopeEnum == Scope.Provided || resolvedScopeEnum == Scope.Test) && dependencies.get(resolvedScopeEnum) != null) {
for (ResolvedDependency d : dependencies.get(resolvedScopeEnum)) {
if (hasAcceptableTransitivity(d, acc) &&
groupId.equals(d.getGroupId()) && artifactId.equals(d.getArtifactId())) {
groupId.equals(d.getGroupId()) && artifactId.equals(d.getArtifactId()) &&
(d.isTransitive() ||
(d.isDirect() && version.equals(d.getVersion())))) {
return maven;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2261,6 +2261,90 @@ void addDependencyUpgradesScopeOnlyWhenExistingDependencyHasNarrowerScopeAndEqua
);
}

@Test
void existingDependencyUpdatedWithMainSources() {
rewriteRun(
spec -> spec.recipe(addDependency("com.google.guava:guava:30.0-jre", null)),
mavenProject("project",
srcMainJava(
java("public class A {}")
),
pomXml(
"""
<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>
""",
"""
<project>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>30.0-jre</version>
</dependency>
</dependencies>
</project>
"""
)
)
);
}

@Test
void existingDependencyUpdatedWithTestSourcesOnly() {
rewriteRun(
spec -> spec.recipe(addDependency("com.google.guava:guava:30.0-jre", null)),
mavenProject("project",
srcTestJava(
java("public class A {}")
),
pomXml(
"""
<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>
""",
"""
<project>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>30.0-jre</version>
</dependency>
</dependencies>
</project>
"""
)
)
);
}

private AddDependency addDependency(@SuppressWarnings("SameParameterValue") String gav) {
return addDependency(gav, null, null, null);
}
Expand Down