| 1 |
package org.codehaus.mojo.animal_sniffer.maven; |
| 2 |
|
| 3 |
/* |
| 4 |
* The MIT License |
| 5 |
* |
| 6 |
* Copyright (c) 2008 Kohsuke Kawaguchi and codehaus.org. |
| 7 |
* |
| 8 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 |
* of this software and associated documentation files (the "Software"), to deal |
| 10 |
* in the Software without restriction, including without limitation the rights |
| 11 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 |
* copies of the Software, and to permit persons to whom the Software is |
| 13 |
* furnished to do so, subject to the following conditions: |
| 14 |
* |
| 15 |
* The above copyright notice and this permission notice shall be included in |
| 16 |
* all copies or substantial portions of the Software. |
| 17 |
* |
| 18 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 |
* THE SOFTWARE. |
| 25 |
* |
| 26 |
*/ |
| 27 |
|
| 28 |
import org.apache.maven.artifact.Artifact; |
| 29 |
import org.apache.maven.artifact.factory.ArtifactFactory; |
| 30 |
import org.apache.maven.artifact.repository.ArtifactRepository; |
| 31 |
import org.apache.maven.artifact.resolver.AbstractArtifactResolutionException; |
| 32 |
import org.apache.maven.artifact.resolver.ArtifactResolver; |
| 33 |
import org.apache.maven.model.Dependency; |
| 34 |
import org.apache.maven.plugin.AbstractMojo; |
| 35 |
import org.apache.maven.plugin.MojoExecutionException; |
| 36 |
import org.apache.maven.plugin.MojoFailureException; |
| 37 |
import org.apache.maven.plugins.annotations.Component; |
| 38 |
import org.apache.maven.plugins.annotations.LifecyclePhase; |
| 39 |
import org.apache.maven.plugins.annotations.Mojo; |
| 40 |
import org.apache.maven.plugins.annotations.Parameter; |
| 41 |
import org.apache.maven.plugins.annotations.ResolutionScope; |
| 42 |
import org.apache.maven.project.MavenProject; |
| 43 |
import org.apache.maven.shared.artifact.filter.PatternExcludesArtifactFilter; |
| 44 |
import org.apache.maven.shared.artifact.filter.PatternIncludesArtifactFilter; |
| 45 |
import org.codehaus.mojo.animal_sniffer.ClassFileVisitor; |
| 46 |
import org.codehaus.mojo.animal_sniffer.ClassListBuilder; |
| 47 |
import org.codehaus.mojo.animal_sniffer.SignatureChecker; |
| 48 |
import org.codehaus.plexus.util.StringUtils; |
| 49 |
|
| 50 |
import java.io.File; |
| 51 |
import java.io.FileInputStream; |
| 52 |
import java.io.IOException; |
| 53 |
import java.util.ArrayList; |
| 54 |
import java.util.Arrays; |
| 55 |
import java.util.Iterator; |
| 56 |
import java.util.List; |
| 57 |
import java.util.Set; |
| 58 |
|
| 59 |
/** |
| 60 |
* Checks the classes compiled by this module. |
| 61 |
* |
| 62 |
* @author Kohsuke Kawaguchi |
| 63 |
*/ |
| 64 |
@Mojo( name = "check", defaultPhase = LifecyclePhase.PROCESS_CLASSES, requiresDependencyResolution = ResolutionScope.COMPILE, threadSafe = true ) |
| 65 |
public class CheckSignatureMojo |
| 66 |
extends AbstractMojo |
| 67 |
{ |
| 68 |
|
| 69 |
/** |
| 70 |
* The directory for compiled classes. |
| 71 |
*/ |
| 72 |
@Parameter( defaultValue = "${project.build.outputDirectory}", required = true, readonly = true ) |
| 73 |
protected File outputDirectory; |
| 74 |
|
| 75 |
/** |
| 76 |
* Signature module to use. |
| 77 |
*/ |
| 78 |
@Parameter( required = true, property="animal.sniffer.signature" ) |
| 79 |
protected Signature signature; |
| 80 |
|
| 81 |
/** |
| 82 |
* @param signatureId |
| 83 |
* A fully-qualified path to a signature jar. This allows users |
| 84 |
* to set a signature for command-line invocations, such as: |
| 85 |
* <p> |
| 86 |
* <code>mvn org.codehaus.mojo:animal-sniffer-maven-plugin:1.15:check -Dsignature=org.codehaus.mojo.signature:java17:1.0</code> |
| 87 |
*/ |
| 88 |
public void setSignature( String signatureId ) { |
| 89 |
String[] signatureParts = signatureId.split(":"); |
| 90 |
if(signatureParts.length == 3) { |
| 91 |
this.signature = new Signature(); |
| 92 |
this.signature.setGroupId(signatureParts[0]); |
| 93 |
this.signature.setArtifactId(signatureParts[1]); |
| 94 |
this.signature.setVersion(signatureParts[2]); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Class names to ignore signatures for (wildcards accepted). |
| 100 |
* |
| 101 |
*/ |
| 102 |
@Parameter |
| 103 |
protected String[] ignores; |
| 104 |
|
| 105 |
/** |
| 106 |
* Annotation names to consider to ignore annotated methods, classes or fields. |
| 107 |
* <p> |
| 108 |
* By default 'org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement' and |
| 109 |
* 'org.jvnet.animal_sniffer.IgnoreJRERequirement' are used. |
| 110 |
* |
| 111 |
* @see SignatureChecker#ANNOTATION_FQN |
| 112 |
* @see SignatureChecker#PREVIOUS_ANNOTATION_FQN |
| 113 |
*/ |
| 114 |
@Parameter |
| 115 |
protected String[] annotations; |
| 116 |
|
| 117 |
/** |
| 118 |
* Should dependencies be ignored. |
| 119 |
* |
| 120 |
*/ |
| 121 |
@Parameter( defaultValue = "true" ) |
| 122 |
protected boolean ignoreDependencies; |
| 123 |
|
| 124 |
/** |
| 125 |
* A list of artifact patterns to include. Patterns can include <code>*</code> as a wildcard match for any |
| 126 |
* <b>whole</b> segment, valid patterns are: |
| 127 |
* <ul> |
| 128 |
* <li><code>groupId:artifactId</code></li> |
| 129 |
* <li><code>groupId:artifactId:type</code></li> |
| 130 |
* <li><code>groupId:artifactId:type:version</code></li> |
| 131 |
* <li><code>groupId:artifactId:type:classifier</code></li> |
| 132 |
* <li><code>groupId:artifactId:type:classifier:version</code></li> |
| 133 |
* </ul> |
| 134 |
* |
| 135 |
* @since 1.12 |
| 136 |
*/ |
| 137 |
@Parameter |
| 138 |
private String[] includeDependencies = null; |
| 139 |
|
| 140 |
/** |
| 141 |
* A list of artifact patterns to exclude. Patterns can include <code>*</code> as a wildcard match for any |
| 142 |
* <b>whole</b> segment, valid patterns are: |
| 143 |
* <ul> |
| 144 |
* <li><code>groupId:artifactId</code></li> |
| 145 |
* <li><code>groupId:artifactId:type</code></li> |
| 146 |
* <li><code>groupId:artifactId:type:version</code></li> |
| 147 |
* <li><code>groupId:artifactId:type:classifier</code></li> |
| 148 |
* <li><code>groupId:artifactId:type:classifier:version</code></li> |
| 149 |
* </ul> |
| 150 |
* |
| 151 |
* @since 1.12 |
| 152 |
*/ |
| 153 |
@Parameter |
| 154 |
private String[] excludeDependencies = null; |
| 155 |
|
| 156 |
/** |
| 157 |
* Should signature checking be skipped? |
| 158 |
* |
| 159 |
*/ |
| 160 |
@Parameter( defaultValue = "false", property = "animal.sniffer.skip" ) |
| 161 |
protected boolean skip; |
| 162 |
|
| 163 |
/** |
| 164 |
*/ |
| 165 |
@Component |
| 166 |
protected ArtifactResolver resolver; |
| 167 |
|
| 168 |
/** |
| 169 |
*/ |
| 170 |
@Parameter( defaultValue = "${project}", readonly = true ) |
| 171 |
protected MavenProject project; |
| 172 |
|
| 173 |
/** |
| 174 |
*/ |
| 175 |
@Parameter( defaultValue = "${localRepository}", readonly=true ) |
| 176 |
protected ArtifactRepository localRepository; |
| 177 |
|
| 178 |
/** |
| 179 |
*/ |
| 180 |
@Component |
| 181 |
protected ArtifactFactory artifactFactory; |
| 182 |
|
| 183 |
public void execute() |
| 184 |
throws MojoExecutionException, MojoFailureException |
| 185 |
{ |
| 186 |
if ( skip ) |
| 187 |
{ |
| 188 |
getLog().info( "Signature checking is skipped." ); |
| 189 |
return; |
| 190 |
} |
| 191 |
|
| 192 |
if ( signature == null || StringUtils.isBlank(signature.getGroupId()) || signature.getGroupId() == "null") { |
| 193 |
getLog().info( "Signature version is: " + signature.getVersion() ); |
| 194 |
return; |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
try |
| 199 |
{ |
| 200 |
if ( StringUtils.isBlank( signature.getVersion() ) ) |
| 201 |
{ |
| 202 |
getLog().debug( "Resolving signature " + signature.getGroupId() + ":" + signature.getArtifactId() |
| 203 |
+ " version from dependencies" ); |
| 204 |
String source = "dependencies"; |
| 205 |
Dependency match = findMatchingDependency( signature, project.getDependencies() ); |
| 206 |
if ( match == null && project.getDependencyManagement() != null ) |
| 207 |
{ |
| 208 |
getLog().debug( "Resolving signature " + signature.getGroupId() + ":" + signature.getArtifactId() |
| 209 |
+ " version from dependencyManagement" ); |
| 210 |
source = "dependencyManagement"; |
| 211 |
match = findMatchingDependency( signature, project.getDependencyManagement().getDependencies() ); |
| 212 |
} |
| 213 |
if ( match != null ) |
| 214 |
{ |
| 215 |
getLog().info( "Resolved signature " + signature.getGroupId() + ":" + signature.getArtifactId() |
| 216 |
+ " version as " + match.getVersion() + " from " + source); |
| 217 |
signature.setVersion( match.getVersion() ); |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
getLog().info( "Checking unresolved references to " + signature ); |
| 222 |
|
| 223 |
Artifact a = signature.createArtifact( artifactFactory ); |
| 224 |
|
| 225 |
resolver.resolve( a, project.getRemoteArtifactRepositories(), localRepository ); |
| 226 |
// just check code from this module |
| 227 |
final Set<String> ignoredPackages = buildPackageList(); |
| 228 |
|
| 229 |
if ( ignores != null ) |
| 230 |
{ |
| 231 |
for ( int i = 0; i < ignores.length; i++ ) |
| 232 |
{ |
| 233 |
String ignore = ignores[i]; |
| 234 |
if ( ignore == null ) |
| 235 |
{ |
| 236 |
continue; |
| 237 |
} |
| 238 |
ignoredPackages.add( ignore.replace( '.', '/' ) ); |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
final SignatureChecker signatureChecker = |
| 243 |
new SignatureChecker( new FileInputStream( a.getFile() ), ignoredPackages, |
| 244 |
new MavenLogger( getLog() ) ); |
| 245 |
signatureChecker.setCheckJars( false ); // don't want to decend into jar files that have been copied to |
| 246 |
// the output directory as resources. |
| 247 |
List<File> sourcePaths = new ArrayList<File>(); |
| 248 |
Iterator<String> iterator = project.getCompileSourceRoots().iterator(); |
| 249 |
while ( iterator.hasNext() ) |
| 250 |
{ |
| 251 |
String path = iterator.next(); |
| 252 |
sourcePaths.add( new File( path ) ); |
| 253 |
} |
| 254 |
signatureChecker.setSourcePath( sourcePaths ); |
| 255 |
|
| 256 |
if ( annotations != null ) |
| 257 |
{ |
| 258 |
signatureChecker.setAnnotationTypes( Arrays.asList( annotations ) ); |
| 259 |
} |
| 260 |
|
| 261 |
signatureChecker.process( outputDirectory ); |
| 262 |
|
| 263 |
if ( signatureChecker.isSignatureBroken() ) |
| 264 |
{ |
| 265 |
throw new MojoFailureException( |
| 266 |
"Signature errors found. Verify them and ignore them with the proper annotation if needed." ); |
| 267 |
} |
| 268 |
} |
| 269 |
catch ( IOException e ) |
| 270 |
{ |
| 271 |
throw new MojoExecutionException( "Failed to check signatures", e ); |
| 272 |
} |
| 273 |
catch ( AbstractArtifactResolutionException e ) |
| 274 |
{ |
| 275 |
throw new MojoExecutionException( "Failed to obtain signature: " + signature, e ); |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
private static Dependency findMatchingDependency( Signature signature, List<Dependency> dependencies ) |
| 280 |
{ |
| 281 |
Dependency match = null; |
| 282 |
for ( Iterator<Dependency> iterator = dependencies.iterator(); iterator.hasNext(); ) |
| 283 |
{ |
| 284 |
Dependency d = iterator.next(); |
| 285 |
if ( StringUtils.isBlank( d.getVersion() ) ) |
| 286 |
{ |
| 287 |
continue; |
| 288 |
} |
| 289 |
if ( StringUtils.equals( d.getGroupId(), signature.getGroupId() ) && StringUtils.equals( d.getArtifactId(), |
| 290 |
signature.getArtifactId() ) ) |
| 291 |
{ |
| 292 |
if ( "signature".equals( d.getType() ) ) |
| 293 |
{ |
| 294 |
// this is a perfect match |
| 295 |
match = d; |
| 296 |
break; |
| 297 |
} |
| 298 |
if ( "pom".equals( d.getType() ) ) |
| 299 |
{ |
| 300 |
if ( match == null || "jar".equals( match.getType() ) ) |
| 301 |
{ |
| 302 |
match = d; |
| 303 |
} |
| 304 |
} |
| 305 |
if ( "jar".equals( d.getType() ) ) |
| 306 |
{ |
| 307 |
if ( match == null ) |
| 308 |
{ |
| 309 |
match = d; |
| 310 |
} |
| 311 |
} |
| 312 |
} |
| 313 |
} |
| 314 |
return match; |
| 315 |
} |
| 316 |
|
| 317 |
/** |
| 318 |
* List of packages defined in the application. |
| 319 |
*/ |
| 320 |
private Set<String> buildPackageList() |
| 321 |
throws IOException |
| 322 |
{ |
| 323 |
ClassListBuilder plb = new ClassListBuilder( new MavenLogger( getLog() ) ); |
| 324 |
apply( plb ); |
| 325 |
return plb.getPackages(); |
| 326 |
} |
| 327 |
|
| 328 |
private void apply( ClassFileVisitor v ) |
| 329 |
throws IOException |
| 330 |
{ |
| 331 |
v.process( outputDirectory ); |
| 332 |
if ( ignoreDependencies ) |
| 333 |
{ |
| 334 |
PatternIncludesArtifactFilter includesFilter = includeDependencies == null |
| 335 |
? null |
| 336 |
: new PatternIncludesArtifactFilter( Arrays.asList( includeDependencies ) ); |
| 337 |
PatternExcludesArtifactFilter excludesFilter = excludeDependencies == null |
| 338 |
? null |
| 339 |
: new PatternExcludesArtifactFilter( Arrays.asList( excludeDependencies ) ); |
| 340 |
|
| 341 |
getLog().debug( "Building list of classes from dependencies" ); |
| 342 |
for ( Iterator<Artifact> i = project.getArtifacts().iterator(); i.hasNext(); ) |
| 343 |
{ |
| 344 |
|
| 345 |
Artifact artifact = i.next(); |
| 346 |
|
| 347 |
if ( !artifact.getArtifactHandler().isAddedToClasspath() ) { |
| 348 |
getLog().debug( "Skipping artifact " + BuildSignaturesMojo.artifactId( artifact ) |
| 349 |
+ " as it is not added to the classpath." ); |
| 350 |
continue; |
| 351 |
} |
| 352 |
|
| 353 |
if ( !( Artifact.SCOPE_COMPILE.equals( artifact.getScope() ) || Artifact.SCOPE_PROVIDED.equals( |
| 354 |
artifact.getScope() ) || Artifact.SCOPE_SYSTEM.equals( artifact.getScope() ) ) ) |
| 355 |
{ |
| 356 |
getLog().debug( "Skipping artifact " + BuildSignaturesMojo.artifactId( artifact ) |
| 357 |
+ " as it is not on the compile classpath." ); |
| 358 |
continue; |
| 359 |
} |
| 360 |
|
| 361 |
if ( includesFilter != null && !includesFilter.include( artifact ) ) |
| 362 |
{ |
| 363 |
getLog().debug( "Skipping classes in artifact " + BuildSignaturesMojo.artifactId( artifact ) |
| 364 |
+ " as it does not match include rules." ); |
| 365 |
continue; |
| 366 |
} |
| 367 |
|
| 368 |
if ( excludesFilter != null && !excludesFilter.include( artifact ) ) |
| 369 |
{ |
| 370 |
getLog().debug( "Skipping classes in artifact " + BuildSignaturesMojo.artifactId( artifact ) |
| 371 |
+ " as it does matches exclude rules." ); |
| 372 |
continue; |
| 373 |
} |
| 374 |
|
| 375 |
getLog().debug( "Adding classes in artifact " + BuildSignaturesMojo.artifactId( artifact ) + |
| 376 |
" to the ignores" ); |
| 377 |
v.process( artifact.getFile() ); |
| 378 |
} |
| 379 |
} |
| 380 |
} |
| 381 |
} |