View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.pmd;
20  
21  import java.io.File;
22  import java.io.FileInputStream;
23  import java.io.IOException;
24  import java.io.InputStream;
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.annotations.Execute;
30  import org.apache.maven.plugins.annotations.LifecyclePhase;
31  import org.apache.maven.plugins.annotations.Mojo;
32  import org.apache.maven.plugins.annotations.Parameter;
33  import org.apache.maven.plugins.pmd.model.CpdErrorDetail;
34  import org.apache.maven.plugins.pmd.model.CpdFile;
35  import org.apache.maven.plugins.pmd.model.Duplication;
36  import org.apache.maven.plugins.pmd.model.io.xpp3.CpdXpp3Reader;
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   * @since 2.0
43   */
44  @Mojo(name = "cpd-check", defaultPhase = LifecyclePhase.VERIFY, threadSafe = true)
45  @Execute(goal = "cpd")
46  public class CpdViolationCheckMojo extends AbstractPmdViolationCheckMojo<Duplication> {
47      /**
48       * Default constructor. Initializes with the correct {@link ExcludeDuplicationsFromFile}.
49       */
50      public CpdViolationCheckMojo() {
51          super(new ExcludeDuplicationsFromFile());
52      }
53  
54      /**
55       * Skip the CPD violation checks. Most useful on the command line via "-Dcpd.skip=true".
56       */
57      @Parameter(property = "cpd.skip", defaultValue = "false")
58      private boolean skip;
59  
60      /**
61       * Whether to fail the build if the validation check fails.
62       *
63       * @since 3.0
64       */
65      @Parameter(property = "cpd.failOnViolation", defaultValue = "true", required = true)
66      protected boolean failOnViolation;
67  
68      /**
69       * {@inheritDoc}
70       */
71      public void execute() throws MojoExecutionException, MojoFailureException {
72          if (skip) {
73              getLog().info("Skipping CPD execution");
74              return;
75          }
76  
77          executeCheck("cpd.xml", "CPD", "duplication", 10);
78      }
79  
80      /**
81       * {@inheritDoc}
82       */
83      @Override
84      protected void printError(Duplication item, String severity) {
85          int lines = item.getLines();
86  
87          StringBuilder buff = new StringBuilder(100);
88          buff.append("CPD ").append(severity).append(": Found ");
89          buff.append(lines).append(" lines of duplicated code at locations:");
90          this.getLog().warn(buff.toString());
91  
92          for (CpdFile file : item.getFiles()) {
93              buff.setLength(0);
94              buff.append("    ");
95              buff.append(file.getPath());
96              buff.append(" line ").append(file.getLine());
97              this.getLog().warn(buff.toString());
98          }
99  
100         this.getLog().debug("CPD " + severity + ": Code Fragment ");
101         this.getLog().debug(item.getCodefragment());
102     }
103 
104     /**
105      * {@inheritDoc}
106      */
107     @Override
108     protected List<Duplication> getErrorDetails(File cpdFile) throws XmlPullParserException, IOException {
109         try (InputStream in = new FileInputStream(cpdFile)) {
110             CpdXpp3Reader reader = new CpdXpp3Reader();
111             CpdErrorDetail details = reader.read(in, false);
112             return details.getDuplications();
113         }
114     }
115 
116     @Override
117     protected int getPriority(Duplication errorDetail) {
118         return 0;
119     }
120 
121     @Override
122     protected ViolationDetails<Duplication> newViolationDetailsInstance() {
123         return new ViolationDetails<>();
124     }
125 
126     @Override
127     public boolean isFailOnViolation() {
128         return failOnViolation;
129     }
130 }