You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by ad...@apache.org on 2019/04/04 17:20:39 UTC

[maven-pmd-plugin] 01/01: [MPMD-277] Plugin tries to download local submodules from repo

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

adangel pushed a commit to branch MPMD-277
in repository https://gitbox.apache.org/repos/asf/maven-pmd-plugin.git

commit 1b07b416a0219a86f39037ea765594458a27f3ce
Author: Andreas Dangel <ad...@apache.org>
AuthorDate: Wed Apr 3 19:39:58 2019 +0200

    [MPMD-277] Plugin tries to download local submodules from repo
    
    * Add Integration Test
    * Exclude modules of the reactor build
    * Issue a warning if the modules are not built yet
---
 .../invoker.properties                             |  2 +-
 .../invoker.properties                             |  4 +-
 .../MPMD-277-multi-module-check/module-a/pom.xml   | 33 +++++++++++
 .../module-a/src/main/java/module/a/IModuleA.java  | 24 ++++++++
 .../module-a/src/main/java/module/a/ModuleA.java   | 24 ++++++++
 .../MPMD-277-multi-module-check/module-b/pom.xml   | 46 +++++++++++++++
 .../module-b/src/main/java/module/b/ModuleB.java   | 54 +++++++++++++++++
 src/it/MPMD-277-multi-module-check/pom.xml         | 69 ++++++++++++++++++++++
 src/it/MPMD-277-multi-module-check/ruleset.xml     | 30 ++++++++++
 src/it/MPMD-277-multi-module-check/verify.groovy   | 23 ++++++++
 .../org/apache/maven/plugins/pmd/PmdReport.java    | 38 ++++++++++--
 11 files changed, 339 insertions(+), 8 deletions(-)

diff --git a/src/it/MPMD-266-typeresolution-aggregated/invoker.properties b/src/it/MPMD-266-typeresolution-aggregated/invoker.properties
index 2490429..81e4c21 100644
--- a/src/it/MPMD-266-typeresolution-aggregated/invoker.properties
+++ b/src/it/MPMD-266-typeresolution-aggregated/invoker.properties
@@ -15,6 +15,6 @@
 # specific language governing permissions and limitations
 # under the License.
 
-invoker.goals = clean install
+invoker.goals = clean package
 invoker.goals.2 = site
 invoker.maven.version = 3+
diff --git a/src/it/MPMD-266-typeresolution-aggregated/invoker.properties b/src/it/MPMD-277-multi-module-check/invoker.properties
similarity index 93%
copy from src/it/MPMD-266-typeresolution-aggregated/invoker.properties
copy to src/it/MPMD-277-multi-module-check/invoker.properties
index 2490429..abe0f0d 100644
--- a/src/it/MPMD-266-typeresolution-aggregated/invoker.properties
+++ b/src/it/MPMD-277-multi-module-check/invoker.properties
@@ -15,6 +15,6 @@
 # specific language governing permissions and limitations
 # under the License.
 
-invoker.goals = clean install
-invoker.goals.2 = site
+invoker.goals = clean package
+invoker.goals.2 = verify
 invoker.maven.version = 3+
