| 1 | |
package org.apache.maven.plugin.dependency; |
| 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.StringWriter; |
| 24 | |
import java.util.Set; |
| 25 | |
|
| 26 | |
import org.apache.commons.lang.StringUtils; |
| 27 | |
import org.apache.maven.artifact.Artifact; |
| 28 | |
import org.apache.maven.plugin.AbstractMojo; |
| 29 | |
import org.apache.maven.plugin.MojoExecutionException; |
| 30 | |
import org.apache.maven.plugin.MojoFailureException; |
| 31 | |
import org.apache.maven.plugins.annotations.Component; |
| 32 | |
import org.apache.maven.plugins.annotations.Parameter; |
| 33 | |
import org.apache.maven.project.MavenProject; |
| 34 | |
import org.apache.maven.shared.dependency.analyzer.ProjectDependencyAnalysis; |
| 35 | |
import org.apache.maven.shared.dependency.analyzer.ProjectDependencyAnalyzer; |
| 36 | |
import org.apache.maven.shared.dependency.analyzer.ProjectDependencyAnalyzerException; |
| 37 | |
import org.codehaus.plexus.PlexusConstants; |
| 38 | |
import org.codehaus.plexus.PlexusContainer; |
| 39 | |
import org.codehaus.plexus.context.Context; |
| 40 | |
import org.codehaus.plexus.context.ContextException; |
| 41 | |
import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable; |
| 42 | |
import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter; |
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | 0 | public abstract class AbstractAnalyzeMojo |
| 53 | |
extends AbstractMojo |
| 54 | |
implements Contextualizable |
| 55 | |
{ |
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
private Context context; |
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
@Component |
| 68 | |
private MavenProject project; |
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
@Parameter( property = "analyzer", defaultValue = "default" ) |
| 81 | |
private String analyzer; |
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
@Parameter( property = "failOnWarning", defaultValue = "false" ) |
| 87 | |
private boolean failOnWarning; |
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
@Parameter( property = "verbose", defaultValue = "false" ) |
| 93 | |
private boolean verbose; |
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
@Parameter( property = "ignoreNonCompile", defaultValue = "false" ) |
| 99 | |
private boolean ignoreNonCompile; |
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
@Parameter( property = "outputXML", defaultValue = "false" ) |
| 107 | |
private boolean outputXML; |
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
@Parameter( property = "scriptableOutput", defaultValue = "false" ) |
| 115 | |
private boolean scriptableOutput; |
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
@Parameter( property = "scriptableFlag", defaultValue = "$$$%%%" ) |
| 123 | |
private String scriptableFlag; |
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | |
|
| 130 | |
@Parameter( defaultValue = "${basedir}", readonly = true ) |
| 131 | |
private File baseDir; |
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | |
@Parameter( defaultValue = "${project.build.directory}", readonly = true ) |
| 139 | |
private File outputDirectory; |
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
|
| 146 | |
|
| 147 | |
@Parameter |
| 148 | |
private String[] usedDependencies; |
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
public void execute() |
| 156 | |
throws MojoExecutionException, MojoFailureException |
| 157 | |
{ |
| 158 | 0 | if ( "pom".equals( project.getPackaging() ) ) |
| 159 | |
{ |
| 160 | 0 | getLog().info( "Skipping pom project" ); |
| 161 | 0 | return; |
| 162 | |
} |
| 163 | |
|
| 164 | 0 | if ( outputDirectory == null || !outputDirectory.exists() ) |
| 165 | |
{ |
| 166 | 0 | getLog().info( "Skipping project with no build directory" ); |
| 167 | 0 | return; |
| 168 | |
} |
| 169 | |
|
| 170 | 0 | boolean warning = checkDependencies(); |
| 171 | |
|
| 172 | 0 | if ( warning && failOnWarning ) |
| 173 | |
{ |
| 174 | 0 | throw new MojoExecutionException( "Dependency problems found" ); |
| 175 | |
} |
| 176 | 0 | } |
| 177 | |
|
| 178 | |
protected ProjectDependencyAnalyzer createProjectDependencyAnalyzer() |
| 179 | |
throws MojoExecutionException |
| 180 | |
{ |
| 181 | |
|
| 182 | 0 | final String role = ProjectDependencyAnalyzer.ROLE; |
| 183 | 0 | final String roleHint = analyzer; |
| 184 | |
|
| 185 | |
try |
| 186 | |
{ |
| 187 | 0 | final PlexusContainer container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY ); |
| 188 | |
|
| 189 | 0 | return (ProjectDependencyAnalyzer) container.lookup( role, roleHint ); |
| 190 | |
} |
| 191 | 0 | catch ( Exception exception ) |
| 192 | |
{ |
| 193 | 0 | throw new MojoExecutionException( |
| 194 | |
"Failed to instantiate ProjectDependencyAnalyser with role " + role + " / role-hint " + roleHint, |
| 195 | |
exception ); |
| 196 | |
} |
| 197 | |
} |
| 198 | |
|
| 199 | |
public void contextualize( Context context ) |
| 200 | |
throws ContextException |
| 201 | |
{ |
| 202 | 0 | this.context = context; |
| 203 | 0 | } |
| 204 | |
|
| 205 | |
|
| 206 | |
|
| 207 | |
private boolean checkDependencies() |
| 208 | |
throws MojoExecutionException |
| 209 | |
{ |
| 210 | |
ProjectDependencyAnalysis analysis; |
| 211 | |
try |
| 212 | |
{ |
| 213 | 0 | analysis = createProjectDependencyAnalyzer().analyze( project ); |
| 214 | |
|
| 215 | 0 | if ( usedDependencies != null ) |
| 216 | |
{ |
| 217 | 0 | analysis = analysis.forceDeclaredDependenciesUsage( usedDependencies ); |
| 218 | |
} |
| 219 | |
} |
| 220 | 0 | catch ( ProjectDependencyAnalyzerException exception ) |
| 221 | |
{ |
| 222 | 0 | throw new MojoExecutionException( "Cannot analyze dependencies", exception ); |
| 223 | 0 | } |
| 224 | |
|
| 225 | 0 | if ( ignoreNonCompile ) |
| 226 | |
{ |
| 227 | 0 | analysis = analysis.ignoreNonCompile(); |
| 228 | |
} |
| 229 | |
|
| 230 | 0 | Set<Artifact> usedDeclared = analysis.getUsedDeclaredArtifacts(); |
| 231 | 0 | Set<Artifact> usedUndeclared = analysis.getUsedUndeclaredArtifacts(); |
| 232 | 0 | Set<Artifact> unusedDeclared = analysis.getUnusedDeclaredArtifacts(); |
| 233 | |
|
| 234 | 0 | if ( ( !verbose || usedDeclared.isEmpty() ) && usedUndeclared.isEmpty() && unusedDeclared.isEmpty() ) |
| 235 | |
{ |
| 236 | 0 | getLog().info( "No dependency problems found" ); |
| 237 | 0 | return false; |
| 238 | |
} |
| 239 | |
|
| 240 | 0 | if ( verbose && !usedDeclared.isEmpty() ) |
| 241 | |
{ |
| 242 | 0 | getLog().info( "Used declared dependencies found:" ); |
| 243 | |
|
| 244 | 0 | logArtifacts( analysis.getUsedDeclaredArtifacts(), false ); |
| 245 | |
} |
| 246 | |
|
| 247 | 0 | if ( !usedUndeclared.isEmpty() ) |
| 248 | |
{ |
| 249 | 0 | getLog().warn( "Used undeclared dependencies found:" ); |
| 250 | |
|
| 251 | 0 | logArtifacts( usedUndeclared, true ); |
| 252 | |
} |
| 253 | |
|
| 254 | 0 | if ( !unusedDeclared.isEmpty() ) |
| 255 | |
{ |
| 256 | 0 | getLog().warn( "Unused declared dependencies found:" ); |
| 257 | |
|
| 258 | 0 | logArtifacts( unusedDeclared, true ); |
| 259 | |
} |
| 260 | |
|
| 261 | 0 | if ( outputXML ) |
| 262 | |
{ |
| 263 | 0 | writeDependencyXML( usedUndeclared ); |
| 264 | |
} |
| 265 | |
|
| 266 | 0 | if ( scriptableOutput ) |
| 267 | |
{ |
| 268 | 0 | writeScriptableOutput( usedUndeclared ); |
| 269 | |
} |
| 270 | |
|
| 271 | 0 | return !usedUndeclared.isEmpty() || !unusedDeclared.isEmpty(); |
| 272 | |
} |
| 273 | |
|
| 274 | |
private void logArtifacts( Set<Artifact> artifacts, boolean warn ) |
| 275 | |
{ |
| 276 | 0 | if ( artifacts.isEmpty() ) |
| 277 | |
{ |
| 278 | 0 | getLog().info( " None" ); |
| 279 | |
} |
| 280 | |
else |
| 281 | |
{ |
| 282 | 0 | for ( Artifact artifact : artifacts ) |
| 283 | |
{ |
| 284 | |
|
| 285 | 0 | artifact.isSnapshot(); |
| 286 | |
|
| 287 | 0 | if ( warn ) |
| 288 | |
{ |
| 289 | 0 | getLog().warn( " " + artifact ); |
| 290 | |
} |
| 291 | |
else |
| 292 | |
{ |
| 293 | 0 | getLog().info( " " + artifact ); |
| 294 | |
} |
| 295 | |
|
| 296 | |
} |
| 297 | |
} |
| 298 | 0 | } |
| 299 | |
|
| 300 | |
private void writeDependencyXML( Set<Artifact> artifacts ) |
| 301 | |
{ |
| 302 | 0 | if ( !artifacts.isEmpty() ) |
| 303 | |
{ |
| 304 | 0 | getLog().info( "Add the following to your pom to correct the missing dependencies: " ); |
| 305 | |
|
| 306 | 0 | StringWriter out = new StringWriter(); |
| 307 | 0 | PrettyPrintXMLWriter writer = new PrettyPrintXMLWriter( out ); |
| 308 | |
|
| 309 | 0 | for ( Artifact artifact : artifacts ) |
| 310 | |
{ |
| 311 | |
|
| 312 | 0 | artifact.isSnapshot(); |
| 313 | |
|
| 314 | 0 | writer.startElement( "dependency" ); |
| 315 | 0 | writer.startElement( "groupId" ); |
| 316 | 0 | writer.writeText( artifact.getGroupId() ); |
| 317 | 0 | writer.endElement(); |
| 318 | 0 | writer.startElement( "artifactId" ); |
| 319 | 0 | writer.writeText( artifact.getArtifactId() ); |
| 320 | 0 | writer.endElement(); |
| 321 | 0 | writer.startElement( "version" ); |
| 322 | 0 | writer.writeText( artifact.getBaseVersion() ); |
| 323 | 0 | if ( !StringUtils.isBlank( artifact.getClassifier() ) ) |
| 324 | |
{ |
| 325 | 0 | writer.startElement( "classifier" ); |
| 326 | 0 | writer.writeText( artifact.getClassifier() ); |
| 327 | 0 | writer.endElement(); |
| 328 | |
} |
| 329 | 0 | writer.endElement(); |
| 330 | |
|
| 331 | 0 | if ( !Artifact.SCOPE_COMPILE.equals( artifact.getScope() ) ) |
| 332 | |
{ |
| 333 | 0 | writer.startElement( "scope" ); |
| 334 | 0 | writer.writeText( artifact.getScope() ); |
| 335 | 0 | writer.endElement(); |
| 336 | |
} |
| 337 | 0 | writer.endElement(); |
| 338 | |
} |
| 339 | |
|
| 340 | 0 | getLog().info( "\n" + out.getBuffer() ); |
| 341 | |
} |
| 342 | 0 | } |
| 343 | |
|
| 344 | |
private void writeScriptableOutput( Set<Artifact> artifacts ) |
| 345 | |
{ |
| 346 | 0 | if ( !artifacts.isEmpty() ) |
| 347 | |
{ |
| 348 | 0 | getLog().info( "Missing dependencies: " ); |
| 349 | 0 | String pomFile = baseDir.getAbsolutePath() + File.separatorChar + "pom.xml"; |
| 350 | 0 | StringBuilder buf = new StringBuilder(); |
| 351 | |
|
| 352 | 0 | for ( Artifact artifact : artifacts ) |
| 353 | |
{ |
| 354 | |
|
| 355 | 0 | artifact.isSnapshot(); |
| 356 | |
|
| 357 | 0 | buf.append( scriptableFlag + ":" + pomFile + ":" + artifact.getDependencyConflictId() + ":" |
| 358 | |
+ artifact.getClassifier() + ":" + artifact.getBaseVersion() + ":" + artifact.getScope() |
| 359 | |
+ "\n" ); |
| 360 | |
} |
| 361 | 0 | getLog().info( "\n" + buf ); |
| 362 | |
} |
| 363 | 0 | } |
| 364 | |
} |