24
24
import java .io .IOException ;
25
25
import java .io .ObjectInputStream ;
26
26
import java .io .ObjectOutputStream ;
27
- import java .io .OutputStreamWriter ;
28
- import java .io .Writer ;
29
27
import java .nio .charset .Charset ;
30
28
import java .util .Objects ;
31
- import java .util .function .Predicate ;
32
29
33
30
import net .sourceforge .pmd .cpd .CPDConfiguration ;
34
- import net .sourceforge .pmd .cpd .CPDReport ;
35
31
import net .sourceforge .pmd .cpd .CPDReportRenderer ;
36
32
import net .sourceforge .pmd .cpd .CSVRenderer ;
37
33
import net .sourceforge .pmd .cpd .CpdAnalysis ;
38
- import net .sourceforge .pmd .cpd .Match ;
39
34
import net .sourceforge .pmd .cpd .SimpleRenderer ;
40
35
import net .sourceforge .pmd .cpd .XMLRenderer ;
41
36
import net .sourceforge .pmd .lang .Language ;
42
37
import org .apache .maven .plugin .MojoExecutionException ;
43
38
import org .apache .maven .plugins .pmd .ExcludeDuplicationsFromFile ;
44
39
import org .apache .maven .reporting .MavenReportException ;
45
- import org .codehaus .plexus .util .FileUtils ;
46
40
import org .slf4j .Logger ;
47
41
import org .slf4j .LoggerFactory ;
48
42
@@ -152,6 +146,8 @@ private CpdResult run() throws MavenReportException {
152
146
cpdConfiguration .setIgnoreAnnotations (request .isIgnoreAnnotations ());
153
147
cpdConfiguration .setIgnoreLiterals (request .isIgnoreLiterals ());
154
148
cpdConfiguration .setIgnoreIdentifiers (request .isIgnoreIdentifiers ());
149
+ // we are not running CPD through CLI and deal with any errors during analysis on our own
150
+ cpdConfiguration .setSkipLexicalErrors (true );
155
151
156
152
String languageId = request .getLanguage ();
157
153
if ("javascript" .equals (languageId )) {
@@ -167,64 +163,24 @@ private CpdResult run() throws MavenReportException {
167
163
request .getFiles ().forEach (f -> cpdConfiguration .addInputPath (f .toPath ()));
168
164
169
165
LOG .debug ("Executing CPD..." );
170
-
171
- // always create XML format. we need to output it even if the file list is empty or we have no duplications
172
- // so the "check" goals can check for violations
173
166
try (CpdAnalysis cpd = CpdAnalysis .create (cpdConfiguration )) {
174
- cpd .performAnalysis (report -> {
175
- try {
176
- writeXmlReport (report );
177
-
178
- // html format is handled by maven site report, xml format has already been rendered
179
- String format = request .getFormat ();
180
- if (!"html" .equals (format ) && !"xml" .equals (format )) {
181
- writeFormattedReport (report );
182
- }
183
- } catch (MavenReportException e ) {
184
- LOG .error ("Error while writing CPD report" , e );
185
- }
186
- });
167
+ CpdReportConsumer reportConsumer = new CpdReportConsumer (request , excludeDuplicationsFromFile );
168
+ cpd .performAnalysis (reportConsumer );
187
169
} catch (IOException e ) {
188
- LOG . error ("Error while executing CPD" , e );
170
+ throw new MavenReportException ("Error while executing CPD" , e );
189
171
}
190
172
LOG .debug ("CPD finished." );
191
173
192
- return new CpdResult (new File (request .getTargetDirectory (), "cpd.xml" ), request .getOutputEncoding ());
193
- }
194
-
195
- private void writeXmlReport (CPDReport cpd ) throws MavenReportException {
196
- File targetFile = writeReport (cpd , new XMLRenderer (request .getOutputEncoding ()), "xml" );
197
- if (request .isIncludeXmlInSite ()) {
198
- File siteDir = new File (request .getReportOutputDirectory ());
199
- siteDir .mkdirs ();
200
- try {
201
- FileUtils .copyFile (targetFile , new File (siteDir , "cpd.xml" ));
202
- } catch (IOException e ) {
203
- throw new MavenReportException (e .getMessage (), e );
204
- }
205
- }
206
- }
207
-
208
- private File writeReport (CPDReport cpd , CPDReportRenderer r , String extension ) throws MavenReportException {
209
- if (r == null ) {
210
- return null ;
174
+ // in constrast to pmd goal, we don't have a parameter for cpd like "skipPmdError" - if there
175
+ // are any errors during CPD analysis, the maven build fails.
176
+ int cpdErrors = cpdConfiguration .getReporter ().numErrors ();
177
+ if (cpdErrors == 1 ) {
178
+ throw new MavenReportException ("There was 1 error while executing CPD" );
179
+ } else if (cpdErrors > 1 ) {
180
+ throw new MavenReportException ("There were " + cpdErrors + " errors while executing CPD" );
211
181
}
212
182
213
- File targetDir = new File (request .getTargetDirectory ());
214
- targetDir .mkdirs ();
215
- File targetFile = new File (targetDir , "cpd." + extension );
216
- try (Writer writer = new OutputStreamWriter (new FileOutputStream (targetFile ), request .getOutputEncoding ())) {
217
- r .render (cpd .filterMatches (filterMatches ()), writer );
218
- writer .flush ();
219
- } catch (IOException ioe ) {
220
- throw new MavenReportException (ioe .getMessage (), ioe );
221
- }
222
- return targetFile ;
223
- }
224
-
225
- private void writeFormattedReport (CPDReport cpd ) throws MavenReportException {
226
- CPDReportRenderer r = createRenderer (request .getFormat (), request .getOutputEncoding ());
227
- writeReport (cpd , r , request .getFormat ());
183
+ return new CpdResult (new File (request .getTargetDirectory (), "cpd.xml" ), request .getOutputEncoding ());
228
184
}
229
185
230
186
/**
@@ -255,18 +211,4 @@ public static CPDReportRenderer createRenderer(String format, String outputEncod
255
211
256
212
return renderer ;
257
213
}
258
-
259
- private Predicate <Match > filterMatches () {
260
- return (Match match ) -> {
261
- LOG .debug ("Filtering duplications. Using " + excludeDuplicationsFromFile .countExclusions ()
262
- + " configured exclusions." );
263
-
264
- if (excludeDuplicationsFromFile .isExcludedFromFailure (match )) {
265
- LOG .debug ("Excluded " + match + " duplications." );
266
- return false ;
267
- } else {
268
- return true ;
269
- }
270
- };
271
- }
272
214
}
0 commit comments