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