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 = 5;
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 getLog().info( "Skipping PMD execution" );
90 return;
91 }
92
93 executeCheck( "pmd.xml", "violation", "PMD violation", failurePriority );
94 }
95
96
97
98
99 protected void printError( Violation item, String severity )
100 {
101
102 StringBuilder buff = new StringBuilder( 100 );
103 buff.append( "PMD " ).append( severity ).append( ": " );
104 if ( item.getViolationClass() != null )
105 {
106 if ( item.getViolationPackage() != null )
107 {
108 buff.append( item.getViolationPackage() );
109 buff.append( "." );
110 }
111 buff.append( item.getViolationClass() );
112 }
113 else
114 {
115 buff.append( item.getFileName() );
116 }
117 buff.append( ":" );
118 buff.append( item.getBeginline() );
119 buff.append( " Rule:" ).append( item.getRule() );
120 buff.append( " Priority:" ).append( item.getPriority() );
121 buff.append( " " ).append( item.getText() ).append( "." );
122
123 this.getLog().info( buff.toString() );
124 }
125
126 @Override
127 protected List<Violation> getErrorDetails( File pmdFile )
128 throws XmlPullParserException, IOException
129 {
130 try ( FileReader reader1 = new FileReader( pmdFile ) )
131 {
132 PmdXpp3Reader reader = new PmdXpp3Reader();
133 PmdErrorDetail details = reader.read( reader1, false );
134
135 List<Violation> violations = new ArrayList<>();
136 for ( PmdFile file : details.getFiles() )
137 {
138 String fullPath = file.getName();
139
140 for ( Violation violation : file.getViolations() )
141 {
142 violation.setFileName( getFilename( fullPath, violation.getViolationPackage() ) );
143 violations.add( violation );
144 }
145 }
146 return violations;
147 }
148 }
149
150 @Override
151 protected int getPriority( Violation errorDetail )
152 {
153 return errorDetail.getPriority();
154 }
155
156 @Override
157 protected ViolationDetails<Violation> newViolationDetailsInstance()
158 {
159 return new ViolationDetails<>();
160 }
161
162 private String getFilename( String fullpath, String pkg )
163 {
164 int index = fullpath.lastIndexOf( File.separatorChar );
165
166 while ( StringUtils.isNotEmpty( pkg ) )
167 {
168 index = fullpath.substring( 0, index ).lastIndexOf( File.separatorChar );
169
170 int dot = pkg.indexOf( '.' );
171
172 if ( dot < 0 )
173 {
174 break;
175 }
176 pkg = pkg.substring( dot + 1 );
177 }
178
179 return fullpath.substring( index + 1 );
180 }
181 }