You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by br...@apache.org on 2006/04/02 04:55:26 UTC

svn commit: r390772 - in /maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd: AbstractPmdViolationCheckMojo.java CpdReport.java CpdViolationCheckMojo.java PmdViolationCheckMojo.java

Author: brett
Date: Sat Apr  1 18:55:25 2006
New Revision: 390772

URL: http://svn.apache.org/viewcvs?rev=390772&view=rev
Log:
[MPMD-14] add a cpd-check goal, and factor out the common elements with the check goal

Added:
    maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/AbstractPmdViolationCheckMojo.java   (with props)
    maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/CpdViolationCheckMojo.java   (with props)
Modified:
    maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/CpdReport.java
    maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/PmdViolationCheckMojo.java

Added: maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/AbstractPmdViolationCheckMojo.java
URL: http://svn.apache.org/viewcvs/maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/AbstractPmdViolationCheckMojo.java?rev=390772&view=auto
==============================================================================
--- maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/AbstractPmdViolationCheckMojo.java (added)
+++ maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/AbstractPmdViolationCheckMojo.java Sat Apr  1 18:55:25 2006
@@ -0,0 +1,91 @@
+package org.apache.maven.plugin.pmd;
+
+import org.apache.maven.plugin.AbstractMojo;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.plugin.MojoFailureException;
+import org.codehaus.plexus.util.xml.pull.MXParser;
+import org.codehaus.plexus.util.xml.pull.XmlPullParser;
+import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
+
+/**
+ * TODO: Description.
+ *
+ * @author <a href="mailto:brett@apache.org">Brett Porter</a>
+ */
+public abstract class AbstractPmdViolationCheckMojo
+    extends AbstractMojo
+{
+    /**
+     * @parameter expression="${project.build.directory}"
+     * @required
+     */
+    private File targetDirectory;
+
+    /**
+     * Fail on violation?
+     *
+     * @parameter expression="${failOnViolation}" default-value="true"
+     * @required
+     */
+    private boolean failOnViolation;
+
+    protected void executeCheck( String filename, String tagName )
+        throws MojoFailureException, MojoExecutionException
+    {
+        File outputFile = new File( targetDirectory, filename );
+        if ( outputFile.exists() )
+        {
+            try
+            {
+                XmlPullParser xpp = new MXParser();
+                FileReader freader = new FileReader( outputFile );
+                BufferedReader breader = new BufferedReader( freader );
+                xpp.setInput( breader );
+
+                int violations = countViolations( xpp, tagName );
+                if ( violations > 0 && failOnViolation )
+                {
+                    throw new MojoFailureException(
+                        "You have " + violations + " violation" + ( violations > 1 ? "s" : "" ) + "." );
+                }
+            }
+            catch ( IOException e )
+            {
+                throw new MojoExecutionException( "Unable to read PMD results xml: " + outputFile.getAbsolutePath(),
+                                                  e );
+            }
+            catch ( XmlPullParserException e )
+            {
+                throw new MojoExecutionException( "Unable to read PMD results xml: " + outputFile.getAbsolutePath(),
+                                                  e );
+            }
+        }
+        else
+        {
+            throw new MojoFailureException( "Unable to perform check, " + "unable to find " + outputFile );
+        }
+    }
+
+    private int countViolations( XmlPullParser xpp, String tagName )
+        throws XmlPullParserException, IOException
+    {
+        int count = 0;
+
+        int eventType = xpp.getEventType();
+        while ( eventType != XmlPullParser.END_DOCUMENT )
+        {
+            if ( eventType == XmlPullParser.START_TAG && tagName.equals( xpp.getName() ) )
+            {
+                count++;
+            }
+            eventType = xpp.next();
+        }
+
+        return count;
+    }
+}

Propchange: maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/AbstractPmdViolationCheckMojo.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/AbstractPmdViolationCheckMojo.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Modified: maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/CpdReport.java
URL: http://svn.apache.org/viewcvs/maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/CpdReport.java?rev=390772&r1=390771&r2=390772&view=diff
==============================================================================
--- maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/CpdReport.java (original)
+++ maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/CpdReport.java Sat Apr  1 18:55:25 2006
@@ -95,7 +95,7 @@
                 String buffer = r.render( cpd.getMatches() );
                 try
                 {
-                    Writer writer = new FileWriter( new File( this.getReportOutputDirectory(), "cpd." + format ) );
+                    Writer writer = new FileWriter( new File( targetDirectory, "cpd." + format ) );
                     writer.write( buffer, 0, buffer.length() );
                     writer.close();
                 }

