You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by sj...@apache.org on 2022/05/27 15:45:11 UTC

[maven-dependency-plugin] branch master updated: [MDEP-602] Log dependency warnings as errors when failOnWarning is set

This is an automated email from the ASF dual-hosted git repository.

sjaranowski 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 f2431f5a [MDEP-602] Log dependency warnings as errors when failOnWarning is set
f2431f5a is described below

commit f2431f5a6601c8f324c2861b3a98f37b1b84e685
Author: Halil Sener <hi...@yahoo.com>
AuthorDate: Tue May 24 22:54:46 2022 +0200

    [MDEP-602] Log dependency warnings as errors when failOnWarning is set
---
 .../projects/analyze/{verify.bsh => verify.groovy} | 18 +++++--
 .../invoker.properties                             | 19 ++++++++
 .../pom.xml                                        | 55 ++++++++++++++++++++++
 .../src/main/java/Main.java}                       |  9 +---
 .../verify.groovy}                                 | 12 ++---
 .../dependency/analyze/AbstractAnalyzeMojo.java    | 24 +++++++---
 6 files changed, 113 insertions(+), 24 deletions(-)

diff --git a/src/it/projects/analyze/verify.bsh b/src/it/projects/analyze/verify.groovy
similarity index 54%
copy from src/it/projects/analyze/verify.bsh
copy to src/it/projects/analyze/verify.groovy
index ab10fcd4..0ff117d2 100644
--- a/src/it/projects/analyze/verify.bsh
+++ b/src/it/projects/analyze/verify.groovy
@@ -17,13 +17,25 @@
  * under the License.
  */
 
-import java.io.*;
-
 File classFile = new File( basedir, "target/classes/Main.class" );
-
+assert classFile.exists();
 if ( !classFile.isFile() )
 {
     throw new Exception( "Build was not forked, class missing " + classFile );
 }
 
+File file = new File( basedir, "build.log" );
+assert file.exists();
+
+String buildLog = file.getText( "UTF-8" );
+assert buildLog.contains( '[INFO] Used declared dependencies found:');
+assert buildLog.contains( '[INFO]    org.apache.maven:maven-artifact:jar:2.0.6:compile');
+assert buildLog.contains( '[INFO]    org.apache.maven:maven-model:jar:2.0.6:compile');
+assert buildLog.contains( '[WARNING] Used undeclared dependencies found:');
+assert buildLog.contains( '[WARNING]    org.apache.maven:maven-repository-metadata:jar:2.0.6:compile');
+assert buildLog.contains( '[WARNING]       class org.apache.maven.artifact.repository.metadata.Metadata');
+assert buildLog.contains( '[WARNING] Unused declared dependencies found:');
+assert buildLog.contains( '[WARNING]    org.apache.maven:maven-project:jar:2.0.6:compile');
+
+
 return true;
diff --git a/src/it/projects/mdep-602-log-on-error-level-when-failOnWarning/invoker.properties b/src/it/projects/mdep-602-log-on-error-level-when-failOnWarning/invoker.properties
new file mode 100644
index 00000000..210b607b
--- /dev/null
+++ b/src/it/projects/mdep-602-log-on-error-level-when-failOnWarning/invoker.properties
@@ -0,0 +1,19 @@
+# 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.buildResult = failure
+invoker.goals = clean ${project.groupId}:${project.artifactId}:${project.version}:analyze
diff --git a/src/it/projects/mdep-602-log-on-error-level-when-failOnWarning/pom.xml b/src/it/projects/mdep-602-log-on-error-level-when-failOnWarning/pom.xml
new file mode 100644
index 00000000..16eae674
--- /dev/null
+++ b/src/it/projects/mdep-602-log-on-error-level-when-failOnWarning/pom.xml
@@ -0,0 +1,55 @@
+<?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</groupId>
+  <artifactId>mdep-602-log-on-error-level-when-failOnWarning</artifactId>
+  <version>1.0.0-SNAPSHOT</version>
+
+  <description>Test that dependency:analyze logs dependency warnings as errors when failOnWarning is set to true</description>
+
+  <properties>
+    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+  </properties>
+
+  <dependencies>
+    <dependency>
+      <groupId>org.apache.maven</groupId>
+      <artifactId>maven-project</artifactId>
+      <version>2.0.6</version>
+    </dependency>
+  </dependencies>
+
+  <build>
+    <pluginManagement>
+      <plugins>
+        <plugin>
+          <artifactId>maven-dependency-plugin</artifactId>
+          <configuration>
+            <failOnWarning>true</failOnWarning>
+          </configuration>
+        </plugin>
+      </plugins>
+    </pluginManagement>
+  </build>
+</project>
diff --git a/src/it/projects/analyze/verify.bsh b/src/it/projects/mdep-602-log-on-error-level-when-failOnWarning/src/main/java/Main.java
similarity index 79%
copy from src/it/projects/analyze/verify.bsh
copy to src/it/projects/mdep-602-log-on-error-level-when-failOnWarning/src/main/java/Main.java
index ab10fcd4..293e02c1 100644
--- a/src/it/projects/analyze/verify.bsh
+++ b/src/it/projects/mdep-602-log-on-error-level-when-failOnWarning/src/main/java/Main.java
@@ -17,13 +17,6 @@
  * under the License.
  */
 
