[MPMD-234] Support incremental analysis cache

git-svn-id: https://svn.apache.org/repos/asf/maven/plugins/trunk@1793232 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/it/MPMD-234-analysis-cache/custom-analysis-cache/pom.xml b/src/it/MPMD-234-analysis-cache/custom-analysis-cache/pom.xml
new file mode 100644
index 0000000..3e7d1f7
--- /dev/null
+++ b/src/it/MPMD-234-analysis-cache/custom-analysis-cache/pom.xml
@@ -0,0 +1,50 @@
+<?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.plugin.pmd.its.MPMD-234-analysis-cache</groupId>
+  <artifactId>custom-analysis-cache</artifactId>
+  <version>1.0-SNAPSHOT</version>
+
+  <description>
+    Verify that an analysis cache file is produced at a custom location.
+  </description>
+
+  <properties>
+    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+  </properties>
+
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-pmd-plugin</artifactId>
+        <version>@project.version@</version>
+        <configuration>
+            <analysisCache>true</analysisCache>
+            <analysisCacheLocation>${project.build.directory}/custom-analysis-pmd.cache</analysisCacheLocation>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+</project>
diff --git a/src/it/MPMD-234-analysis-cache/custom-analysis-cache/src/main/java/com/mycompany/app/Hello.java b/src/it/MPMD-234-analysis-cache/custom-analysis-cache/src/main/java/com/mycompany/app/Hello.java
new file mode 100644
index 0000000..f7f504d
--- /dev/null
+++ b/src/it/MPMD-234-analysis-cache/custom-analysis-cache/src/main/java/com/mycompany/app/Hello.java
@@ -0,0 +1,28 @@
+package com.mycompany.app;
+
+/*
+ * 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
+{
+    public static void main( String[] args )
+    {
+        System.out.println( args[0] );
+    }
+}
diff --git a/src/it/MPMD-234-analysis-cache/default-analysis-cache/pom.xml b/src/it/MPMD-234-analysis-cache/default-analysis-cache/pom.xml
new file mode 100644
index 0000000..8671bad
--- /dev/null
+++ b/src/it/MPMD-234-analysis-cache/default-analysis-cache/pom.xml
@@ -0,0 +1,49 @@
+<?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.plugin.pmd.its.MPMD-234-analysis-cache</groupId>
+  <artifactId>default-analysis-cache</artifactId>
+  <version>1.0-SNAPSHOT</version>
+
+  <description>
+    Verify that an analysis cache file is produced at the default location.
+  </description>
+
+  <properties>
+    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+  </properties>
+
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-pmd-plugin</artifactId>
+        <version>@project.version@</version>
+        <configuration>
+            <analysisCache>true</analysisCache>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+</project>
diff --git a/src/it/MPMD-234-analysis-cache/default-analysis-cache/src/main/java/com/mycompany/app/Hello.java b/src/it/MPMD-234-analysis-cache/default-analysis-cache/src/main/java/com/mycompany/app/Hello.java
new file mode 100644
index 0000000..f7f504d
--- /dev/null
+++ b/src/it/MPMD-234-analysis-cache/default-analysis-cache/src/main/java/com/mycompany/app/Hello.java
@@ -0,0 +1,28 @@
+package com.mycompany.app;
+
+/*
+ * 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
+{
+    public static void main( String[] args )
+    {
+        System.out.println( args[0] );
+    }
+}
diff --git a/src/it/MPMD-234-analysis-cache/invoker.properties b/src/it/MPMD-234-analysis-cache/invoker.properties
new file mode 100644
index 0000000..268725a
--- /dev/null
+++ b/src/it/MPMD-234-analysis-cache/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 = clean verify pmd:pmd
diff --git a/src/it/MPMD-234-analysis-cache/no-analysis-cache/pom.xml b/src/it/MPMD-234-analysis-cache/no-analysis-cache/pom.xml
new file mode 100644
index 0000000..2ac62f6
--- /dev/null
+++ b/src/it/MPMD-234-analysis-cache/no-analysis-cache/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 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.plugin.pmd.its.MPMD-234-analysis-cache</groupId>
+  <artifactId>no-analysis-cache</artifactId>
+  <version>1.0-SNAPSHOT</version>
+
+  <description>
+    Verify that no analysis cache file is produced.
+  </description>
+
+  <properties>
+    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+  </properties>
+
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-pmd-plugin</artifactId>
+        <version>@project.version@</version>
+      </plugin>
+    </plugins>
+  </build>
+</project>
diff --git a/src/it/MPMD-234-analysis-cache/no-analysis-cache/src/main/java/com/mycompany/app/Hello.java b/src/it/MPMD-234-analysis-cache/no-analysis-cache/src/main/java/com/mycompany/app/Hello.java
new file mode 100644
index 0000000..f7f504d
--- /dev/null
+++ b/src/it/MPMD-234-analysis-cache/no-analysis-cache/src/main/java/com/mycompany/app/Hello.java
@@ -0,0 +1,28 @@
+package com.mycompany.app;
+
+/*
+ * 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
+{
+    public static void main( String[] args )
+    {
+        System.out.println( args[0] );
+    }
+}
diff --git a/src/it/MPMD-234-analysis-cache/pom.xml b/src/it/MPMD-234-analysis-cache/pom.xml
new file mode 100644
index 0000000..c4365f1
--- /dev/null
+++ b/src/it/MPMD-234-analysis-cache/pom.xml
@@ -0,0 +1,43 @@
+<?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.plugin.pmd.its</groupId>
+  <artifactId>MPMD-234-analysis-cache</artifactId>
+  <version>1.0-SNAPSHOT</version>
+  <packaging>pom</packaging>
+
+  <description>
+    Verify various analysis cache configurations.
+  </description>
+
+  <properties>
+    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+  </properties>
+  
+  <modules>
+    <module>no-analysis-cache</module>
+    <module>default-analysis-cache</module>
+    <module>custom-analysis-cache</module>
+  </modules>
+</project>
diff --git a/src/it/MPMD-234-analysis-cache/verify.groovy b/src/it/MPMD-234-analysis-cache/verify.groovy
new file mode 100644
index 0000000..2b94fb7
--- /dev/null
+++ b/src/it/MPMD-234-analysis-cache/verify.groovy
@@ -0,0 +1,27 @@
+/*
+ * 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 cacheFile = new File( basedir, 'no-analysis-cache/target/pmd/pmd.cache' )
+assert !cacheFile.exists()
+
+File defaultAnalysisCacheLocation = new File( basedir, 'default-analysis-cache/target/pmd/pmd.cache' )
+assert defaultAnalysisCacheLocation.exists()
+
+File customCacheLocation = new File( basedir, 'custom-analysis-cache/target/custom-analysis-pmd.cache' )
+assert customCacheLocation.exists()
diff --git a/src/main/java/org/apache/maven/plugin/pmd/PmdReport.java b/src/main/java/org/apache/maven/plugin/pmd/PmdReport.java
index 52a3635..2cc2a09 100644
--- a/src/main/java/org/apache/maven/plugin/pmd/PmdReport.java
+++ b/src/main/java/org/apache/maven/plugin/pmd/PmdReport.java
@@ -182,6 +182,29 @@
     private boolean skipPmdError;
 
     /**
+     * Enables the analysis cache, which speeds up PMD. This
+     * requires a cache file, that contains the results of the last
+     * PMD run. Thus the cache is only effective, if this file is
+     * not cleaned between runs.
+     *
+     * @since 3.8
+     */
+    @Parameter( property = "pmd.analysisCache", defaultValue = "false" )
+    private boolean analysisCache;
+
+    /**
+     * The location of the analysis cache, if it is enabled.
+     * This file contains the results of the last PMD run and must not be cleaned
+     * between consecutive PMD runs. Otherwise the cache is not in use.
+     * If the file doesn't exist, PMD executes as if there is no cache enabled and
+     * all files are analyzed. Otherwise only changed files will be analyzed again.
+     *
+     * @since 3.8
+     */
+    @Parameter( property = "pmd.analysisCacheLocation", defaultValue = "${project.build.directory}/pmd/pmd.cache" )
+    private String analysisCacheLocation;
+
+    /**
      * {@inheritDoc}
      */
     public String getName( Locale locale )
