diff --git a/pom.xml b/pom.xml index fc068ac..7de2db6 100644 --- a/pom.xml +++ b/pom.xml @@ -96,6 +96,13 @@ 1 true + + + org.junit.jupiter + junit-jupiter-engine + 5.9.3 + test + diff --git a/src/it/display-optional-information/expected.txt b/src/it/display-optional-information/expected.txt new file mode 100644 index 0000000..ec5a3d5 --- /dev/null +++ b/src/it/display-optional-information/expected.txt @@ -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) \ No newline at end of file diff --git a/src/it/display-optional-information/invoker.properties b/src/it/display-optional-information/invoker.properties new file mode 100644 index 0000000..38ca093 --- /dev/null +++ b/src/it/display-optional-information/invoker.properties @@ -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 diff --git a/src/it/display-optional-information/pom.xml b/src/it/display-optional-information/pom.xml new file mode 100644 index 0000000..96cd40d --- /dev/null +++ b/src/it/display-optional-information/pom.xml @@ -0,0 +1,68 @@ + + + + + 4.0.0 + + org.apache.maven.its.dependency-tree + test + 1.0-SNAPSHOT + + Test + + 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. + + + + UTF-8 + + + + + org.apache.maven + maven-settings + 2.0.6 + + + org.apache.maven + maven-model + 2.0.5 + true + + + + + + + + org.apache.maven.its.dependency-tree + graph-maven-plugin + 1.0-SNAPSHOT + + target/tree.txt + + + + + + diff --git a/src/it/display-optional-information/verify.groovy b/src/it/display-optional-information/verify.groovy new file mode 100644 index 0000000..7d4dc1e --- /dev/null +++ b/src/it/display-optional-information/verify.groovy @@ -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 ) diff --git a/src/main/java/org/apache/maven/shared/dependency/graph/internal/DefaultDependencyGraphBuilder.java b/src/main/java/org/apache/maven/shared/dependency/graph/internal/DefaultDependencyGraphBuilder.java index 955dc3d..19d49f7 100644 --- a/src/main/java/org/apache/maven/shared/dependency/graph/internal/DefaultDependencyGraphBuilder.java +++ b/src/main/java/org/apache/maven/shared/dependency/graph/internal/DefaultDependencyGraphBuilder.java @@ -129,7 +129,7 @@ private DependencyNode buildDependencyNode( String premanagedScope = DependencyManagerUtils.getPremanagedScope(node); List 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()) { diff --git a/src/main/java/org/apache/maven/shared/dependency/graph/internal/DefaultDependencyNode.java b/src/main/java/org/apache/maven/shared/dependency/graph/internal/DefaultDependencyNode.java index 1c2d712..bd33d09 100644 --- a/src/main/java/org/apache/maven/shared/dependency/graph/internal/DefaultDependencyNode.java +++ b/src/main/java/org/apache/maven/shared/dependency/graph/internal/DefaultDependencyNode.java @@ -175,6 +175,6 @@ public List getExclusions() { */ @Override public String toNodeString() { - return String.valueOf(artifact); + return artifact + (Boolean.TRUE.equals(optional) ? " (optional)" : ""); } } diff --git a/src/test/java/org/apache/maven/shared/dependency/graph/internal/DefaultDependencyNodeTest.java b/src/test/java/org/apache/maven/shared/dependency/graph/internal/DefaultDependencyNodeTest.java new file mode 100644 index 0000000..da7afea --- /dev/null +++ b/src/test/java/org/apache/maven/shared/dependency/graph/internal/DefaultDependencyNodeTest.java @@ -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()); + } +}