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 cf2f3a9e [MDEP-928] Allow to exclude classes from dependency:analyze
cf2f3a9e is described below

commit cf2f3a9e5cbf79b0c8f7088374ce0379d34673a2
Author: Slawomir Jaranowski <s.jaranow...@gmail.com>
AuthorDate: Tue May 21 21:36:42 2024 +0200

    [MDEP-928] Allow to exclude classes from dependency:analyze
---
 pom.xml                                            |  6 --
 .../analyze-excluded-classes/invoker.properties    | 18 ++++++
 src/it/projects/analyze-excluded-classes/pom.xml   | 70 ++++++++++++++++++++++
 .../projects/analyze-excluded-classes/setup.groovy | 26 ++++++++
 .../src/main/java/Main.java                        | 31 ++++++++++
 .../analyze-excluded-classes/verify.groovy         | 31 ++++++++++
 .../dependency/analyze/AbstractAnalyzeMojo.java    | 10 +++-
 .../dependency/analyze/AnalyzeReportMojo.java      | 11 +++-
 8 files changed, 195 insertions(+), 8 deletions(-)

diff --git a/pom.xml b/pom.xml
index 301f67b1..c6b7658c 100644
--- a/pom.xml
+++ b/pom.xml
@@ -243,12 +243,6 @@ under the License.
       <groupId>org.apache.maven.shared</groupId>
       <artifactId>maven-dependency-analyzer</artifactId>
       <version>1.14.0</version>
-      <exclusions>
-        <exclusion>
-          <groupId>org.apache.maven</groupId>
-          <artifactId>*</artifactId>
-        </exclusion>
-      </exclusions>
     </dependency>
     <dependency>
       <groupId>org.apache.maven.shared</groupId>
