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 2016/02/10 20:30:07 UTC

svn commit: r1729696 - in /maven/plugins/trunk/maven-pmd-plugin/src: main/java/org/apache/maven/plugin/pmd/ test/java/org/apache/maven/plugin/pmd/ test/resources/unit/exclude-roots/ test/resources/unit/exclude-roots/baseroot/ test/resources/unit/exclud...

Author: adangel
Date: Wed Feb 10 19:30:07 2016
New Revision: 1729696

URL: http://svn.apache.org/viewvc?rev=1729696&view=rev
Log:
[MPMD-178] excludeRoots should handle subdirectories

Contributed by: Michael Yates

Added:
    maven/plugins/trunk/maven-pmd-plugin/src/test/resources/unit/exclude-roots/
    maven/plugins/trunk/maven-pmd-plugin/src/test/resources/unit/exclude-roots/baseroot/
    maven/plugins/trunk/maven-pmd-plugin/src/test/resources/unit/exclude-roots/baseroot/src1/
    maven/plugins/trunk/maven-pmd-plugin/src/test/resources/unit/exclude-roots/baseroot/src1/PMDViolationExample.java
    maven/plugins/trunk/maven-pmd-plugin/src/test/resources/unit/exclude-roots/baseroot/src2/
    maven/plugins/trunk/maven-pmd-plugin/src/test/resources/unit/exclude-roots/baseroot/src2/PMDViolationExample.java
    maven/plugins/trunk/maven-pmd-plugin/src/test/resources/unit/exclude-roots/othersrc/
    maven/plugins/trunk/maven-pmd-plugin/src/test/resources/unit/exclude-roots/othersrc/PMDViolationExample.java
    maven/plugins/trunk/maven-pmd-plugin/src/test/resources/unit/exclude-roots/pmd-exclude-roots-plugin-config.xml
    maven/plugins/trunk/maven-pmd-plugin/src/test/resources/unit/exclude-roots/src/
    maven/plugins/trunk/maven-pmd-plugin/src/test/resources/unit/exclude-roots/src/PMDViolationExample.java
Modified:
    maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/AbstractPmdReport.java
    maven/plugins/trunk/maven-pmd-plugin/src/test/java/org/apache/maven/plugin/pmd/PmdReportTest.java

