Skip to content

Commit

Permalink
[MSHARED-1286]: Display if dependency is optional in tree
Browse files Browse the repository at this point in the history
This should reintroduce that dependency:tree shows if a dependency is optional which was removed in 3.3.0 of dependency plugin.
  • Loading branch information
vbreivik authored and slawekjaranowski committed Jul 23, 2023
1 parent 791dbc8 commit 6c1bff3
Show file tree
Hide file tree
Showing 8 changed files with 170 additions and 2 deletions.
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@
<version>1</version>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.9.3</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
7 changes: 7 additions & 0 deletions src/it/display-optional-information/expected.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
org.apache.maven.its.dependency-tree:test:jar:1.0-SNAPSHOT
+- org.apache.maven:maven-settings:jar:2.0.6:compile
| +- org.codehaus.plexus:plexus-utils:jar:1.4.1:compile
| \- org.codehaus.plexus:plexus-container-default:jar:1.0-alpha-9-stable-1:compile
| +- junit:junit:jar:3.8.1:compile
| \- classworlds:classworlds:jar:1.1-alpha-2:compile
\- org.apache.maven:maven-model:jar:2.0.5:compile (optional)
18 changes: 18 additions & 0 deletions src/it/display-optional-information/invoker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

invoker.goals = org.apache.maven.its.dependency-tree:graph-maven-plugin:graph
68 changes: 68 additions & 0 deletions src/it/display-optional-information/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Licensed to the Apache Software Foundation (ASF) under one
~ or more contributor license agreements. See the NOTICE file
~ distributed with this work for additional information
~ regarding copyright ownership. The ASF licenses this file
~ to you under the Apache License, Version 2.0 (the
~ "License"); you may not use this file except in compliance
~ with the License. You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing,
~ software distributed under the License is distributed on an
~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
~ KIND, either express or implied. See the License for the
~ specific language governing permissions and limitations
~ under the License.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.apache.maven.its.dependency-tree</groupId>
<artifactId>test</artifactId>
<version>1.0-SNAPSHOT</version>

<name>Test</name>
<description>
Test dependency-tree with actual Maven version through dependency:tree (easier than shade, or MPIR:dependencies).
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.
</description>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-settings</artifactId>
<version>2.0.6</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-model</artifactId>
<version>2.0.5</version>
<optional>true</optional>
</dependency>
</dependencies>

<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.its.dependency-tree</groupId>
<artifactId>graph-maven-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
<configuration>
<outputFile>target/tree.txt</outputFile>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
23 changes: 23 additions & 0 deletions src/it/display-optional-information/verify.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/


actual = new File( basedir, "target/tree.txt" ).readLines()
expected = new File( basedir, "expected.txt" ).readLines()
assert actual.equals( expected )
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ private DependencyNode buildDependencyNode(
String premanagedScope = DependencyManagerUtils.getPremanagedScope(node);

List<org.apache.maven.model.Exclusion> exclusions = null;
Boolean optional = null;
Boolean optional = artifact.isOptional();
if (node.getDependency() != null) {
exclusions = new ArrayList<>(node.getDependency().getExclusions().size());
for (Exclusion exclusion : node.getDependency().getExclusions()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,6 @@ public List<Exclusion> getExclusions() {
*/
@Override
public String toNodeString() {
return String.valueOf(artifact);
return artifact + (Boolean.TRUE.equals(optional) ? " (optional)" : "");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.maven.shared.dependency.graph.internal;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.DefaultArtifact;
import org.junit.jupiter.api.Test;

import static java.util.Collections.emptyList;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class DefaultDependencyNodeTest {

private final Artifact artifact = new DefaultArtifact("group", "artifact", "1.2", "compile", "jar", "", null);

@Test
public void nodeString_should_display_if_dependency_is_optonal() {
DefaultDependencyNode optionalNode =
new DefaultDependencyNode(null, artifact, "1.0", "compile", "1.0", true, emptyList());
assertEquals("group:artifact:jar:1.2:compile (optional)", optionalNode.toNodeString());
}

@Test
public void nodeString_for_mandatory_depenendency_does_not_contain_optional_information() {
DefaultDependencyNode optionalNode =
new DefaultDependencyNode(null, artifact, "1.0", "compile", "1.0", false, emptyList());
assertEquals("group:artifact:jar:1.2:compile", optionalNode.toNodeString());
}
}

0 comments on commit 6c1bff3

Please sign in to comment.