1 package org.apache.maven.plugins.pmd;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
41
42
43
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
52
53 public CpdViolationCheckMojo()
54 {
55 super( new ExcludeDuplicationsFromFile() );
56 }
57
58
59
60
61 @Parameter( property = "cpd.skip", defaultValue = "false" )
62 private boolean skip;
63
64
65
66
67
68
69 @Parameter( property = "cpd.failOnViolation", defaultValue = "true", required = true )
70 protected boolean failOnViolation;
71
72
73
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
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
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 }