1 package org.apache.maven.plugin.checkstyle;
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.net.URL;
24 import java.util.Collections;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Locale;
28 import java.util.Map;
29
30 import org.apache.maven.model.Resource;
31 import org.apache.maven.plugin.checkstyle.exec.CheckstyleExecutorRequest;
32 import org.apache.maven.plugins.annotations.Mojo;
33 import org.apache.maven.plugins.annotations.Parameter;
34 import org.apache.maven.plugins.annotations.ResolutionScope;
35 import org.apache.maven.project.MavenProject;
36 import org.apache.maven.reporting.MavenReportException;
37 import org.codehaus.plexus.util.StringUtils;
38
39
40
41
42
43
44
45
46
47
48 @Mojo( name = "checkstyle", requiresDependencyResolution = ResolutionScope.COMPILE, threadSafe = true )
49 public class CheckstyleReport
50 extends AbstractCheckstyleReport
51 {
52
53
54
55 private static final Map<String, String> FORMAT_TO_CONFIG_LOCATION;
56
57 static
58 {
59 Map<String, String> fmt2Cfg = new HashMap<String, String>();
60
61 fmt2Cfg.put( "sun", "config/sun_checks.xml" );
62 fmt2Cfg.put( "maven", "config/maven_checks.xml" );
63
64 FORMAT_TO_CONFIG_LOCATION = Collections.unmodifiableMap( fmt2Cfg );
65 }
66
67
68
69
70
71
72
73 @Parameter( defaultValue = "sun" )
74 private String format;
75
76
77
78
79
80
81
82 @Parameter
83 private File propertiesFile;
84
85
86
87
88
89
90
91 @Parameter
92 private URL propertiesURL;
93
94
95
96
97
98
99
100
101 @Parameter( defaultValue = "${basedir}/LICENSE.txt" )
102 private File headerFile;
103
104
105
106
107
108
109
110
111
112
113 @Parameter
114 private String suppressionsFile;
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132 @Parameter
133 private String packageNamesLocation;
134
135
136
137
138
139
140
141 @Parameter
142 private String packageNamesFile;
143
144
145 protected MavenProject getProject()
146 {
147 return project;
148 }
149
150
151 public void executeReport( Locale locale )
152 throws MavenReportException
153 {
154 mergeDeprecatedInfo();
155 super.executeReport( locale );
156 }
157
158
159
160
161 protected CheckstyleExecutorRequest createRequest()
162 throws MavenReportException
163 {
164 CheckstyleExecutorRequest request = new CheckstyleExecutorRequest();
165 request.setConsoleListener( getConsoleListener() ).setConsoleOutput( consoleOutput )
166 .setExcludes( excludes ).setFailsOnError( failsOnError ).setIncludes( includes )
167 .setResourceIncludes( resourceIncludes )
168 .setResourceExcludes( resourceExcludes )
169 .setIncludeResources( includeResources )
170 .setIncludeTestResources( includeTestResources )
171 .setIncludeTestSourceDirectory( includeTestSourceDirectory ).setListener( getListener() )
172 .setProject( project ).setSourceDirectories( getSourceDirectories() )
173 .setResources( resources )
174 .setStringOutputStream( stringOutputStream ).setSuppressionsLocation( suppressionsLocation )
175 .setTestSourceDirectories( getTestSourceDirectories() ).setConfigLocation( configLocation )
176 .setPropertyExpansion( propertyExpansion ).setHeaderLocation( headerLocation )
177 .setCacheFile( cacheFile ).setSuppressionsFileExpression( suppressionsFileExpression )
178 .setEncoding( encoding ).setPropertiesLocation( propertiesLocation );
179 return request;
180 }
181
182
183 public String getOutputName()
184 {
185 return "checkstyle";
186 }
187
188
189 public boolean canGenerateReport()
190 {
191 if ( skip )
192 {
193 return false;
194 }
195
196
197 for ( File sourceDirectory : getSourceDirectories() )
198 {
199 if ( sourceDirectory.exists() )
200 {
201 return true;
202 }
203 }
204
205 if ( includeTestSourceDirectory )
206 {
207 for ( File testSourceDirectory : getTestSourceDirectories() )
208 {
209 if ( testSourceDirectory.exists() )
210 {
211 return true;
212 }
213 }
214 }
215
216 return ( ( includeResources && hasResources( resources ) )
217 || ( includeTestResources && hasResources( testResources ) )
218 );
219 }
220
221
222
223
224
225
226 private boolean hasResources( List<Resource> resources )
227 {
228 for ( Resource resource : resources )
229 {
230 if ( new File( resource.getDirectory() ).exists() )
231 {
232 return true;
233 }
234 }
235 return false;
236 }
237
238
239
240
241
242
243
244
245 private void mergeDeprecatedInfo()
246 throws MavenReportException
247 {
248 if ( "config/sun_checks.xml".equals( configLocation ) && !"sun".equals( format ) )
249 {
250 configLocation = FORMAT_TO_CONFIG_LOCATION.get( format );
251
252 throw new MavenReportException( "'format' parameter is deprecated: please replace with <configLocation>"
253 + configLocation + "</configLocation>." );
254 }
255
256 if ( StringUtils.isEmpty( propertiesLocation ) )
257 {
258 if ( propertiesFile != null )
259 {
260 propertiesLocation = propertiesFile.getPath();
261
262 throw new MavenReportException( "'propertiesFile' parameter is deprecated: please replace with "
263 + "<propertiesLocation>" + propertiesLocation + "</propertiesLocation>." );
264 }
265 else if ( propertiesURL != null )
266 {
267 propertiesLocation = propertiesURL.toExternalForm();
268
269 throw new MavenReportException( "'propertiesURL' parameter is deprecated: please replace with "
270 + "<propertiesLocation>" + propertiesLocation + "</propertiesLocation>." );
271 }
272 }
273
274 if ( "LICENSE.txt".equals( headerLocation ) )
275 {
276 File defaultHeaderFile = new File( project.getBasedir(), "LICENSE.txt" );
277 if ( !defaultHeaderFile.equals( headerFile ) )
278 {
279 headerLocation = headerFile.getPath();
280 }
281 }
282
283 if ( StringUtils.isEmpty( suppressionsLocation ) )
284 {
285 suppressionsLocation = suppressionsFile;
286
287 if ( StringUtils.isNotEmpty( suppressionsFile ) )
288 {
289 throw new MavenReportException( "'suppressionsFile' parameter is deprecated: please replace with "
290 + "<suppressionsLocation>" + suppressionsLocation + "</suppressionsLocation>." );
291 }
292 }
293
294 if ( StringUtils.isEmpty( packageNamesLocation ) )
295 {
296 packageNamesLocation = packageNamesFile;
297
298 if ( StringUtils.isNotEmpty( packageNamesFile ) )
299 {
300 throw new MavenReportException( "'packageNamesFile' parameter is deprecated: please replace with "
301 + "<packageNamesFile>" + suppressionsLocation + "</packageNamesFile>." );
302 }
303 }
304 }
305
306 }