diff --git a/src/it/projects/analyze-excluded-classes/invoker.properties 
b/src/it/projects/analyze-excluded-classes/invoker.properties
new file mode 100644
index 00000000..0eca1be8
--- /dev/null
+++ b/src/it/projects/analyze-excluded-classes/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 = 
${project.groupId}:${project.artifactId}:${project.version}:analyze
diff --git a/src/it/projects/analyze-excluded-classes/pom.xml 
b/src/it/projects/analyze-excluded-classes/pom.xml
new file mode 100644
index 00000000..7d678478
--- /dev/null
+++ b/src/it/projects/analyze-excluded-classes/pom.xml
@@ -0,0 +1,70 @@
+<?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</groupId>
+  <artifactId>test</artifactId>
+  <version>1.0-SNAPSHOT</version>
+
+  <name>Test</name>
+  <description>
+    Test dependency:analyze with excluded classes
+  </description>
+
+  <properties>
+    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+  </properties>
+
+  <dependencies>
+    <dependency>
+      <groupId>org.apache.maven</groupId>
+      <artifactId>maven-artifact</artifactId>
+      <version>@mavenVersion@</version>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.maven</groupId>
+      <artifactId>maven-core</artifactId>
+      <version>@mavenVersion@</version>
+    </dependency>
+  </dependencies>
+
+  <build>
+    <pluginManagement>
+      <plugins>
+        <plugin>
+          <artifactId>maven-dependency-plugin</artifactId>
+          <version>@project.version@</version>
+          <configuration>
+            <outputXML>true</outputXML>
+            <scriptableOutput>true</scriptableOutput>
+            <verbose>true</verbose>
+            <excludedClasses>
+              <exclude>org.example.Bad.*</exclude>
+            </excludedClasses>
+          </configuration>
+        </plugin>
+      </plugins>
+    </pluginManagement>
+  </build>
+</project>
diff --git a/src/it/projects/analyze-excluded-classes/setup.groovy 
b/src/it/projects/analyze-excluded-classes/setup.groovy
new file mode 100644
index 00000000..c4521097
--- /dev/null
+++ b/src/it/projects/analyze-excluded-classes/setup.groovy
@@ -0,0 +1,26 @@
+/*
+ * 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.
+ */
+
+// prepare a class with wrong bytecode
+def badClass = new File(basedir, 'target/classes/org/example/BadClass.class')
+
+badClass.getParentFile().mkdirs()
+badClass << 'some content'
+
+assert badClass.isFile()
diff --git a/src/it/projects/analyze-excluded-classes/src/main/java/Main.java 
b/src/it/projects/analyze-excluded-classes/src/main/java/Main.java
new file mode 100644
index 00000000..e4d166fa
--- /dev/null
+++ b/src/it/projects/analyze-excluded-classes/src/main/java/Main.java
@@ -0,0 +1,31 @@
+/*
+ * 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.apache.maven.artifact.Artifact;
+import org.apache.maven.artifact.repository.metadata.Metadata;
+import org.apache.maven.model.Model;
+
+public class Main
+{
+    public static final String SCOPE_COMPILE = Artifact.SCOPE_COMPILE;
+
+    public Model model = null;
+
+    public Metadata metadata = null;
+}
diff --git a/src/it/projects/analyze-excluded-classes/verify.groovy 
b/src/it/projects/analyze-excluded-classes/verify.groovy
new file mode 100644
index 00000000..47457c6f
--- /dev/null
+++ b/src/it/projects/analyze-excluded-classes/verify.groovy
@@ -0,0 +1,31 @@
+/*
+ * 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.
+ */
+
+File classFile = new File( basedir, "target/classes/Main.class" )
+assert classFile.exists()
+assert classFile.isFile() : "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( '[WARNING] Used undeclared dependencies found:')
+assert buildLog.contains( '[WARNING]    
org.apache.maven:maven-model:jar:3.6.3:compile')
+assert buildLog.contains( '[WARNING] Unused declared dependencies found:')
+assert buildLog.contains( '[WARNING]    
org.apache.maven:maven-core:jar:3.6.3:compile')
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 59b3ba3c..35b287ec 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
@@ -266,6 +266,14 @@ public abstract class AbstractAnalyzeMojo extends 
AbstractMojo {
     @Parameter
     private List<String> ignoredPackagings = Arrays.asList("pom", "ear");
 
+    /**
+     * List Excluded classes patterns from analyze. Java regular expression 
pattern is applied to full class name.
+     *
+     * @since 3.7.0
+     */
+    @Parameter(property = "mdep.analyze.excludedClasses")
+    private Set<String> excludedClasses;
+
     // Mojo methods -----------------------------------------------------------
 
     /*
@@ -321,7 +329,7 @@ public abstract class AbstractAnalyzeMojo extends 
AbstractMojo {
     private boolean checkDependencies() throws MojoExecutionException {
         ProjectDependencyAnalysis analysis;
         try {
-            analysis = createProjectDependencyAnalyzer().analyze(project);
+            analysis = createProjectDependencyAnalyzer().analyze(project, 
excludedClasses);
 
             if (usedDependencies != null) {
                 analysis = 
analysis.forceDeclaredDependenciesUsage(usedDependencies);
diff --git 
a/src/main/java/org/apache/maven/plugins/dependency/analyze/AnalyzeReportMojo.java
 
b/src/main/java/org/apache/maven/plugins/dependency/analyze/AnalyzeReportMojo.java
index 5ef0ea7c..0c8d4ce6 100644
--- 
a/src/main/java/org/apache/maven/plugins/dependency/analyze/AnalyzeReportMojo.java
+++ 
b/src/main/java/org/apache/maven/plugins/dependency/analyze/AnalyzeReportMojo.java
@@ -19,6 +19,7 @@
 package org.apache.maven.plugins.dependency.analyze;
 
 import java.util.Locale;
+import java.util.Set;
 
 import org.apache.maven.plugins.annotations.Component;
 import org.apache.maven.plugins.annotations.Execute;
@@ -75,6 +76,14 @@ public class AnalyzeReportMojo extends AbstractMavenReport {
     @Parameter(property = "mdep.analyze.skip", defaultValue = "false")
     private boolean skip;
 
+    /**
+     * List Excluded classes patterns from analyze. Java regular expression 
pattern is applied to full class name.
+     *
+     * @since 3.7.0
+     */
+    @Parameter(property = "mdep.analyze.excludedClasses")
+    private Set<String> excludedClasses;
+
     /**
      * Internationalization component
      */
@@ -91,7 +100,7 @@ public class AnalyzeReportMojo extends AbstractMavenReport {
         // Step 1: Analyze the project
         ProjectDependencyAnalysis analysis;
         try {
-            analysis = analyzer.analyze(project);
+            analysis = analyzer.analyze(project, excludedClasses);
 
             if (usedDependencies != null) {
                 analysis = 
analysis.forceDeclaredDependenciesUsage(usedDependencies);

Reply via email to