@@ -623,6 +646,12 @@
 
         configuration.setBenchmark( benchmark );
 
+        if ( analysisCache )
+        {
+            configuration.setAnalysisCacheLocation( analysisCacheLocation );
+            getLog().debug( "Using analysis cache location: " + analysisCacheLocation );
+        }
+
         return configuration;
     }
 
diff --git a/src/test/java/org/apache/maven/plugin/pmd/PmdReportTest.java b/src/test/java/org/apache/maven/plugin/pmd/PmdReportTest.java
index f3929d5..16773b5 100644
--- a/src/test/java/org/apache/maven/plugin/pmd/PmdReportTest.java
+++ b/src/test/java/org/apache/maven/plugin/pmd/PmdReportTest.java
@@ -91,6 +91,24 @@
         assertTrue( str.contains( "/xref/def/configuration/AppSample.html#L45" ) );
     }
 
+    public void testDefaultConfigurationWithAnalysisCache()
+            throws Exception
+    {
+        FileUtils.copyDirectoryStructure( new File( getBasedir(),
+                                                    "src/test/resources/unit/default-configuration/jxr-files" ),
+                                          new File( getBasedir(), "target/test/unit/pmd-with-analysis-cache-plugin-config/target/site" ) );
+
+        File testPom =
+            new File( getBasedir(),
+                      "src/test/resources/unit/default-configuration/pmd-with-analysis-cache-plugin-config.xml" );
+        PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom );
+        mojo.execute();
+
+        // check if the PMD analysis cache file has been generated
+        File cacheFile = new File( getBasedir(), "target/test/unit/pmd-with-analysis-cache-plugin-config/target/pmd/pmd.cache" );
+        assertTrue( FileUtils.fileExists( cacheFile.getAbsolutePath() ) );
+    }
+
     public void testJavascriptConfiguration()
         throws Exception
     {
diff --git a/src/test/resources/unit/default-configuration/pmd-with-analysis-cache-plugin-config.xml b/src/test/resources/unit/default-configuration/pmd-with-analysis-cache-plugin-config.xml
new file mode 100644
index 0000000..897a1b0
--- /dev/null
+++ b/src/test/resources/unit/default-configuration/pmd-with-analysis-cache-plugin-config.xml
@@ -0,0 +1,68 @@
+<!--
+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-with-analysis-cache</artifactId>
+  <packaging>jar</packaging>
+  <version>1.0-SNAPSHOT</version>
+  <inceptionYear>2017</inceptionYear>
+  <name>Maven PMD Plugin Default Configuration Test</name>
+  <url>http://maven.apache.org</url>
+  <build>
+    <finalName>pmd-with-analysis-cache-plugin-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/pmd-with-analysis-cache-plugin-config/target/site</outputDirectory>
+          <targetDirectory>${basedir}/target/test/unit/pmd-with-analysis-cache-plugin-config/target</targetDirectory>
+          <format>xml</format>
+          <linkXRef>true</linkXRef>
+          <xrefLocation>${basedir}/target/test/unit/pmd-with-analysis-cache-plugin-config/target/site/xref</xrefLocation>
+          <sourceEncoding>UTF-8</sourceEncoding>
+          <analysisCache>true</analysisCache>
+          <analysisCacheLocation>${basedir}/target/test/unit/pmd-with-analysis-cache-plugin-config/target/pmd/pmd.cache</analysisCacheLocation>
+
+          <compileSourceRoots>
+            <compileSourceRoot>${basedir}/src/test/resources/unit/default-configuration/</compileSourceRoot>
+          </compileSourceRoots>
+        </configuration>
+        <dependencies>
+          <dependency>
+            <groupId>pmd</groupId>
+            <artifactId>pmd</artifactId>
+            <version>3.8</version>
+          </dependency>
+        </dependencies>
+      </plugin>
+    </plugins>
+  </build>
+  <reporting>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-jxr-plugin</artifactId>
+      </plugin>
+    </plugins>
+  </reporting>
+</project>