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.apache.maven.plugins.annotations.Execute;
34  import org.apache.maven.plugins.annotations.LifecyclePhase;
35  import org.apache.maven.plugins.annotations.Mojo;
36  import org.apache.maven.plugins.annotations.Parameter;
37  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
38  
39  /**
40   * Fail the build if there were any CPD violations in the source code.
41   *
42   * @version $Id: CpdViolationCheckMojo.html 1011460 2017-05-01 07:24:19Z adangel $
43   * @since 2.0
44   */
45  @Mojo( name = "cpd-check", defaultPhase = LifecyclePhase.VERIFY, threadSafe = true )
46  @Execute( goal = "cpd" )
47  public class CpdViolationCheckMojo
48      extends AbstractPmdViolationCheckMojo<Duplication>
49  {
50      /**
51       * Default constructor. Initializes with the correct {@link ExcludeDuplicationsFromFile}.
52       */
53      public CpdViolationCheckMojo()
54      {
55          super( new ExcludeDuplicationsFromFile() );
56      }
57  
58      /**
59       * Skip the CPD violation checks. Most useful on the command line via "-Dcpd.skip=true".
60       */
61      @Parameter( property = "cpd.skip", defaultValue = "false" )
62      private boolean skip;
63  
64      /**
65       * Whether to fail the build if the validation check fails.
66       *
67       * @since 3.0
68       */
69      @Parameter( property = "cpd.failOnViolation", defaultValue = "true", required = true )
70      protected boolean failOnViolation;
71  
72      /**
73       * {@inheritDoc}
74       */
75      public void execute()
76          throws MojoExecutionException, MojoFailureException
77      {
78          if ( !skip )
79          {
80              executeCheck( "cpd.xml", "duplication", "CPD duplication", 10 );
81          }
82      }
83  
84      /**
85       * {@inheritDoc}
86       */
87      @Override
88      protected void printError( Duplication item, String severity )
89      {
90          int lines = item.getLines();
91  
92          StringBuilder buff = new StringBuilder( 100 );
93          buff.append( "CPD " ).append( severity ).append( ": Found " );
94          buff.append( lines ).append( " lines of duplicated code at locations:" );
95          this.getLog().info( buff.toString() );
96  
97          for ( CpdFile file : item.getFiles() )
98          {
99              buff.setLength( 0 );
100             buff.append( "    " );
101             buff.append( file.getPath() );
102             buff.append( " line " ).append( file.getLine() );
103             this.getLog().info( buff.toString() );
104         }
105 
106         this.getLog().debug( "CPD " + severity + ": Code Fragment " );
107         this.getLog().debug( item.getCodefragment() );
108     }
109 
110     /**
111      * {@inheritDoc}
112      */
113     @Override
114     protected List<Duplication> getErrorDetails( File cpdFile )
115         throws XmlPullParserException, IOException
116     {
117         CpdXpp3Reader reader = new CpdXpp3Reader();
118         CpdErrorDetail details = reader.read( new FileReader( cpdFile ), false );
119         return details.getDuplications();
120     }
121 
122     @Override
123     protected int getPriority( Duplication errorDetail )
124     {
125         return 0;
126     }
127 
128     @Override
129     protected ViolationDetails<Duplication> newViolationDetailsInstance()
130     {
131         return new ViolationDetails<>();
132     }
133 
134     @Override
135     public boolean isFailOnViolation()
136     {
137         return failOnViolation;
138     }
139 }