1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
41
42
43
44 @Mojo(name = "cpd-check", defaultPhase = LifecyclePhase.VERIFY, threadSafe = true)
45 @Execute(goal = "cpd")
46 public class CpdViolationCheckMojo extends AbstractPmdViolationCheckMojo<Duplication> {
47
48
49
50 public CpdViolationCheckMojo() {
51 super(new ExcludeDuplicationsFromFile());
52 }
53
54
55
56
57 @Parameter(property = "cpd.skip", defaultValue = "false")
58 private boolean skip;
59
60
61
62
63
64
65 @Parameter(property = "cpd.failOnViolation", defaultValue = "true", required = true)
66 protected boolean failOnViolation;
67
68
69
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
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
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 }