Skip to content

Commit

Permalink
Preserve multiple dependencyManagement with different types (#4290)
Browse files Browse the repository at this point in the history
It is a valid case to specify different dependencyManagement dependencies for the same artifact but with different types (used for example if you create a test-jar of a dependency and manage its version).

Find managedDependency() did not take type into account
  • Loading branch information
askoog authored Jun 27, 2024
1 parent b3bba3d commit 652e454
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,9 @@ public ResolvedManagedDependency findManagedDependency(Xml.Tag tag) {
.orElse(getResolutionResult().getPom().getGroupId()));
String artifactId = getResolutionResult().getPom().getValue(tag.getChildValue("artifactId").orElse(""));
String classifier = getResolutionResult().getPom().getValue(tag.getChildValue("classifier").orElse(null));
String type = getResolutionResult().getPom().getValue(tag.getChildValue("type").orElse(null));
if (groupId != null && artifactId != null) {
return findManagedDependency(groupId, artifactId, classifier);
return findManagedDependency(groupId, artifactId, classifier, type);
}
return null;
}
Expand All @@ -283,10 +284,16 @@ public ResolvedManagedDependency findManagedDependency(String groupId, String ar

@Nullable
private ResolvedManagedDependency findManagedDependency(String groupId, String artifactId, @Nullable String classifier) {
return findManagedDependency(groupId, artifactId, classifier, null);
}

@Nullable
private ResolvedManagedDependency findManagedDependency(String groupId, String artifactId, @Nullable String classifier, @Nullable String type) {
for (ResolvedManagedDependency d : getResolutionResult().getPom().getDependencyManagement()) {
if (groupId.equals(d.getGroupId()) &&
artifactId.equals(d.getArtifactId()) &&
(classifier == null || classifier.equals(d.getClassifier()))) {
(classifier == null || classifier.equals(d.getClassifier())) &&
(type == null || type.equals(d.getType()))) {
return d;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,39 @@ void keepDependencyWithType() {
);
}

@Test
void keepDependencyManagementWithType() {
rewriteRun(
pomXml(
"""
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.acme</groupId>
<artifactId>example-dependency</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.acme</groupId>
<artifactId>example-dependency</artifactId>
<version>1.0.0</version>
<type>test-jar</type>
</dependency>
</dependencies>
</dependencyManagement>
</project>
"""
)
);
}

@Test
void keepDependencyWithDifferentScope() {
rewriteRun(
Expand Down

0 comments on commit 652e454

Please sign in to comment.