From bdc2eac7bb4fc3c74f70fdc01f5d3c7ec521e53d Mon Sep 17 00:00:00 2001 From: Joseph Walton Date: Wed, 15 Dec 2021 22:15:29 +1100 Subject: [PATCH 1/5] [MDEP-779] Show specific classes causing this dependency to be used. --- .../analyze/AbstractAnalyzeMojo.java | 52 ++++++++++++++++--- 1 file changed, 45 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/apache/maven/plugins/dependency/analyze/AbstractAnalyzeMojo.java b/src/main/java/org/apache/maven/plugins/dependency/analyze/AbstractAnalyzeMojo.java index e3634a0d1..624200e2e 100644 --- a/src/main/java/org/apache/maven/plugins/dependency/analyze/AbstractAnalyzeMojo.java +++ b/src/main/java/org/apache/maven/plugins/dependency/analyze/AbstractAnalyzeMojo.java @@ -24,8 +24,10 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; +import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.List; +import java.util.Map; import java.util.Set; import org.apache.commons.lang3.StringUtils; @@ -327,7 +329,8 @@ private boolean checkDependencies() } Set usedDeclared = new LinkedHashSet<>( analysis.getUsedDeclaredArtifacts() ); - Set usedUndeclared = new LinkedHashSet<>( analysis.getUsedUndeclaredArtifacts() ); + Map> usedUndeclaredWithClasses = + new LinkedHashMap<>( analysis.getUsedUndeclaredArtifactsWithClasses() ); Set unusedDeclared = new LinkedHashSet<>( analysis.getUnusedDeclaredArtifacts() ); Set testArtifactsWithNonTestScope = new LinkedHashSet<>( analysis.getTestArtifactsWithNonTestScope() ); @@ -340,8 +343,9 @@ private boolean checkDependencies() filterArtifactsByScope( unusedDeclared, Artifact.SCOPE_RUNTIME ); } - ignoredUsedUndeclared.addAll( filterDependencies( usedUndeclared, ignoredDependencies ) ); - ignoredUsedUndeclared.addAll( filterDependencies( usedUndeclared, ignoredUsedUndeclaredDependencies ) ); + ignoredUsedUndeclared.addAll( filterDependencies( usedUndeclaredWithClasses.keySet(), ignoredDependencies ) ); + ignoredUsedUndeclared.addAll( filterDependencies( usedUndeclaredWithClasses.keySet(), + ignoredUsedUndeclaredDependencies ) ); ignoredUnusedDeclared.addAll( filterDependencies( unusedDeclared, ignoredDependencies ) ); ignoredUnusedDeclared.addAll( filterDependencies( unusedDeclared, ignoredUnusedDeclaredDependencies ) ); @@ -357,11 +361,11 @@ private boolean checkDependencies() reported = true; } - if ( !usedUndeclared.isEmpty() ) + if ( !usedUndeclaredWithClasses.isEmpty() ) { getLog().warn( "Used undeclared dependencies found:" ); - logArtifacts( usedUndeclared, true ); + logArtifacts( usedUndeclaredWithClasses, true ); reported = true; warning = true; } @@ -402,12 +406,12 @@ private boolean checkDependencies() if ( outputXML ) { - writeDependencyXML( usedUndeclared ); + writeDependencyXML( usedUndeclaredWithClasses.keySet() ); } if ( scriptableOutput ) { - writeScriptableOutput( usedUndeclared ); + writeScriptableOutput( usedUndeclaredWithClasses.keySet() ); } if ( !reported ) @@ -449,6 +453,40 @@ private void logArtifacts( Set artifacts, boolean warn ) } } + private void logArtifacts( Map> artifacts, boolean warn ) + { + if ( artifacts.isEmpty() ) + { + getLog().info( " None" ); + } + else + { + for ( Map.Entry> entry : artifacts.entrySet() ) + { + // called because artifact will set the version to -SNAPSHOT only if I do this. MNG-2961 + entry.getKey().isSnapshot(); + + if ( warn ) + { + getLog().warn( " " + entry.getKey() ); + for ( String clazz : entry.getValue() ) + { + getLog().warn( " class " + clazz ); + } + } + else + { + getLog().info( " " + entry.getKey() ); + for ( String clazz : entry.getValue() ) + { + getLog().info( " class " + clazz ); + } + } + + } + } + } + private void writeDependencyXML( Set artifacts ) { if ( !artifacts.isEmpty() ) From b99d6fd22a82e70077fe1277023130fa334171bd Mon Sep 17 00:00:00 2001 From: Joseph Walton Date: Wed, 15 Dec 2021 22:23:05 +1100 Subject: [PATCH 2/5] [MDEP-779] Only print classes when verbose. --- .../plugins/dependency/analyze/AbstractAnalyzeMojo.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/apache/maven/plugins/dependency/analyze/AbstractAnalyzeMojo.java b/src/main/java/org/apache/maven/plugins/dependency/analyze/AbstractAnalyzeMojo.java index 624200e2e..860135565 100644 --- a/src/main/java/org/apache/maven/plugins/dependency/analyze/AbstractAnalyzeMojo.java +++ b/src/main/java/org/apache/maven/plugins/dependency/analyze/AbstractAnalyzeMojo.java @@ -365,7 +365,14 @@ private boolean checkDependencies() { getLog().warn( "Used undeclared dependencies found:" ); - logArtifacts( usedUndeclaredWithClasses, true ); + if ( verbose ) + { + logArtifacts( usedUndeclaredWithClasses, true ); + } + else + { + logArtifacts( usedUndeclaredWithClasses.keySet(), true ); + } reported = true; warning = true; } From 544c83f530c7f9ced49619b24da230a58d4b77b1 Mon Sep 17 00:00:00 2001 From: Joseph Walton Date: Mon, 31 Jan 2022 01:07:04 +1100 Subject: [PATCH 3/5] [MDEP-779] Add the test project from maven-dependency-analyzer This fixture works for the analyzer test; copy it over. --- .../pom.xml | 41 +++++++++++++++++++ .../java/usedUndeclaredReference/Project.java | 25 +++++++++++ 2 files changed, 66 insertions(+) create mode 100644 src/it/projects/mdep-779-analyze-only-verbose-shows-class-names/pom.xml create mode 100644 src/it/projects/mdep-779-analyze-only-verbose-shows-class-names/src/main/java/usedUndeclaredReference/Project.java diff --git a/src/it/projects/mdep-779-analyze-only-verbose-shows-class-names/pom.xml b/src/it/projects/mdep-779-analyze-only-verbose-shows-class-names/pom.xml new file mode 100644 index 000000000..2a8e65209 --- /dev/null +++ b/src/it/projects/mdep-779-analyze-only-verbose-shows-class-names/pom.xml @@ -0,0 +1,41 @@ + + + + + + 4.0.0 + + org.apache.maven.shared.dependency-analyzer.tests + usedUndeclaredReference + 1.0 + jar + + + + dom4j + dom4j + 1.6.1 + + + diff --git a/src/it/projects/mdep-779-analyze-only-verbose-shows-class-names/src/main/java/usedUndeclaredReference/Project.java b/src/it/projects/mdep-779-analyze-only-verbose-shows-class-names/src/main/java/usedUndeclaredReference/Project.java new file mode 100644 index 000000000..5432b4056 --- /dev/null +++ b/src/it/projects/mdep-779-analyze-only-verbose-shows-class-names/src/main/java/usedUndeclaredReference/Project.java @@ -0,0 +1,25 @@ +package usedUndeclaredReference; + +/* + * 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. + */ + +public class Project +{ + public static final Class CLASS_REF = org.apache.xmlcommons.Version.class; +} From 407db2b3d22270ee6798c69b13a77906b302bde4 Mon Sep 17 00:00:00 2001 From: Joseph Walton Date: Mon, 31 Jan 2022 01:48:15 +1100 Subject: [PATCH 4/5] [MDEP-779] Add test expectations Invoke dependency:analyze-only in verbose mode, and ensure that the result includes the expected warnings. --- .../invoker.properties | 18 ++++++++++ .../verify.bsh | 33 +++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 src/it/projects/mdep-779-analyze-only-verbose-shows-class-names/invoker.properties create mode 100644 src/it/projects/mdep-779-analyze-only-verbose-shows-class-names/verify.bsh diff --git a/src/it/projects/mdep-779-analyze-only-verbose-shows-class-names/invoker.properties b/src/it/projects/mdep-779-analyze-only-verbose-shows-class-names/invoker.properties new file mode 100644 index 000000000..342590243 --- /dev/null +++ b/src/it/projects/mdep-779-analyze-only-verbose-shows-class-names/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 = clean package ${project.groupId}:${project.artifactId}:${project.version}:analyze-only -Dverbose diff --git a/src/it/projects/mdep-779-analyze-only-verbose-shows-class-names/verify.bsh b/src/it/projects/mdep-779-analyze-only-verbose-shows-class-names/verify.bsh new file mode 100644 index 000000000..467467ddb --- /dev/null +++ b/src/it/projects/mdep-779-analyze-only-verbose-shows-class-names/verify.bsh @@ -0,0 +1,33 @@ +/* + * 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. + */ + +import org.codehaus.plexus.util.*; + +String log = FileUtils.fileRead( new File( basedir, "build.log" ) ); + +String expected = "[WARNING] Used undeclared dependencies found:\n" + + "[WARNING] xml-apis:xml-apis:jar:1.0.b2:compile\n" + + "[WARNING] class org.apache.xmlcommons.Version\n"; + +if ( !log.contains(expected) ) +{ + throw new Exception( "Expected warning missing" ); +} + +return true; From 4122b8eb54fd23d8170c1572cef17d6f43947d2b Mon Sep 17 00:00:00 2001 From: Joseph Walton Date: Mon, 31 Jan 2022 11:01:39 +1100 Subject: [PATCH 5/5] [MDEP-779] Unify line separators for testing under Windows --- .../mdep-779-analyze-only-verbose-shows-class-names/verify.bsh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/it/projects/mdep-779-analyze-only-verbose-shows-class-names/verify.bsh b/src/it/projects/mdep-779-analyze-only-verbose-shows-class-names/verify.bsh index 467467ddb..35957daaf 100644 --- a/src/it/projects/mdep-779-analyze-only-verbose-shows-class-names/verify.bsh +++ b/src/it/projects/mdep-779-analyze-only-verbose-shows-class-names/verify.bsh @@ -21,6 +21,8 @@ import org.codehaus.plexus.util.*; String log = FileUtils.fileRead( new File( basedir, "build.log" ) ); +log = StringUtils.unifyLineSeparators(log, "\n"); + String expected = "[WARNING] Used undeclared dependencies found:\n" + "[WARNING] xml-apis:xml-apis:jar:1.0.b2:compile\n" + "[WARNING] class org.apache.xmlcommons.Version\n";