View Javadoc
1   package org.apache.maven.plugins.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.plugins.pmd.model.CpdErrorDetail;
30  import org.apache.maven.plugins.pmd.model.CpdFile;
31  import org.apache.maven.plugins.pmd.model.Duplication;
32  import org.apache.maven.plugins.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   * Fails the build if there were any CPD violations in the source code.
41   *
42   * @version $Id$
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              getLog().info( "Skipping CPD execution" );
81              return;
82          }
83  
84          executeCheck( "cpd.xml", "duplication", "CPD duplication", 10 );
85      }
86  
87      /**
88       * {@inheritDoc}
89       */
90      @Override
91      protected void printError( Duplication item, String severity )
92      {
93          int lines = item.getLines();
94  
95          StringBuilder buff = new StringBuilder( 100 );
96          buff.append( "CPD " ).append( severity ).append( ": Found " );
97          buff.append( lines ).append( " lines of duplicated code at locations:" );
98          this.getLog().info( buff.toString() );
99  
100         for ( CpdFile file : item.getFiles() )
101         {
102             buff.setLength( 0 );
103             buff.append( "    " );
104             buff.append( file.getPath() );
105             buff.append( " line " ).append( file.getLine() );
106             this.getLog().info( buff.toString() );
107         }
108 
109         this.getLog().debug( "CPD " + severity + ": Code Fragment " );
110         this.getLog().debug( item.getCodefragment() );
111     }
112 
113     /**
114      * {@inheritDoc}
115      */
116     @Override
117     protected List<Duplication> getErrorDetails( File cpdFile )
118         throws XmlPullParserException, IOException
119     {
120         try ( FileReader fileReader = new FileReader( cpdFile ) )
121         {
122             CpdXpp3Reader reader = new CpdXpp3Reader();
123             CpdErrorDetail details = reader.read( fileReader, false );
124             return details.getDuplications();
125         }
126     }
127 
128     @Override
129     protected int getPriority( Duplication errorDetail )
130     {
131         return 0;
132     }
133 
134     @Override
135     protected ViolationDetails<Duplication> newViolationDetailsInstance()
136     {
137         return new ViolationDetails<>();
138     }
139 
140     @Override
141     public boolean isFailOnViolation()
142     {
143         return failOnViolation;
144     }
145 }