Skip to content

Commit 6c1bff3

Browse files
vbreivikslawekjaranowski
authored andcommitted
[MSHARED-1286]: Display if dependency is optional in tree
This should reintroduce that dependency:tree shows if a dependency is optional which was removed in 3.3.0 of dependency plugin.
1 parent 791dbc8 commit 6c1bff3

File tree

8 files changed

+170
-2
lines changed

8 files changed

+170
-2
lines changed

pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,13 @@
9696
<version>1</version>
9797
<optional>true</optional>
9898
</dependency>
99+
100+
<dependency>
101+
<groupId>org.junit.jupiter</groupId>
102+
<artifactId>junit-jupiter-api</artifactId>
103+
<version>5.9.3</version>
104+
<scope>test</scope>
105+
</dependency>
99106
</dependencies>
100107

101108
<build>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
org.apache.maven.its.dependency-tree:test:jar:1.0-SNAPSHOT
2+
+- org.apache.maven:maven-settings:jar:2.0.6:compile
3+
| +- org.codehaus.plexus:plexus-utils:jar:1.4.1:compile
4+
| \- org.codehaus.plexus:plexus-container-default:jar:1.0-alpha-9-stable-1:compile
5+
| +- junit:junit:jar:3.8.1:compile
6+
| \- classworlds:classworlds:jar:1.1-alpha-2:compile
7+
\- org.apache.maven:maven-model:jar:2.0.5:compile (optional)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
invoker.goals = org.apache.maven.its.dependency-tree:graph-maven-plugin:graph
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Licensed to the Apache Software Foundation (ASF) under one
4+
~ or more contributor license agreements. See the NOTICE file
5+
~ distributed with this work for additional information
6+
~ regarding copyright ownership. The ASF licenses this file
7+
~ to you under the Apache License, Version 2.0 (the
8+
~ "License"); you may not use this file except in compliance
9+
~ with the License. You may obtain a copy of the License at
10+
~
11+
~ http://www.apache.org/licenses/LICENSE-2.0
12+
~
13+
~ Unless required by applicable law or agreed to in writing,
14+
~ software distributed under the License is distributed on an
15+
~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
~ KIND, either express or implied. See the License for the
17+
~ specific language governing permissions and limitations
18+
~ under the License.
19+
-->
20+
21+
<project xmlns="http://maven.apache.org/POM/4.0.0"
22+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
23+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
24+
<modelVersion>4.0.0</modelVersion>
25+
26+
<groupId>org.apache.maven.its.dependency-tree</groupId>
27+
<artifactId>test</artifactId>
28+
<version>1.0-SNAPSHOT</version>
29+
30+
<name>Test</name>
31+
<description>
32+
Test dependency-tree with actual Maven version through dependency:tree (easier than shade, or MPIR:dependencies).
33+
Needs to be run in sequence with Maven 2.2.1, 3.0.x and 3.1-alpha-1+ to check the 3 graph API implementations.
34+
</description>
35+
36+
<properties>
37+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
38+
</properties>
39+
40+
<dependencies>
41+
<dependency>
42+
<groupId>org.apache.maven</groupId>
43+
<artifactId>maven-settings</artifactId>
44+
<version>2.0.6</version>
45+
</dependency>
46+
<dependency>
47+
<groupId>org.apache.maven</groupId>
48+
<artifactId>maven-model</artifactId>
49+
<version>2.0.5</version>
50+
<optional>true</optional>
51+
</dependency>
52+
</dependencies>
53+
54+
<build>
55+
<pluginManagement>
56+
<plugins>
57+
<plugin>
58+
<groupId>org.apache.maven.its.dependency-tree</groupId>
59+
<artifactId>graph-maven-plugin</artifactId>
60+
<version>1.0-SNAPSHOT</version>
61+
<configuration>
62+
<outputFile>target/tree.txt</outputFile>
63+
</configuration>
64+
</plugin>
65+
</plugins>
66+
</pluginManagement>
67+
</build>
68+
</project>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
21+
actual = new File( basedir, "target/tree.txt" ).readLines()
22+
expected = new File( basedir, "expected.txt" ).readLines()
23+
assert actual.equals( expected )

src/main/java/org/apache/maven/shared/dependency/graph/internal/DefaultDependencyGraphBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ private DependencyNode buildDependencyNode(
129129
String premanagedScope = DependencyManagerUtils.getPremanagedScope(node);
130130

131131
List<org.apache.maven.model.Exclusion> exclusions = null;
132-
Boolean optional = null;
132+
Boolean optional = artifact.isOptional();
133133
if (node.getDependency() != null) {
134134
exclusions = new ArrayList<>(node.getDependency().getExclusions().size());
135135
for (Exclusion exclusion : node.getDependency().getExclusions()) {

src/main/java/org/apache/maven/shared/dependency/graph/internal/DefaultDependencyNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,6 @@ public List<Exclusion> getExclusions() {
175175
*/
176176
@Override
177177
public String toNodeString() {
178-
return String.valueOf(artifact);
178+
return artifact + (Boolean.TRUE.equals(optional) ? " (optional)" : "");
179179
}
180180
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.maven.shared.dependency.graph.internal;
20+
21+
import org.apache.maven.artifact.Artifact;
22+
import org.apache.maven.artifact.DefaultArtifact;
23+
import org.junit.jupiter.api.Test;
24+
25+
import static java.util.Collections.emptyList;
26+
import static org.junit.jupiter.api.Assertions.assertEquals;
27+
28+
public class DefaultDependencyNodeTest {
29+
30+
private final Artifact artifact = new DefaultArtifact("group", "artifact", "1.2", "compile", "jar", "", null);
31+
32+
@Test
33+
public void nodeString_should_display_if_dependency_is_optonal() {
34+
DefaultDependencyNode optionalNode =
35+
new DefaultDependencyNode(null, artifact, "1.0", "compile", "1.0", true, emptyList());
36+
assertEquals("group:artifact:jar:1.2:compile (optional)", optionalNode.toNodeString());
37+
}
38+
39+
@Test
40+
public void nodeString_for_mandatory_depenendency_does_not_contain_optional_information() {
41+
DefaultDependencyNode optionalNode =
42+
new DefaultDependencyNode(null, artifact, "1.0", "compile", "1.0", false, emptyList());
43+
assertEquals("group:artifact:jar:1.2:compile", optionalNode.toNodeString());
44+
}
45+
}

0 commit comments

Comments
 (0)