Skip to content

Commit

Permalink
Add types field to the output tree (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
asafambar authored Dec 28, 2023
1 parent 640f89f commit 7c234b9
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 17 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,19 @@ mvn com.jfrog:maven-dep-tree:tree -DdepsTreeOutputFile=<path/to/output/file>
"children": [],
"configurations": [
"test"
],
"types": [
"jar"
]
},
"org.jfrog.test:multi:3.7-SNAPSHOT": {
"children": [
"junit:junit:3.8.1"
],
"configurations": []
"configurations": [],
"types": [
"pom"
]
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/jfrog/mavendeptree/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,12 @@ 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());
mavenDependencyNode = new MavenDependencyNode(artifact.getScope(), artifact.getType());
nodes.put(getGavString(artifact), mavenDependencyNode);
} else {
// Node exists in the map - add the scope
mavenDependencyNode.addConfiguration(artifact.getScope());
mavenDependencyNode.addType(artifact.getType());
}
List<DependencyNode> children = node.getChildren();
if (children != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@ public class MavenDependencyNode {
private final Set<String> children = new HashSet<>();
// 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<>();

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

public void addChild(String childName) {
Expand All @@ -33,9 +37,13 @@ public void addConfiguration(String configuration) {
configurations.add(configuration);
}

public void addType(String type) {
types.add(type);
}

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

@Override
Expand Down
23 changes: 12 additions & 11 deletions src/test/java/com/jfrog/mavendeptree/UtilsTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,22 @@ public class UtilsTests {

@Test
public void testPopulateDependencyMapNoChildren() {
DependencyNode root = createDependency("root-group", "root-artifact", "root-version", null);
DependencyNode root = createDependency("root-group", "root-artifact", "root-version", null, "");
runPopulateDependencyMap(root, 1);
}

@Test
public void testPopulateDependencyMapDuplicateChildren() {
DefaultDependencyNode root = createDependency("root-group", "root-artifact", "root-version", null);
DefaultDependencyNode root = createDependency("root-group", "root-artifact", "root-version", null, "");

// Root's children
DefaultDependencyNode firstChild = createDependency("child1-group", "child1-artifact", "child1-version", "scope1");
DefaultDependencyNode secondChild = createDependency("child2-group", "child2-artifact", "child2-version", "scope2");
DefaultDependencyNode firstChild = createDependency("child1-group", "child1-artifact", "child1-version", "scope1", "");
DefaultDependencyNode secondChild = createDependency("child2-group", "child2-artifact", "child2-version", "scope2", "type2");
root.setChildren(Lists.newArrayList(firstChild, secondChild));

// First child's children
DefaultDependencyNode thirdChild = createDependency("child3-group", "child3-artifact", "child3-version", "scope3");
DefaultDependencyNode secondsChildWithOtherScope = createDependency("child2-group", "child2-artifact", "child2-version", "other-scope");
DefaultDependencyNode thirdChild = createDependency("child3-group", "child3-artifact", "child3-version", "scope3", "");
DefaultDependencyNode secondsChildWithOtherScope = createDependency("child2-group", "child2-artifact", "child2-version", "other-scope", "other-type");

firstChild.setChildren(Lists.newArrayList(secondsChildWithOtherScope, thirdChild));

Expand All @@ -60,20 +60,21 @@ public void testPopulateDependencyMapDuplicateChildren() {
assertNotNull(secondChildNode);
assertEquals(secondChildNode.getChildren(), new HashSet<>());
assertEquals(secondChildNode.getConfigurations(), Sets.newHashSet("scope2", "other-scope"));
assertEquals(secondChildNode.getTypes(), Sets.newHashSet("type2", "other-type"));
}

@Test
public void testGetNodeId() {
Artifact artifact = createArtifact("group", "artifact", "1.0.0", "scope");
Artifact artifact = createArtifact("group", "artifact", "1.0.0", "scope", "");
assertEquals(getGavString(artifact), "group:artifact:1.0.0");
}

private Artifact createArtifact(String groupId, String artifactId, String version, String scope) {
return new DefaultArtifact(groupId, artifactId, version, scope, "", "", null);
private Artifact createArtifact(String groupId, String artifactId, String version, String scope, String type) {
return new DefaultArtifact(groupId, artifactId, version, scope, type, "", null);
}

private DefaultDependencyNode createDependency(String groupId, String artifactId, String version, String scope) {
Artifact artifact = createArtifact(groupId, artifactId, version, scope);
private DefaultDependencyNode createDependency(String groupId, String artifactId, String version, String scope, String type) {
Artifact artifact = createArtifact(groupId, artifactId, version, scope, type);
return new DefaultDependencyNode(artifact);
}

Expand Down
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"));
assertEquals(mavenDependencyTree.getNodes().get("junit:junit:3.8.1"), new MavenDependencyNode("test", "jar"));
}
}
}
Expand Down Expand Up @@ -102,7 +102,7 @@ private void testMavenArchetype(String projectName) throws VerificationException
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");
MavenDependencyNode expectedSecondNode = new MavenDependencyNode("test", "jar");
assertEquals(nodes.get("junit:junit:3.8.1"), expectedSecondNode);
}
}

0 comments on commit 7c234b9

Please sign in to comment.