|
18 | 18 |
|
19 | 19 | import java.io.File; |
20 | 20 | import java.math.BigInteger; |
| 21 | +import java.net.URL; |
| 22 | +import java.net.URLClassLoader; |
21 | 23 | import java.util.ArrayList; |
22 | 24 | import java.util.Arrays; |
23 | 25 | import java.util.Collections; |
24 | 26 | import java.util.Date; |
25 | 27 | import java.util.LinkedList; |
26 | 28 | import java.util.List; |
27 | 29 |
|
| 30 | +import org.apache.maven.model.Resource; |
28 | 31 | import org.apache.maven.plugin.AbstractMojo; |
29 | 32 | import org.apache.maven.plugin.MojoExecutionException; |
30 | 33 | import org.apache.maven.plugin.MojoFailureException; |
31 | | -import org.apache.maven.plugins.annotations.LifecyclePhase; |
32 | | -import org.apache.maven.plugins.annotations.Mojo; |
33 | | -import org.apache.maven.plugins.annotations.Parameter; |
| 34 | +import org.apache.maven.plugins.annotations.*; |
| 35 | +import org.apache.maven.project.MavenProject; |
| 36 | +import org.codehaus.plexus.resource.ResourceManager; |
| 37 | +import org.codehaus.plexus.resource.loader.FileResourceCreationException; |
| 38 | +import org.codehaus.plexus.resource.loader.ResourceNotFoundException; |
34 | 39 | import org.scalastyle.Directory; |
35 | 40 | import org.scalastyle.FileSpec; |
36 | 41 | import org.scalastyle.Message; |
|
45 | 50 | /** |
46 | 51 | * Entry point for scalastyle maven plugin. |
47 | 52 | */ |
48 | | -@Mojo(name = "check", defaultPhase = LifecyclePhase.VERIFY, requiresProject = true) |
| 53 | +@Mojo(name = "check", defaultPhase = LifecyclePhase.VERIFY, requiresProject = true, requiresDependencyResolution = ResolutionScope.TEST) |
49 | 54 | public class ScalastyleViolationCheckMojo extends AbstractMojo { |
| 55 | + |
50 | 56 | /** |
51 | | - * Specifies the configuration file location |
| 57 | + * <p> |
| 58 | + * Specifies the location of the scalstyle XML configuration file to use. |
| 59 | + * </p> |
| 60 | + * <p> |
| 61 | + * Potential values are a filesystem path, a URL, or a classpath resource. |
| 62 | + * </p> |
| 63 | + * <p> |
| 64 | + * This parameter is resolved as file, classpath resource then URL. |
| 65 | + * </p> |
| 66 | + * <p> |
| 67 | + * <b>default_config.xml</b> from scalastyle classpath jar is used if non is specified in configuration |
| 68 | + * </p> |
| 69 | + * <p/> |
52 | 70 | */ |
53 | | - @Parameter(property = "scalastyle.config.location", required = true) |
| 71 | + @Parameter(property = "scalastyle.config.location", required = true, defaultValue = "default_config.xml") |
54 | 72 | private String configLocation; |
55 | 73 |
|
56 | 74 | /** |
@@ -147,6 +165,16 @@ public class ScalastyleViolationCheckMojo extends AbstractMojo { |
147 | 165 | @Parameter(property = "scalastyle.input.encoding") |
148 | 166 | private String inputEncoding; |
149 | 167 |
|
| 168 | + |
| 169 | + /** |
| 170 | + * The Maven Project Object. |
| 171 | + */ |
| 172 | + @Component |
| 173 | + protected MavenProject project; |
| 174 | + |
| 175 | + @Component |
| 176 | + private ResourceManager resourceManager; |
| 177 | + |
150 | 178 | public void execute() throws MojoFailureException, MojoExecutionException { |
151 | 179 | if (Boolean.TRUE.equals(skip)) { |
152 | 180 | getLog().warn("Scalastyle:check is skipped as scalastyle.skip=true"); |
@@ -175,10 +203,8 @@ public void execute() throws MojoFailureException, MojoExecutionException { |
175 | 203 | } |
176 | 204 |
|
177 | 205 | private void performCheck() throws MojoFailureException, MojoExecutionException { |
178 | | - checkConfigFile(configLocation); |
179 | | - |
180 | 206 | try { |
181 | | - ScalastyleConfiguration configuration = ScalastyleConfiguration.readFromXml(configLocation); |
| 207 | + ScalastyleConfiguration configuration = ScalastyleConfiguration.readFromXml(getConfigFile(configLocation)); |
182 | 208 | long start = now(); |
183 | 209 | List<Message<FileSpec>> messages = new ScalastyleChecker<FileSpec>().checkFilesAsJava(configuration, getFilesToProcess()); |
184 | 210 |
|
@@ -221,14 +247,55 @@ private long now() { |
221 | 247 | return new Date().getTime(); |
222 | 248 | } |
223 | 249 |
|
224 | | - private void checkConfigFile(String configLocation) throws MojoFailureException, MojoExecutionException { |
| 250 | + private String getConfigFile(String configLocation) throws MojoFailureException { |
225 | 251 | if (configLocation == null) { |
226 | 252 | throw new MojoFailureException("configLocation is required"); |
227 | 253 | } |
228 | 254 |
|
229 | | - if (!new File(configLocation).exists()) { |
230 | | - throw new MojoFailureException("configLocation " + configLocation + " does not exist"); |
| 255 | + if (new File(configLocation).exists()) { |
| 256 | + return configLocation; |
| 257 | + } |
| 258 | + |
| 259 | + ClassLoader original = Thread.currentThread().getContextClassLoader(); |
| 260 | + try { |
| 261 | + Thread.currentThread().setContextClassLoader(getClassLoaderWithProjectResources()); |
| 262 | + File configFile = resourceManager.getResourceAsFile(configLocation); |
| 263 | + if ( configFile == null ){ |
| 264 | + throw new MojoFailureException("Unable to process configuration file at location "+ configLocation); |
| 265 | + } |
| 266 | + return configFile.getAbsolutePath(); |
| 267 | + } catch (ResourceNotFoundException e) { |
| 268 | + throw new MojoFailureException("Unable to find configuration file at location "+ configLocation); |
| 269 | + } catch (FileResourceCreationException e) { |
| 270 | + throw new MojoFailureException("Unable to process configuration file at location "+ configLocation,e); |
| 271 | + }finally { |
| 272 | + Thread.currentThread().setContextClassLoader(original); |
| 273 | + } |
| 274 | + |
| 275 | + } |
| 276 | + |
| 277 | + private URLClassLoader getClassLoaderWithProjectResources() throws MojoFailureException { |
| 278 | + List<String> classPathStrings = new ArrayList<String>(); |
| 279 | + List<URL> urls = new ArrayList<URL>( classPathStrings.size() ); |
| 280 | + |
| 281 | + try { |
| 282 | + classPathStrings.addAll(project.getTestCompileSourceRoots()); |
| 283 | + classPathStrings.addAll(project.getCompileSourceRoots()); |
| 284 | + |
| 285 | + for(Resource resource:project.getTestResources()){ |
| 286 | + classPathStrings.add(resource.getDirectory()); |
| 287 | + } |
| 288 | + for(Resource resource:project.getResources()){ |
| 289 | + classPathStrings.add(resource.getDirectory()); |
| 290 | + } |
| 291 | + for ( String path : classPathStrings ){ |
| 292 | + urls.add(new File(path).toURI().toURL()); |
| 293 | + } |
| 294 | + } catch (Exception e) { |
| 295 | + throw new MojoFailureException(e.getMessage(),e); |
231 | 296 | } |
| 297 | + |
| 298 | + return new URLClassLoader(urls.toArray(new URL[urls.size()]), Thread.currentThread().getContextClassLoader()); |
232 | 299 | } |
233 | 300 |
|
234 | 301 | private List<FileSpec> getFilesToProcess() { |
@@ -271,3 +338,4 @@ private List<File> arrayOrValue(File[] array, File value) { |
271 | 338 | return (array != null) ? Arrays.asList(array) : Collections.singletonList(value); |
272 | 339 | } |
273 | 340 | } |
| 341 | + |
0 commit comments