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