-import java.io.*;
-
-File classFile = new File( basedir, "target/classes/Main.class" );
-
-if ( !classFile.isFile() )
+public class Main
 {
-    throw new Exception( "Build was not forked, class missing " + classFile );
 }
-
-return true;
diff --git a/src/it/projects/analyze/verify.bsh b/src/it/projects/mdep-602-log-on-error-level-when-failOnWarning/verify.groovy
similarity index 74%
rename from src/it/projects/analyze/verify.bsh
rename to src/it/projects/mdep-602-log-on-error-level-when-failOnWarning/verify.groovy
index ab10fcd4..e3ff3530 100644
--- a/src/it/projects/analyze/verify.bsh
+++ b/src/it/projects/mdep-602-log-on-error-level-when-failOnWarning/verify.groovy
@@ -17,13 +17,11 @@
  * under the License.
  */
 
-import java.io.*;
+File file = new File( basedir, "build.log" );
+assert file.exists();
 
-File classFile = new File( basedir, "target/classes/Main.class" );
-
-if ( !classFile.isFile() )
-{
-    throw new Exception( "Build was not forked, class missing " + classFile );
-}
+String buildLog = file.getText( "UTF-8" );
+assert buildLog.contains( '[ERROR] Unused declared dependencies found:');
+assert buildLog.contains( '[ERROR]    org.apache.maven:maven-project:jar:2.0.6:compile' );
 
 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 384bb965..c421ab68 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
@@ -406,7 +406,7 @@ public abstract class AbstractAnalyzeMojo
 
         if ( !usedUndeclaredWithClasses.isEmpty() )
         {
-            getLog().warn( "Used undeclared dependencies found:" );
+            logDependencyWarning( "Used undeclared dependencies found:" );
 
             if ( verbose )
             {
@@ -422,7 +422,7 @@ public abstract class AbstractAnalyzeMojo
 
         if ( !unusedDeclared.isEmpty() )
         {
-            getLog().warn( "Unused declared dependencies found:" );
+            logDependencyWarning( "Unused declared dependencies found:" );
 
             logArtifacts( unusedDeclared, true );
             reported = true;
@@ -431,7 +431,7 @@ public abstract class AbstractAnalyzeMojo
 
         if ( !nonTestScope.isEmpty() )
         {
-            getLog().warn( "Non-test scoped test only dependencies found:" );
+            logDependencyWarning( "Non-test scoped test only dependencies found:" );
 
             logArtifacts( nonTestScope, true );
             reported = true;
@@ -500,7 +500,7 @@ public abstract class AbstractAnalyzeMojo
 
                 if ( warn )
                 {
-                    getLog().warn( "   " + artifact );
+                    logDependencyWarning( "   " + artifact );
                 }
                 else
                 {
@@ -526,10 +526,10 @@ public abstract class AbstractAnalyzeMojo
 
                 if ( warn )
                 {
-                    getLog().warn( "   " + entry.getKey() );
+                    logDependencyWarning( "   " + entry.getKey() );
                     for ( String clazz : entry.getValue() )
                     {
-                        getLog().warn( "      class " + clazz );
+                        logDependencyWarning( "      class " + clazz );
                     }
                 }
                 else
@@ -545,6 +545,18 @@ public abstract class AbstractAnalyzeMojo
         }
     }
 
+    private void logDependencyWarning( CharSequence content )
+    {
+        if ( failOnWarning )
+        {
+            getLog().error( content );
+        }
+        else
+        {
+            getLog().warn( content );
+        }
+    }
+
     private void writeDependencyXML( Set<Artifact> artifacts )
     {
         if ( !artifacts.isEmpty() )