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.ArrayList;
26 import java.util.List;
27
28 import org.apache.maven.plugin.MojoExecutionException;
29 import org.apache.maven.plugin.MojoFailureException;
30 import org.apache.maven.plugins.pmd.model.PmdErrorDetail;
31 import org.apache.maven.plugins.pmd.model.PmdFile;
32 import org.apache.maven.plugins.pmd.model.Violation;
33 import org.apache.maven.plugins.pmd.model.io.xpp3.PmdXpp3Reader;
34 import org.apache.maven.plugins.annotations.Execute;
35 import org.apache.maven.plugins.annotations.LifecyclePhase;
36 import org.apache.maven.plugins.annotations.Mojo;
37 import org.apache.maven.plugins.annotations.Parameter;
38 import org.codehaus.plexus.util.StringUtils;
39 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
40
41
42
43
44
45
46
47 @Mojo( name = "check", defaultPhase = LifecyclePhase.VERIFY, threadSafe = true )
48 @Execute( goal = "pmd" )
49 public class PmdViolationCheckMojo
50 extends AbstractPmdViolationCheckMojo<Violation>
51 {
52
53
54
55 public PmdViolationCheckMojo()
56 {
57 super( new ExcludeViolationsFromFile() );
58 }
59
60
61
62
63
64
65
66
67
68
69
70
71
72 @Parameter( property = "pmd.failurePriority", defaultValue = "5", required = true )
73 private int failurePriority;
74
75
76
77
78 @Parameter( property = "pmd.skip", defaultValue = "false" )
79 private boolean skip;
80
81
82
83
84 public void execute()
85 throws MojoExecutionException, MojoFailureException
86 {
87 if ( !skip )
88 {
89 executeCheck( "pmd.xml", "violation", "PMD violation", failurePriority );
90 }
91 }
92
93
94
95
96 protected void printError( Violation item, String severity )
97 {
98
99 StringBuilder buff = new StringBuilder( 100 );
100 buff.append( "PMD " ).append( severity ).append( ": " );
101 if ( item.getViolationClass() != null )
102 {
103 if ( item.getViolationPackage() != null )
104 {
105 buff.append( item.getViolationPackage() );
106 buff.append( "." );
107 }
108 buff.append( item.getViolationClass() );
109 }
110 else
111 {
112 buff.append( item.getFileName() );
113 }
114 buff.append( ":" );
115 buff.append( item.getBeginline() );
116 buff.append( " Rule:" ).append( item.getRule() );
117 buff.append( " Priority:" ).append( item.getPriority() );
118 buff.append( " " ).append( item.getText() ).append( "." );
119
120 this.getLog().info( buff.toString() );
121 }
122
123 @Override
124 protected List<Violation> getErrorDetails( File pmdFile )
125 throws XmlPullParserException, IOException
126 {
127 try ( FileReader reader1 = new FileReader( pmdFile ) )
128 {
129 PmdXpp3Reader reader = new PmdXpp3Reader();
130 PmdErrorDetail details = reader.read( reader1, false );
131
132 List<Violation> violations = new ArrayList<>();
133 for ( PmdFile file : details.getFiles() )
134 {
135 String fullPath = file.getName();
136
137 for ( Violation violation : file.getViolations() )
138 {
139 violation.setFileName( getFilename( fullPath, violation.getViolationPackage() ) );
140 violations.add( violation );
141 }
142 }
143 return violations;
144 }
145 }
146
147 @Override
148 protected int getPriority( Violation errorDetail )
149 {
150 return errorDetail.getPriority();
151 }
152
153 @Override
154 protected ViolationDetails<Violation> newViolationDetailsInstance()
155 {
156 return new ViolationDetails<>();
157 }
158
159 private String getFilename( String fullpath, String pkg )
160 {
161 int index = fullpath.lastIndexOf( File.separatorChar );
162
163 while ( StringUtils.isNotEmpty( pkg ) )
164 {
165 index = fullpath.substring( 0, index ).lastIndexOf( File.separatorChar );
166
167 int dot = pkg.indexOf( '.' );
168
169 if ( dot < 0 )
170 {
171 break;
172 }
173 pkg = pkg.substring( dot + 1 );
174 }
175
176 return fullpath.substring( index + 1 );
177 }
178 }