Modified: maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/AbstractPmdReport.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/AbstractPmdReport.java?rev=1729696&r1=1729695&r2=1729696&view=diff
==============================================================================
--- maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/AbstractPmdReport.java (original)
+++ maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/AbstractPmdReport.java Wed Feb 10 19:30:07 2016
@@ -366,7 +366,7 @@ public abstract class AbstractPmdReport
         {
             getLog().debug( "Searching for files in directory " + finfo.getSourceDirectory().toString() );
             File sourceDirectory = finfo.getSourceDirectory();
-            if ( sourceDirectory.isDirectory() && !excludeRootFiles.contains( sourceDirectory ) )
+            if ( sourceDirectory.isDirectory() && !isDirectoryExcluded( excludeRootFiles, sourceDirectory ) )
             {
                 List<File> newfiles = FileUtils.getFiles( sourceDirectory, including, excluding );
                 for ( File newfile : newfiles )
@@ -379,6 +379,22 @@ public abstract class AbstractPmdReport
         return files;
     }
 
+    private boolean isDirectoryExcluded( Collection<File> excludeRootFiles, File sourceDirectoryToCheck )
+    {
+        boolean returnVal = false;
+        for ( File excludeDir : excludeRootFiles )
+        {
+            if ( sourceDirectoryToCheck.getAbsolutePath().startsWith( excludeDir.getAbsolutePath() ) )
+            {
+                getLog().debug( "Directory " + sourceDirectoryToCheck.getAbsolutePath()
+                                    + " has been excluded as it matches excludeRoot " + excludeDir.getAbsolutePath() );
+                returnVal = true;
+                break;
+            }
+        }
+        return returnVal;
+    }
+
     /**
      * Gets the comma separated list of effective include patterns.
      *

Modified: maven/plugins/trunk/maven-pmd-plugin/src/test/java/org/apache/maven/plugin/pmd/PmdReportTest.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-pmd-plugin/src/test/java/org/apache/maven/plugin/pmd/PmdReportTest.java?rev=1729696&r1=1729695&r2=1729696&view=diff
==============================================================================
--- maven/plugins/trunk/maven-pmd-plugin/src/test/java/org/apache/maven/plugin/pmd/PmdReportTest.java (original)
+++ maven/plugins/trunk/maven-pmd-plugin/src/test/java/org/apache/maven/plugin/pmd/PmdReportTest.java Wed Feb 10 19:30:07 2016
@@ -441,4 +441,18 @@ public class PmdReportTest
             assertTrue( e.getMessage().endsWith( "Found 1 PMD processing errors" ) );
         }
     }
+
+    public void testPMDExcludeRootsShouldExcludeSubdirectories() throws Exception {
+        File testPom = new File(getBasedir(), "src/test/resources/unit/exclude-roots/pmd-exclude-roots-plugin-config.xml");
+        PmdReport mojo = (PmdReport) lookupMojo ("pmd", testPom);
+        mojo.execute();
+
+        File generatedFile = new File( getBasedir(), "target/test/unit/exclude-roots/target/pmd.xml" );
+        assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
+        String str = readFile( generatedFile );
+
+        assertTrue( "Seems like all directories are excluded now", str.contains("ForLoopShouldBeWhileLoop") );
+        assertFalse( "Exclusion of an exact source directory not working", str.contains( "OverrideBothEqualsAndHashcode" ) );
+        assertFalse( "Exclusion of basedirectory with subdirectories not working (MPMD-178)", str.contains( "JumbledIncrementer") );
+    }
 }

Added: maven/plugins/trunk/maven-pmd-plugin/src/test/resources/unit/exclude-roots/baseroot/src1/PMDViolationExample.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-pmd-plugin/src/test/resources/unit/exclude-roots/baseroot/src1/PMDViolationExample.java?rev=1729696&view=auto
==============================================================================
--- maven/plugins/trunk/maven-pmd-plugin/src/test/resources/unit/exclude-roots/baseroot/src1/PMDViolationExample.java (added)
+++ maven/plugins/trunk/maven-pmd-plugin/src/test/resources/unit/exclude-roots/baseroot/src1/PMDViolationExample.java Wed Feb 10 19:30:07 2016
@@ -0,0 +1,28 @@
+/*
+ * 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 PMDViolationExample {
+    public void jumbledIncrementer() {
+        for (int i = 0; i < 10; i++) {          // only references 'i'
+            for (int k = 0; k < 20; i++) {      // references both 'i' and 'k'
+                System.out.println("Hello");
+            }
+        }
+    }
+}
\ No newline at end of file

Added: maven/plugins/trunk/maven-pmd-plugin/src/test/resources/unit/exclude-roots/baseroot/src2/PMDViolationExample.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-pmd-plugin/src/test/resources/unit/exclude-roots/baseroot/src2/PMDViolationExample.java?rev=1729696&view=auto
==============================================================================
--- maven/plugins/trunk/maven-pmd-plugin/src/test/resources/unit/exclude-roots/baseroot/src2/PMDViolationExample.java (added)
+++ maven/plugins/trunk/maven-pmd-plugin/src/test/resources/unit/exclude-roots/baseroot/src2/PMDViolationExample.java Wed Feb 10 19:30:07 2016
@@ -0,0 +1,28 @@
+/*
+ * 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 PMDViolationExample {
+    public void jumbledIncrementer() {
+        for (int i = 0; i < 10; i++) {          // only references 'i'
+            for (int k = 0; k < 20; i++) {      // references both 'i' and 'k'
+                System.out.println("Hello");
+            }
+        }
+    }
+}
\ No newline at end of file

Added: maven/plugins/trunk/maven-pmd-plugin/src/test/resources/unit/exclude-roots/othersrc/PMDViolationExample.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-pmd-plugin/src/test/resources/unit/exclude-roots/othersrc/PMDViolationExample.java?rev=1729696&view=auto
==============================================================================
--- maven/plugins/trunk/maven-pmd-plugin/src/test/resources/unit/exclude-roots/othersrc/PMDViolationExample.java (added)
+++ maven/plugins/trunk/maven-pmd-plugin/src/test/resources/unit/exclude-roots/othersrc/PMDViolationExample.java Wed Feb 10 19:30:07 2016
@@ -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.
+ */
+
+public class PMDViolationExample {
+    // OverrideBothEqualsAndHashcode!
+    @Override
+    public int hashCode() {
+        return 0;
+    }
+}
\ No newline at end of file

Added: maven/plugins/trunk/maven-pmd-plugin/src/test/resources/unit/exclude-roots/pmd-exclude-roots-plugin-config.xml
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-pmd-plugin/src/test/resources/unit/exclude-roots/pmd-exclude-roots-plugin-config.xml?rev=1729696&view=auto
==============================================================================
--- maven/plugins/trunk/maven-pmd-plugin/src/test/resources/unit/exclude-roots/pmd-exclude-roots-plugin-config.xml (added)
+++ maven/plugins/trunk/maven-pmd-plugin/src/test/resources/unit/exclude-roots/pmd-exclude-roots-plugin-config.xml Wed Feb 10 19:30:07 2016
@@ -0,0 +1,72 @@
+<!--
+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>def.configuration</groupId>
+  <artifactId>pmd-exclude-roots-config</artifactId>
+  <packaging>jar</packaging>
+  <version>1.0-SNAPSHOT</version>
+  <inceptionYear>2006</inceptionYear>
+  <name>Maven PMD Plugin ExcludeRoots Configuration Test</name>
+  <url>http://maven.apache.org</url>
+  <build>
+    <finalName>pmd-exclude-roots-config</finalName>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-pmd-plugin</artifactId>
+        <configuration>
+          <project implementation="org.apache.maven.plugin.pmd.stubs.DefaultConfigurationMavenProjectStub"/>
+          <outputDirectory>${basedir}/target/test/unit/exclude-roots/target/site</outputDirectory>
+          <targetDirectory>${basedir}/target/test/unit/exclude-roots/target</targetDirectory>
+          <format>xml</format>
+          <sourceEncoding>UTF-8</sourceEncoding>
+
+          <compileSourceRoots>
+            <compileSourceRoot>${basedir}/src/test/resources/unit/exclude-roots/src</compileSourceRoot>
+            <compileSourceRoot>${basedir}/src/test/resources/unit/exclude-roots/baseroot/src1</compileSourceRoot>
+            <compileSourceRoot>${basedir}/src/test/resources/unit/exclude-roots/baseroot/src2</compileSourceRoot>
+            <compileSourceRoot>${basedir}/src/test/resources/unit/exclude-roots/othersrc</compileSourceRoot>
+          </compileSourceRoots>
+
+          <excludeRoots>
+            <excludeRoot>${basedir}/src/test/resources/unit/exclude-roots/baseroot</excludeRoot>
+            <excludeRoot>${basedir}/src/test/resources/unit/exclude-roots/othersrc</excludeRoot>
+          </excludeRoots>
+        </configuration>
+        <dependencies>
+          <dependency>
+            <groupId>pmd</groupId>
+            <artifactId>pmd</artifactId>
+            <version>3.6</version>
+          </dependency>
+        </dependencies>
+      </plugin>
+    </plugins>
+  </build>
+  <reporting>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-jxr-plugin</artifactId>
+      </plugin>
+    </plugins>
+  </reporting>
+</project>

Added: maven/plugins/trunk/maven-pmd-plugin/src/test/resources/unit/exclude-roots/src/PMDViolationExample.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-pmd-plugin/src/test/resources/unit/exclude-roots/src/PMDViolationExample.java?rev=1729696&view=auto
==============================================================================
--- maven/plugins/trunk/maven-pmd-plugin/src/test/resources/unit/exclude-roots/src/PMDViolationExample.java (added)
+++ maven/plugins/trunk/maven-pmd-plugin/src/test/resources/unit/exclude-roots/src/PMDViolationExample.java Wed Feb 10 19:30:07 2016
@@ -0,0 +1,24 @@
+/*
+ * 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 PMDViolationExample {
+    public void forLoopShouldBeWhileLoop() {
+        for (;true;) true; // No Init or Update part, may as well be: while (true)
+    }
+}
\ No newline at end of file