View Javadoc

1   package org.apache.maven.plugin.pmd;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.io.FileReader;
24  import java.io.IOException;
25  import java.util.List;
26  
27  import org.apache.maven.plugin.MojoExecutionException;
28  import org.apache.maven.plugin.MojoFailureException;
29  import org.apache.maven.plugin.pmd.model.CpdErrorDetail;
30  import org.apache.maven.plugin.pmd.model.CpdFile;
31  import org.apache.maven.plugin.pmd.model.Duplication;
32  import org.apache.maven.plugin.pmd.model.io.xpp3.CpdXpp3Reader;
33  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
34  
35  /**
36   * Fail the build if there were any CPD violations in the source code.
37   *
38   * @since 2.0
39   * @version $Id: CpdViolationCheckMojo.html 816696 2012-05-08 15:19:20Z hboutemy $
40   * @goal cpd-check
41   * @phase verify
42   * @execute goal="cpd"
43   * @threadSafe
44   */
45  public class CpdViolationCheckMojo
46      extends AbstractPmdViolationCheckMojo<Duplication>
47  {
48  
49      /**
50       * Skip the CPD violation checks.  Most useful on the command line
51       * via "-Dcpd.skip=true".
52       *
53       * @parameter expression="${cpd.skip}" default-value="false"
54       */
55      private boolean skip;
56  
57      /** {@inheritDoc} */
58      public void execute()
59          throws MojoExecutionException, MojoFailureException
60      {
61          if ( !skip )
62          {
63              executeCheck( "cpd.xml", "duplication", "CPD duplication", 10 );
64          }
65      }
66  
67      /** {@inheritDoc} */
68      protected void printError( Duplication item, String severity )
69      {
70          int lines = item.getLines();
71  
72  
73          StringBuffer buff = new StringBuffer( 100 );
74          buff.append( "CPD " + severity + ": Found " );
75          buff.append( lines ).append( " lines of duplicated code at locations:" );
76          this.getLog().info( buff.toString() );
77  
78          
79          for( CpdFile file : item.getFiles() )
80          {
81              buff.setLength( 0 );
82              buff.append( "    " );
83              buff.append( file.getPath() );
84              buff.append( " line " ).append( file.getLine() );
85              this.getLog().info( buff.toString() );
86          }
87  
88          this.getLog().debug( "CPD " + severity + ": Code Fragment " );
89          this.getLog().debug( item.getCodefragment() );
90      }
91  
92      /** {@inheritDoc} */
93      protected List<Duplication> getErrorDetails( File cpdFile )
94          throws XmlPullParserException, IOException
95      {
96          CpdXpp3Reader reader = new CpdXpp3Reader();
97          CpdErrorDetail details = reader.read( new FileReader( cpdFile ), false );
98          return details.getDuplications();
99      }
100     
101     @Override
102     protected int getPriority( Duplication errorDetail )
103     {
104         return 0;
105     }
106     
107     @Override
108     protected ViolationDetails<Duplication> newViolationDetailsInstance()
109     {
110         return new ViolationDetails<Duplication>();
111     }
112 }