Skip to content

Commit

Permalink
Support classifier attribute for dependency. (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
asafambar authored May 20, 2024
1 parent 1ad3cce commit 745f8de
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 8 deletions.
5 changes: 4 additions & 1 deletion src/main/java/com/jfrog/mavendeptree/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ static void populateDependencyMap(DependencyNode node, Map<String, MavenDependen
MavenDependencyNode mavenDependencyNode = nodes.get(getGavString(artifact));
if (mavenDependencyNode == null) {
// Node does not exist in the Map - add it
mavenDependencyNode = new MavenDependencyNode(artifact.getScope(), artifact.getType());
mavenDependencyNode = new MavenDependencyNode(artifact.getScope(), artifact.getType(), artifact.getClassifier());
nodes.put(getGavString(artifact), mavenDependencyNode);
} else {
// Node exists in the map - add the scope
Expand Down Expand Up @@ -117,6 +117,9 @@ public static String getGavString(Artifact artifact) {
if (artifact == null) {
return "";
}
if (artifact.hasClassifier()) {
return String.join(":", artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion()+"-"+artifact.getClassifier());
}
return String.join(":", artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,18 @@ public class MavenDependencyNode {
// The Maven scopes such as compile, runtime, test, etc. We use the same naming convention as in the gradle-dep-tree.
private final Set<String> configurations = new HashSet<>();
private final Set<String> types = new HashSet<>();
private String classifier;

public MavenDependencyNode(String configuration, String type) {
public MavenDependencyNode(String configuration, String type, String classifier) {
if (configuration != null) {
this.configurations.add(configuration);
}
if (type != null) {
this.types.add(type);
}
if (classifier != null) {
this.setClassifier(classifier);
}
}

public void addChild(String childName) {
Expand All @@ -41,9 +45,13 @@ public void addType(String type) {
types.add(type);
}

public void setClassifier(String classifier) {
this.classifier = classifier;
}

@Override
public int hashCode() {
return Objects.hash(children, configurations, types);
return Objects.hash(children, configurations, types, classifier);
}

@Override
Expand All @@ -52,6 +60,8 @@ public boolean equals(Object obj) {
return false;
}
MavenDependencyNode other = (MavenDependencyNode) obj;
return children.equals(other.children) && configurations.equals(other.configurations);
return children.equals(other.children) &&
configurations.equals(other.configurations) &&
Objects.equals(classifier,other.classifier);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public void testMultiModule() throws VerificationException, IOException, ParserC
assertEquals(root, "org.jfrog.test:multi3:3.7-SNAPSHOT");
} else {
assertEquals(root, "org.jfrog.test:multi:3.7-SNAPSHOT");
assertEquals(mavenDependencyTree.getNodes().get("junit:junit:3.8.1"), new MavenDependencyNode("test", "jar"));
assertEquals(mavenDependencyTree.getNodes().get("junit:junit:3.8.1"), new MavenDependencyNode("test", "jar", null));
}
}
}
Expand Down Expand Up @@ -98,11 +98,16 @@ private void testMavenArchetype(String projectName) throws VerificationException

// Check nodes
Map<String, MavenDependencyNode> nodes = mavenDependencyTree.getNodes();
assertEquals(nodes.size(), 2);
assertEquals(nodes.size(), 3);
MavenDependencyNode expectedFirstNode = new MavenDependencyNode();
expectedFirstNode.addChild("junit:junit:3.8.1");
assertEquals(nodes.get("org.example:maven-archetype-simple:1.0-SNAPSHOT"), expectedFirstNode);
MavenDependencyNode expectedSecondNode = new MavenDependencyNode("test", "jar");
expectedFirstNode.addChild("org.testng:testng:5.9-jdk15");
MavenDependencyNode node1 = nodes.get("org.example:maven-archetype-simple:1.0-SNAPSHOT");
assertEquals(node1, expectedFirstNode);
MavenDependencyNode expectedSecondNode = new MavenDependencyNode("test", "jar", null);
assertEquals(nodes.get("junit:junit:3.8.1"), expectedSecondNode);
// with classifier
MavenDependencyNode expectedThirdNode = new MavenDependencyNode("test", "jar", "jdk15");
assertEquals(nodes.get("org.testng:testng:5.9-jdk15"), expectedThirdNode);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<classifier>jdk15</classifier>
<version>5.9</version>
<scope>test</scope>
</dependency>
</dependencies>

<dependencyManagement>
Expand Down
7 changes: 7 additions & 0 deletions src/test/resources/integration/maven-archetype-simple/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<classifier>jdk15</classifier>
<version>5.9</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down

0 comments on commit 745f8de

Please sign in to comment.