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 2018/06/23 15:52:16 UTC

[maven-pmd-plugin] 02/02: [MPMD-268] - Missing warnings about deprecated rules

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

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

commit a52aeadaea217ace4cb39679a9fa8715ad7af2db
Author: Andreas Dangel <ad...@apache.org>
AuthorDate: Sat Jun 23 17:50:15 2018 +0200

    [MPMD-268] - Missing warnings about deprecated rules
---
 .../MPMD-268-deprecated-rules/invoker.properties   | 20 ++++++++
 src/it/MPMD-268-deprecated-rules/pom.xml           | 60 ++++++++++++++++++++++
 src/it/MPMD-268-deprecated-rules/ruleset.xml       | 32 ++++++++++++
 .../src/main/java/sample/Sample.java               | 24 +++++++++
 src/it/MPMD-268-deprecated-rules/verify.groovy     | 22 ++++++++
 .../org/apache/maven/plugins/pmd/PmdReport.java    | 17 +++++-
 6 files changed, 173 insertions(+), 2 deletions(-)

diff --git a/src/it/MPMD-268-deprecated-rules/invoker.properties b/src/it/MPMD-268-deprecated-rules/invoker.properties
new file mode 100644
index 0000000..5ad0800
--- /dev/null
+++ b/src/it/MPMD-268-deprecated-rules/invoker.properties
@@ -0,0 +1,20 @@
+# 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
+invoker.maven.version = 3.1.0+
+invoker.debug = false
diff --git a/src/it/MPMD-268-deprecated-rules/pom.xml b/src/it/MPMD-268-deprecated-rules/pom.xml
new file mode 100644
index 0000000..1653a46
--- /dev/null
+++ b/src/it/MPMD-268-deprecated-rules/pom.xml
@@ -0,0 +1,60 @@
+<?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.pmd.its</groupId>
+  <artifactId>MPMD-268-deprecated-rules</artifactId>
+  <version>1.0-SNAPSHOT</version>
+  <packaging>jar</packaging>
+
+  <description>
+    Verify PMD logs deprecated rules
+  </description>
+
+  <properties>
+    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+  </properties>
+
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>@project.groupId@</groupId>
+        <artifactId>@project.artifactId@</artifactId>
+        <version>@project.version@</version>
+        <configuration>
+            <rulesets>
+                <ruleset>ruleset.xml</ruleset>
+            </rulesets>
+        </configuration>
+        <executions>
+          <execution>
+            <phase>verify</phase>
+            <goals>
+              <goal>check</goal>
+            </goals>
+          </execution>
+        </executions>
+      </plugin>
+    </plugins>
+  </build>
+</project>
diff --git a/src/it/MPMD-268-deprecated-rules/ruleset.xml b/src/it/MPMD-268-deprecated-rules/ruleset.xml
new file mode 100644
index 0000000..2b4f05b
--- /dev/null
+++ b/src/it/MPMD-268-deprecated-rules/ruleset.xml
@@ -0,0 +1,32 @@
+<?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-268</description>
+
+    <!-- Explicitly marking this rule a deprecated, which should issue a warning when executing PMD -->
+    <rule ref="category/java/codestyle.xml/ControlStatementBraces"
+        deprecated="true" />
+
+</ruleset>
diff --git a/src/it/MPMD-268-deprecated-rules/src/main/java/sample/Sample.java b/src/it/MPMD-268-deprecated-rules/src/main/java/sample/Sample.java
new file mode 100644
index 0000000..af4c55b
--- /dev/null
+++ b/src/it/MPMD-268-deprecated-rules/src/main/java/sample/Sample.java
@@ -0,0 +1,24 @@
+package sample;
+
+/*
+ * 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 Sample
+{
+}
\ No newline at end of file
diff --git a/src/it/MPMD-268-deprecated-rules/verify.groovy b/src/it/MPMD-268-deprecated-rules/verify.groovy
new file mode 100644
index 0000000..009a5dd
--- /dev/null
+++ b/src/it/MPMD-268-deprecated-rules/verify.groovy
@@ -0,0 +1,22 @@
+/*
+ * 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 buildLog = new File( basedir, 'build.log' )
+assert buildLog.exists()
+assert buildLog.text.contains( "[WARNING] Use Rule name category/java/codestyle.xml/ControlStatementBraces instead of the deprecated" )
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 84d56ff..fa354a1 100644
--- a/src/main/java/org/apache/maven/plugins/pmd/PmdReport.java
+++ b/src/main/java/org/apache/maven/plugins/pmd/PmdReport.java
@@ -55,6 +55,7 @@ import net.sourceforge.pmd.Report;
 import net.sourceforge.pmd.RuleContext;
 import net.sourceforge.pmd.RulePriority;
 import net.sourceforge.pmd.RuleSetFactory;
+import net.sourceforge.pmd.RuleSetNotFoundException;
 import net.sourceforge.pmd.RuleSetReferenceId;
 import net.sourceforge.pmd.RuleViolation;
 import net.sourceforge.pmd.benchmark.Benchmarker;
@@ -228,6 +229,7 @@ public class PmdReport
     /**
      * {@inheritDoc}
      */
+    @Override
     public String getName( Locale locale )
     {
         return getBundle( locale ).getString( "report.pmd.name" );
@@ -236,6 +238,7 @@ public class PmdReport
     /**
      * {@inheritDoc}
      */
+    @Override
     public String getDescription( Locale locale )
     {
         return getBundle( locale ).getString( "report.pmd.description" );
@@ -502,12 +505,21 @@ public class PmdReport
             throws MavenReportException
     {
         RuleSetFactory ruleSetFactory = new RuleSetFactory( new ResourceLoader(),
-                RulePriority.valueOf( this.minimumPriority ), false, true );
-        RuleContext ruleContext = new RuleContext();
+                RulePriority.valueOf( this.minimumPriority ), true, true );
+        try
+        {
+            // load the ruleset once to log out any deprecated rules as warnings
+            ruleSetFactory.createRuleSets( pmdConfiguration.getRuleSets() );
+        }
+        catch ( RuleSetNotFoundException e1 )
+        {
+            throw new MavenReportException( "The ruleset could not be loaded", e1 );
+        }
 
         try
         {
             getLog().debug( "Executing PMD..." );
+            RuleContext ruleContext = new RuleContext();
             PMD.processFiles( pmdConfiguration, ruleSetFactory, dataSources, ruleContext,
                               Arrays.<Renderer>asList( renderer ) );
 
@@ -700,6 +712,7 @@ public class PmdReport
     /**
      * {@inheritDoc}
      */
+    @Override
     public String getOutputName()
     {
         return "pmd";