diff --git a/src/it/MPMD-277-multi-module-check/module-a/pom.xml b/src/it/MPMD-277-multi-module-check/module-a/pom.xml
new file mode 100644
index 0000000..c972de6
--- /dev/null
+++ b/src/it/MPMD-277-multi-module-check/module-a/pom.xml
@@ -0,0 +1,33 @@
+<?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>
+  <modelVersion>4.0.0</modelVersion>
+
+  <parent>
+    <groupId>org.apache.maven.plugins.pmd.it</groupId>
+    <artifactId>MPMD-277-multi-module-check-parent</artifactId>
+    <version>1.0-SNAPSHOT</version>
+  </parent>
+
+  <artifactId>MPMD-277-multi-module-check-module-a</artifactId>
+
+</project>
diff --git a/src/it/MPMD-277-multi-module-check/module-a/src/main/java/module/a/IModuleA.java b/src/it/MPMD-277-multi-module-check/module-a/src/main/java/module/a/IModuleA.java
new file mode 100644
index 0000000..83cfecf
--- /dev/null
+++ b/src/it/MPMD-277-multi-module-check/module-a/src/main/java/module/a/IModuleA.java
@@ -0,0 +1,24 @@
+package module.a;
+
+/*
+ * 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 interface IModuleA
+{
+}
\ No newline at end of file
diff --git a/src/it/MPMD-277-multi-module-check/module-a/src/main/java/module/a/ModuleA.java b/src/it/MPMD-277-multi-module-check/module-a/src/main/java/module/a/ModuleA.java
new file mode 100644
index 0000000..9d5d21d
--- /dev/null
+++ b/src/it/MPMD-277-multi-module-check/module-a/src/main/java/module/a/ModuleA.java
@@ -0,0 +1,24 @@
+package module.a;
+
+/*
+ * 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 ModuleA implements IModuleA
+{
+}
\ No newline at end of file
diff --git a/src/it/MPMD-277-multi-module-check/module-b/pom.xml b/src/it/MPMD-277-multi-module-check/module-b/pom.xml
new file mode 100644
index 0000000..8d76fcd
--- /dev/null
+++ b/src/it/MPMD-277-multi-module-check/module-b/pom.xml
@@ -0,0 +1,46 @@
+<?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>
+  <modelVersion>4.0.0</modelVersion>
+
+  <parent>
+    <groupId>org.apache.maven.plugins.pmd.it</groupId>
+    <artifactId>MPMD-277-multi-module-check-parent</artifactId>
+    <version>1.0-SNAPSHOT</version>
+  </parent>
+
+  <artifactId>MPMD-277-multi-module-check-module-b</artifactId>
+
+  <dependencies>
+    <dependency>
+      <groupId>${project.groupId}</groupId>
+      <artifactId>MPMD-277-multi-module-check-module-a</artifactId>
+      <version>${project.version}</version>
+    </dependency>
+
+    <dependency>
+      <groupId>org.apache.commons</groupId>
+      <artifactId>commons-math</artifactId>
+      <version>2.2</version>
+    </dependency>
+  </dependencies>
+</project>
diff --git a/src/it/MPMD-277-multi-module-check/module-b/src/main/java/module/b/ModuleB.java b/src/it/MPMD-277-multi-module-check/module-b/src/main/java/module/b/ModuleB.java
new file mode 100644
index 0000000..0689d93
--- /dev/null
+++ b/src/it/MPMD-277-multi-module-check/module-b/src/main/java/module/b/ModuleB.java
@@ -0,0 +1,54 @@
+package module.b;
+
+/*
+ * 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 module.a.IModuleA;
+import module.a.ModuleA;
+import org.apache.commons.math.complex.Complex;
+import org.apache.commons.math.FieldElement;
+
+public class ModuleB
+{
+    public static void main( String[] args )
+    {
+        ModuleA m = new ModuleA();
+        doSomething( m );
+    }
+
+    // this method will be detected as being unsued,
+    // if typeresolution is not setup correctly: module a needs
+    // to be on PMD's auxclasspath, so that PMD knows, that ModuleA
+    // implements IModuleA
+    private static void doSomething( IModuleA module )
+    {
+        System.out.println( module );
+    }
+
+    public static void aPublicMethod()
+    {
+        Complex u = new Complex(1, 1);
+        aPrivateMethod( u );
+    }
+
+    private static void aPrivateMethod( FieldElement<Complex> u )
+    {
+        System.out.println( "aPrivateMethod: " + u );
+    }
+}
\ No newline at end of file
diff --git a/src/it/MPMD-277-multi-module-check/pom.xml b/src/it/MPMD-277-multi-module-check/pom.xml
new file mode 100644
index 0000000..5c7b887
--- /dev/null
+++ b/src/it/MPMD-277-multi-module-check/pom.xml
@@ -0,0 +1,69 @@
+<?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>
+  <modelVersion>4.0.0</modelVersion>
+
+  <groupId>org.apache.maven.plugins.pmd.it</groupId>
+  <artifactId>MPMD-277-multi-module-check-parent</artifactId>
+  <version>1.0-SNAPSHOT</version>
+  <packaging>pom</packaging>
+
+  <properties>
+    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+    <maven.compiler.source>1.8</maven.compiler.source>
+    <maven.compiler.target>1.8</maven.compiler.target>
+  </properties>
+
+  <modules>
+    <module>module-a</module>
+    <module>module-b</module>
+  </modules>
+
+  <build>
+    <pluginManagement>
+      <plugins>
+        <plugin>
+          <groupId>@project.groupId@</groupId>
+          <artifactId>@project.artifactId@</artifactId>
+          <version>@project.version@</version>
+          <configuration>
+            <typeResolution>true</typeResolution>
+            <aggregate>true</aggregate>
+          </configuration>
+          <executions>
+            <execution>
+              <goals>
+                <goal>check</goal>
+              </goals>
+            </execution>
+          </executions>
+        </plugin>
+      </plugins>
+    </pluginManagement>
+    <plugins>
+      <plugin>
+        <groupId>@project.groupId@</groupId>
+        <artifactId>@project.artifactId@</artifactId>
+      </plugin>
+    </plugins>
+  </build>
+</project>
diff --git a/src/it/MPMD-277-multi-module-check/ruleset.xml b/src/it/MPMD-277-multi-module-check/ruleset.xml
new file mode 100644
index 0000000..004e68a
--- /dev/null
+++ b/src/it/MPMD-277-multi-module-check/ruleset.xml
@@ -0,0 +1,30 @@
+<?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.
+-->
+
+<ruleset name="Custom ruleset"
+    xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
+    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+    xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 http://pmd.sourceforge.net/ruleset_2_0_0.xsd">
+    <description>Custom Ruleset for test case MPMD-277</description>
+
+    <rule ref="category/java/bestpractices.xml/UnusedPrivateMethod" />
+
+</ruleset>
diff --git a/src/it/MPMD-277-multi-module-check/verify.groovy b/src/it/MPMD-277-multi-module-check/verify.groovy
new file mode 100644
index 0000000..ac95dd7
--- /dev/null
+++ b/src/it/MPMD-277-multi-module-check/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.
+ */
+
+File pmdXml = new File( basedir, "target/pmd.xml" )
+assert pmdXml.exists()
+assert !pmdXml.text.contains( "Avoid unused private methods such as 'doSomething(IModuleA)'." )
+assert !pmdXml.text.contains( "Avoid unused private methods such as 'aPrivateMethod(FieldElement)'." )
diff --git a/src/main/java/org/apache/maven/plugins/pmd/PmdReport.java b/src/main/java/org/apache/maven/plugins/pmd/PmdReport.java
index bc68b08..9ea9a03 100644
--- a/src/main/java/org/apache/maven/plugins/pmd/PmdReport.java
+++ b/src/main/java/org/apache/maven/plugins/pmd/PmdReport.java
@@ -45,7 +45,10 @@ import org.apache.maven.project.DefaultProjectBuildingRequest;
 import org.apache.maven.project.MavenProject;
 import org.apache.maven.project.ProjectBuildingRequest;
 import org.apache.maven.reporting.MavenReportException;
