This is an automated email from the ASF dual-hosted git repository. elharo pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/maven-dependency-plugin.git
The following commit(s) were added to refs/heads/master by this push: new 130aecd [MDEP-708] dependency:analyze should recommend test scope where possible (#80) 130aecd is described below commit 130aecddae40cd2d9d8ab9ba6dc48779b57c968b Author: ian-lavallee <34783068+ian-laval...@users.noreply.github.com> AuthorDate: Wed Aug 5 16:36:27 2020 -0400 [MDEP-708] dependency:analyze should recommend test scope where possible (#80) * added nonTestScopeTestArtifact logic --- pom.xml | 3 +- .../invoker.properties | 21 ++++++ .../analyze-testDependencyWithNonTestScope/pom.xml | 76 ++++++++++++++++++++++ .../src/main/java/hello/Hello.java | 24 +++++++ .../src/test/java/hello/HelloTest.java | 35 ++++++++++ .../verify.bsh | 63 ++++++++++++++++++ .../dependency/analyze/AbstractAnalyzeMojo.java | 13 +++- .../dependency/analyze/AnalyzeReportView.java | 18 +++++ 8 files changed, 251 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index 4169862..230809a 100644 --- a/pom.xml +++ b/pom.xml @@ -210,7 +210,7 @@ under the License. <dependency> <groupId>org.apache.maven.shared</groupId> <artifactId>maven-dependency-analyzer</artifactId> - <version>1.11.2</version> + <version>1.11.3-SNAPSHOT</version> <exclusions> <exclusion> <artifactId>maven-project</artifactId> @@ -435,6 +435,7 @@ under the License. <filterProperties> <repository.proxy.url>${repository.proxy.url}</repository.proxy.url> </filterProperties> + <addTestClassPath>true</addTestClassPath> </configuration> </plugin> <plugin> diff --git a/src/it/projects/analyze-testDependencyWithNonTestScope/invoker.properties b/src/it/projects/analyze-testDependencyWithNonTestScope/invoker.properties new file mode 100644 index 0000000..6e4c8f3 --- /dev/null +++ b/src/it/projects/analyze-testDependencyWithNonTestScope/invoker.properties @@ -0,0 +1,21 @@ +# 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 ${project.groupId}:${project.artifactId}:${project.version}:analyze-report +# don't know why it fails with Maven 2 on some weird java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory +# no time to investigate, Maven 2 is not supported any more +invoker.maven.version = 3.0+ diff --git a/src/it/projects/analyze-testDependencyWithNonTestScope/pom.xml b/src/it/projects/analyze-testDependencyWithNonTestScope/pom.xml new file mode 100644 index 0000000..710dcb4 --- /dev/null +++ b/src/it/projects/analyze-testDependencyWithNonTestScope/pom.xml @@ -0,0 +1,76 @@ +<?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.plugins.dependency.its</groupId> + <artifactId>nonTestScopeTestArtifactsAnalysis</artifactId> + <version>1.0.0-SNAPSHOT</version> + + <properties> + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + <maven.compiler.release>1.7</maven.compiler.release> + <maven.compiler.sources>1.7</maven.compiler.sources> + </properties> + + <build> + <pluginManagement> + <plugins> + <plugin> + <artifactId>maven-dependency-plugin</artifactId> + <configuration> + <outputXML>true</outputXML> + <scriptableOutput>true</scriptableOutput> + <verbose>true</verbose> + </configuration> + </plugin> + </plugins> + </pluginManagement> + </build> + + <dependencies> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <version>4.12</version> + </dependency> + <dependency> + <groupId>org.assertj</groupId> + <artifactId>assertj-core</artifactId> + <!-- use 2.9.1 for Java 7 projects --> + <version>2.9.1</version> + <scope>runtime</scope> + </dependency> + <!-- Integration test for scope analysis --> + <dependency> + <groupId>org.jsoup</groupId> + <artifactId>jsoup</artifactId> + <version>1.13.1</version> + <!--<scope>test</scope>--> + </dependency> + <dependency> + <groupId>org.apache.maven</groupId> + <artifactId>maven-project</artifactId> + <version>2.0.6</version> + </dependency> + </dependencies> +</project> diff --git a/src/it/projects/analyze-testDependencyWithNonTestScope/src/main/java/hello/Hello.java b/src/it/projects/analyze-testDependencyWithNonTestScope/src/main/java/hello/Hello.java new file mode 100644 index 0000000..7b5e10d --- /dev/null +++ b/src/it/projects/analyze-testDependencyWithNonTestScope/src/main/java/hello/Hello.java @@ -0,0 +1,24 @@ +package hello; + +/* + * 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 Hello +{ +} diff --git a/src/it/projects/analyze-testDependencyWithNonTestScope/src/test/java/hello/HelloTest.java b/src/it/projects/analyze-testDependencyWithNonTestScope/src/test/java/hello/HelloTest.java new file mode 100644 index 0000000..c877aad --- /dev/null +++ b/src/it/projects/analyze-testDependencyWithNonTestScope/src/test/java/hello/HelloTest.java @@ -0,0 +1,35 @@ +package hello; + +/* + * 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.junit.Test; +import static org.junit.Assert.*; + +import static org.assertj.core.api.Assertions.assertThat; + +public class HelloTest { + + @Test + public void testAdd() { + assertEquals( 1 + 1, 2 ); + assertThat( "aaa" ).contains( "a" ); + } + +} diff --git a/src/it/projects/analyze-testDependencyWithNonTestScope/verify.bsh b/src/it/projects/analyze-testDependencyWithNonTestScope/verify.bsh new file mode 100644 index 0000000..9ff1775 --- /dev/null +++ b/src/it/projects/analyze-testDependencyWithNonTestScope/verify.bsh @@ -0,0 +1,63 @@ +/* + * 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 java.io.*; +import org.jsoup.Jsoup; +import org.jsoup.helper.Validate; +import org.jsoup.nodes.Document; +import org.jsoup.nodes.Element; +import org.jsoup.select.Elements; + +File htmlFile = new File( basedir, "target/dependency-analysis.html" ); + +if ( !htmlFile.isFile() ) +{ + throw new Exception( "Dependency analysis file missing " + htmlFile ); +} +if ( htmlFile.length() == 0 ) +{ + throw new Exception( "Dependency analysis file empty " + htmlFile ); +} + +Document doc = Jsoup.parse( htmlFile, "UTF-8"); + +Elements entry = doc.select( "#contentBox > section > section:nth-child(5) > table > tbody > tr.b" ); + +if( entry.isEmpty() ) +{ + throw new Exception ( "Can't find table entry under CompileScopedTestDependencies header " + htmlFile); +} +else if ( entry.size() != 1 ) +{ + throw new Exception ( "Table entry under CompileScopedTestDependencies header has " + entry.size() + + "rows but only 1 was expected " + htmlFile ); +} + +Element groupIdElement = doc.select( +"#contentBox > section > section:nth-child(5) > table > tbody > tr.b > td:nth-child(1)" ).first(); + +String groupId = groupIdElement.text(); + +if ( !groupId.equals( "junit" ) ) +{ + throw new Exception ( "Table entry under CompileScopedTestDependencies header groupId is not junit. Value is " + + groupId + " in " + htmlFile); +} + +return true; 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 1b44a5d..d32420c 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 @@ -49,7 +49,7 @@ import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter; /** * Analyzes the dependencies of this project and determines which are: used and declared; used and undeclared; unused - * and declared. + * and declared; compile scoped but only used in tests. * * @author <a href="mailto:markhob...@gmail.com">Mark Hobson</a> * @since 2.0-alpha-5 @@ -318,6 +318,8 @@ public abstract class AbstractAnalyzeMojo Set<Artifact> usedDeclared = new LinkedHashSet<>( analysis.getUsedDeclaredArtifacts() ); Set<Artifact> usedUndeclared = new LinkedHashSet<>( analysis.getUsedUndeclaredArtifacts() ); Set<Artifact> unusedDeclared = new LinkedHashSet<>( analysis.getUnusedDeclaredArtifacts() ); + Set<Artifact> testArtifactsWithNonTestScope = new LinkedHashSet<>( + analysis.getTestArtifactsWithNonTestScope() ); Set<Artifact> ignoredUsedUndeclared = new LinkedHashSet<>(); Set<Artifact> ignoredUnusedDeclared = new LinkedHashSet<>(); @@ -357,6 +359,15 @@ public abstract class AbstractAnalyzeMojo warning = true; } + if ( !testArtifactsWithNonTestScope.isEmpty() ) + { + getLog().warn( "Non-test scoped test only dependencies found:" ); + + logArtifacts( testArtifactsWithNonTestScope, true ); + reported = true; + warning = true; + } + if ( verbose && !ignoredUsedUndeclared.isEmpty() ) { getLog().info( "Ignored used undeclared dependencies:" ); diff --git a/src/main/java/org/apache/maven/plugins/dependency/analyze/AnalyzeReportView.java b/src/main/java/org/apache/maven/plugins/dependency/analyze/AnalyzeReportView.java index c537da0..571be74 100644 --- a/src/main/java/org/apache/maven/plugins/dependency/analyze/AnalyzeReportView.java +++ b/src/main/java/org/apache/maven/plugins/dependency/analyze/AnalyzeReportView.java @@ -108,6 +108,24 @@ public class AnalyzeReportView } sink.section2_(); + // Generate Non-Test Scoped Test Dependencies: + sink.section2(); + sink.sectionTitle2(); + sink.text( "Compile Scoped Test Dependencies" ); + sink.sectionTitle2_(); + if ( analysis.getTestArtifactsWithNonTestScope().isEmpty() ) + { + sink.paragraph(); + sink.text( "None" ); + sink.paragraph_(); + sink.horizontalRule(); + } + else + { + generateDependenciesTable( sink, analysis.getTestArtifactsWithNonTestScope().iterator() ); + } + sink.section2_(); + sink.section1_(); // Closing the report