Added: maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/CpdViolationCheckMojo.java
URL: http://svn.apache.org/viewcvs/maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/CpdViolationCheckMojo.java?rev=390772&view=auto
==============================================================================
--- maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/CpdViolationCheckMojo.java (added)
+++ maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/CpdViolationCheckMojo.java Sat Apr  1 18:55:25 2006
@@ -0,0 +1,37 @@
+package org.apache.maven.plugin.pmd;
+
+/*
+ * Copyright 2001-2006 The Apache Software Foundation.
+ *
+ * Licensed 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.plugin.MojoExecutionException;
+import org.apache.maven.plugin.MojoFailureException;
+
+/**
+ * Perform a violation check against the last pmd run to see if there are any violations.
+ *
+ * @goal cpd-check
+ * @phase verify
+ * @execute goal="cpd"
+ */
+public class CpdViolationCheckMojo
+    extends AbstractPmdViolationCheckMojo
+{
+    public void execute()
+        throws MojoExecutionException, MojoFailureException
+    {
+        executeCheck( "cpd.xml", "duplication" );
+    }
+}

Propchange: maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/CpdViolationCheckMojo.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/CpdViolationCheckMojo.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Modified: maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/PmdViolationCheckMojo.java
URL: http://svn.apache.org/viewcvs/maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/PmdViolationCheckMojo.java?rev=390772&r1=390771&r2=390772&view=diff
==============================================================================
--- maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/PmdViolationCheckMojo.java (original)
+++ maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/PmdViolationCheckMojo.java Sat Apr  1 18:55:25 2006
@@ -16,17 +16,8 @@
  * limitations under the License.
  */
 
-import org.apache.maven.plugin.AbstractMojo;
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.plugin.MojoFailureException;
-import org.codehaus.plexus.util.xml.pull.MXParser;
-import org.codehaus.plexus.util.xml.pull.XmlPullParser;
-import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
-
-import java.io.BufferedReader;
-import java.io.File;
-import java.io.FileReader;
-import java.io.IOException;
 
 /**
  * Perform a violation check against the last pmd run to see if there are any violations.
@@ -36,77 +27,11 @@
  * @execute goal="pmd"
  */
 public class PmdViolationCheckMojo
-    extends AbstractMojo
+    extends AbstractPmdViolationCheckMojo
 {
-    /**
-     * @parameter expression="${project.build.directory}"
-     * @required
-     */
-    private File targetDirectory;
-
-    /**
-     * Fail on violation?
-     *
-     * @parameter expression="${failOnViolation}" default-value="true"
-     * @required
-     */
-    private boolean failOnViolation;
-
-    /**
-     * Number of violations
-     */
     public void execute()
         throws MojoExecutionException, MojoFailureException
     {
-        File outputFile = new File( targetDirectory, "pmd.xml" );
-        if ( outputFile.exists() )
-        {
-            try
-            {
-                XmlPullParser xpp = new MXParser();
-                FileReader freader = new FileReader( outputFile );
-                BufferedReader breader = new BufferedReader( freader );
-                xpp.setInput( breader );
-
-                int violations = countViolations( xpp );
-                if ( violations > 0 && failOnViolation )
-                {
-                    throw new MojoFailureException(
-                        "You have " + violations + " PMD violation" + ( violations > 1 ? "s" : "" ) + "." );
-                }
-            }
-            catch ( IOException e )
-            {
-                throw new MojoExecutionException( "Unable to read PMD results xml: " + outputFile.getAbsolutePath(),
-                                                  e );
-            }
-            catch ( XmlPullParserException e )
-            {
-                throw new MojoExecutionException( "Unable to read PMD results xml: " + outputFile.getAbsolutePath(),
-                                                  e );
-            }
-        }
-        else
-        {
-            throw new MojoFailureException( "Unable to perform pmd:check, " + "unable to find " + outputFile );
-        }
-    }
-
-    private int countViolations( XmlPullParser xpp )
-        throws XmlPullParserException, IOException
-    {
-        int count = 0;
-
-        int eventType = xpp.getEventType();
-        while ( eventType != XmlPullParser.END_DOCUMENT )
-        {
-            if ( eventType == XmlPullParser.START_TAG && "violation".equals( xpp.getName() ) )
-            {
-                count++;
-            }
-            eventType = xpp.next();
-        }
-
-        return count;
+        executeCheck( "pmd.xml", "violation" );
     }
 }