+import org.apache.maven.shared.artifact.filter.resolve.AndFilter;
+import org.apache.maven.shared.artifact.filter.resolve.ExclusionsFilter;
 import org.apache.maven.shared.artifact.filter.resolve.ScopeFilter;
+import org.apache.maven.shared.artifact.filter.resolve.TransformableFilter;
 import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResult;
 import org.apache.maven.shared.transfer.dependencies.resolve.DependencyResolver;
 import org.codehaus.plexus.resource.ResourceManager;
@@ -721,24 +724,49 @@ public class PmdReport
             {
                 List<String> dependencies = new ArrayList<>();
 
+                // collect exclusions for projects within the reactor
+                // if module a depends on module b and both are in the reactor
+                // then we don't want to resolve the dependency as an artifact.
+                List<String> exclusionPatterns = new ArrayList<>();
                 for ( MavenProject localProject : reactorProjects )
                 {
-                    // Add the project's target folder first
-                    classpath.addAll( includeTests ? localProject.getTestClasspathElements()
-                            : localProject.getCompileClasspathElements() );
+                    exclusionPatterns.add( localProject.getGroupId() + ":" + localProject.getArtifactId() );
+                }
+                TransformableFilter filter = new AndFilter( Arrays.asList(
+                        new ExclusionsFilter( exclusionPatterns ),
+                        includeTests ? ScopeFilter.including( "test" ) : ScopeFilter.including( "compile" )
+                ) );
 
+                for ( MavenProject localProject : reactorProjects )
+                {
                     ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(
                             session.getProjectBuildingRequest() );
 
                     Iterable<ArtifactResult> resolvedDependencies = dependencyResolver.resolveDependencies(
-                            buildingRequest, localProject.getModel(),
-                            includeTests ? ScopeFilter.including( "test" ) : ScopeFilter.including( "compile" ) );
+                            buildingRequest, localProject.getModel(), filter );
 
                     for ( ArtifactResult resolvedArtifact : resolvedDependencies )
                     {
                         dependencies.add( resolvedArtifact.getArtifact().getFile().toString() );
                     }
 
+                    List<String> projectCompileClasspath = includeTests ? localProject.getTestClasspathElements()
+                            : localProject.getCompileClasspathElements();
+                    // Add the project's target folder first
+                    classpath.addAll( projectCompileClasspath );
+                    if ( !localProject.isExecutionRoot() )
+                    {
+                        for ( String path : projectCompileClasspath )
+                        {
+                            File pathFile = new File( path );
+                            if ( !pathFile.exists() || pathFile.list().length == 0 )
+                            {
+                                getLog().warn( "The project " + localProject.getArtifactId()
+                                    + " does not seem to be compiled. PMD results might be inaccurate." );
+                            }
+                        }
+                    }
+
                 }
 
                 // Add the dependencies as last entries