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 org.apache.maven.artifact.Artifact; |
23 | |
import org.apache.maven.artifact.factory.ArtifactFactory; |
24 | |
import org.apache.maven.artifact.metadata.ArtifactMetadataSource; |
25 | |
import org.apache.maven.artifact.repository.ArtifactRepository; |
26 | |
import org.apache.maven.artifact.resolver.ArtifactNotFoundException; |
27 | |
import org.apache.maven.artifact.resolver.ArtifactResolutionException; |
28 | |
import org.apache.maven.artifact.resolver.ArtifactResolutionResult; |
29 | |
import org.apache.maven.artifact.resolver.ArtifactResolver; |
30 | |
import org.apache.maven.artifact.resolver.filter.AndArtifactFilter; |
31 | |
import org.apache.maven.artifact.resolver.filter.ArtifactFilter; |
32 | |
import org.apache.maven.plugin.AbstractMojo; |
33 | |
import org.apache.maven.plugin.MojoExecutionException; |
34 | |
import org.apache.maven.plugin.MojoFailureException; |
35 | |
import org.apache.maven.plugins.annotations.Component; |
36 | |
import org.apache.maven.plugins.annotations.Mojo; |
37 | |
import org.apache.maven.plugins.annotations.Parameter; |
38 | |
import org.apache.maven.project.MavenProject; |
39 | |
import org.apache.maven.project.artifact.InvalidDependencyVersionException; |
40 | |
import org.apache.maven.shared.artifact.filter.PatternExcludesArtifactFilter; |
41 | |
import org.apache.maven.shared.artifact.filter.PatternIncludesArtifactFilter; |
42 | |
import org.codehaus.plexus.util.FileUtils; |
43 | |
import org.codehaus.plexus.util.StringUtils; |
44 | |
|
45 | |
import java.io.File; |
46 | |
import java.io.IOException; |
47 | |
import java.util.ArrayList; |
48 | |
import java.util.Arrays; |
49 | |
import java.util.LinkedHashSet; |
50 | |
import java.util.List; |
51 | |
import java.util.Set; |
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
@Mojo( name = "purge-local-repository", threadSafe = true ) |
61 | 0 | public class PurgeLocalRepositoryMojo |
62 | |
extends AbstractMojo |
63 | |
{ |
64 | |
|
65 | |
public static final String FILE_FUZZINESS = "file"; |
66 | |
|
67 | |
public static final String VERSION_FUZZINESS = "version"; |
68 | |
|
69 | |
public static final String ARTIFACT_ID_FUZZINESS = "artifactId"; |
70 | |
|
71 | |
public static final String GROUP_ID_FUZZINESS = "groupId"; |
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
@Component |
77 | |
private MavenProject project; |
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
@Parameter |
88 | |
private List<String> manualIncludes; |
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
@Parameter( property = "manualInclude" ) |
98 | |
private String manualInclude; |
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
@Parameter |
106 | |
private List<String> includes; |
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
@Parameter( property = "include" ) |
116 | |
private String include; |
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
@Parameter |
122 | |
private List<String> excludes; |
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
@Parameter( property = "exclude" ) |
130 | |
private String exclude; |
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
@Parameter( property = "reResolve", defaultValue = "true" ) |
137 | |
private boolean reResolve; |
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
@Parameter( defaultValue = "${localRepository}", readonly = true, required = true ) |
143 | |
private ArtifactRepository localRepository; |
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
@Parameter( defaultValue = "${project.remoteArtifactRepositories}", readonly = true, required = true ) |
149 | |
protected List<ArtifactRepository> remoteRepositories; |
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
@Component |
155 | |
private ArtifactResolver resolver; |
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
@Component |
161 | |
private ArtifactMetadataSource metadataSource; |
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
@Parameter( property = "resolutionFuzziness", defaultValue = "version" ) |
173 | |
private String resolutionFuzziness; |
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | |
@Parameter( property = "actTransitively", defaultValue = "true" ) |
179 | |
private boolean actTransitively; |
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
@Component |
185 | |
private ArtifactFactory factory; |
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
@Parameter( property = "verbose", defaultValue = "false" ) |
191 | |
private boolean verbose; |
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
@Parameter( property = "snapshotsOnly", defaultValue = "false" ) |
199 | |
private boolean snapshotsOnly; |
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
private class DirectDependencyFilter |
205 | |
implements ArtifactFilter |
206 | |
{ |
207 | |
|
208 | |
private Artifact projectArtifact; |
209 | |
|
210 | |
private Set<Artifact> directDependencyArtifacts; |
211 | |
|
212 | |
|
213 | |
|
214 | |
|
215 | |
|
216 | |
|
217 | |
|
218 | |
public DirectDependencyFilter( Artifact projectArtifact, Set<Artifact> directDependencyArtifacts ) |
219 | 0 | { |
220 | 0 | this.projectArtifact = projectArtifact; |
221 | 0 | this.directDependencyArtifacts = directDependencyArtifacts; |
222 | 0 | } |
223 | |
|
224 | |
public boolean include( Artifact artifact ) |
225 | |
{ |
226 | 0 | if ( artifactsGAMatch( artifact, projectArtifact ) ) |
227 | |
{ |
228 | 0 | return true; |
229 | |
} |
230 | 0 | for ( Artifact depArtifact : directDependencyArtifacts ) |
231 | |
{ |
232 | 0 | if ( this.artifactsGAMatch( artifact, depArtifact ) ) |
233 | |
{ |
234 | 0 | return true; |
235 | |
} |
236 | |
} |
237 | 0 | return false; |
238 | |
} |
239 | |
|
240 | |
|
241 | |
|
242 | |
|
243 | |
private boolean artifactsGAMatch( Artifact artifact1, Artifact artifact2 ) |
244 | |
{ |
245 | 0 | if ( artifact1 == artifact2 ) |
246 | |
{ |
247 | 0 | return true; |
248 | |
} |
249 | |
|
250 | 0 | if ( !artifact1.getGroupId().equals( artifact2.getGroupId() ) ) |
251 | |
{ |
252 | 0 | getLog().debug( "Different groupId: " + artifact1 + " " + artifact2 ); |
253 | 0 | return false; |
254 | |
} |
255 | 0 | if ( !artifact1.getArtifactId().equals( artifact2.getArtifactId() ) ) |
256 | |
{ |
257 | 0 | getLog().debug( "Different artifactId: " + artifact1 + " " + artifact2 ); |
258 | 0 | return false; |
259 | |
} |
260 | 0 | return true; |
261 | |
} |
262 | |
} |
263 | |
|
264 | |
|
265 | |
|
266 | |
|
267 | 0 | private class SystemScopeExcludeFilter |
268 | |
implements ArtifactFilter |
269 | |
{ |
270 | |
public boolean include( Artifact artifact ) |
271 | |
{ |
272 | 0 | return !Artifact.SCOPE_SYSTEM.equals( artifact.getScope() ); |
273 | |
} |
274 | |
} |
275 | |
|
276 | |
|
277 | |
|
278 | |
|
279 | 0 | private class SnapshotsFilter |
280 | |
implements ArtifactFilter |
281 | |
{ |
282 | |
public boolean include( Artifact artifact ) |
283 | |
{ |
284 | 0 | return artifact.isSnapshot(); |
285 | |
} |
286 | |
} |
287 | |
|
288 | |
public void execute() |
289 | |
throws MojoExecutionException, MojoFailureException |
290 | |
{ |
291 | 0 | if ( !StringUtils.isEmpty( manualInclude ) ) |
292 | |
{ |
293 | 0 | manualIncludes = this.parseIncludes( manualInclude ); |
294 | |
} |
295 | |
|
296 | 0 | if ( manualIncludes != null && manualIncludes.size() > 0 ) |
297 | |
{ |
298 | 0 | manualPurge( manualIncludes ); |
299 | 0 | return; |
300 | |
} |
301 | |
|
302 | 0 | Set<Artifact> dependencyArtifacts = null; |
303 | |
|
304 | |
try |
305 | |
{ |
306 | 0 | dependencyArtifacts = project.createArtifacts( factory, null, null ); |
307 | |
} |
308 | 0 | catch ( InvalidDependencyVersionException e ) |
309 | |
{ |
310 | 0 | throw new MojoFailureException( "Unable to purge dependencies due to invalid dependency version ", e ); |
311 | 0 | } |
312 | |
|
313 | 0 | ArtifactFilter artifactFilter = createPurgeArtifactsFilter( dependencyArtifacts ); |
314 | |
|
315 | 0 | Set<Artifact> resolvedArtifactsToPurge = |
316 | |
getFilteredResolvedArtifacts( project, dependencyArtifacts, artifactFilter ); |
317 | |
|
318 | 0 | if ( resolvedArtifactsToPurge.isEmpty() ) |
319 | |
{ |
320 | 0 | getLog().info( "No artifacts included for purge for project: " + project.getId() ); |
321 | 0 | return; |
322 | |
} |
323 | |
|
324 | 0 | verbose( "Purging dependencies for project: " + project.getId() ); |
325 | 0 | purgeArtifacts( resolvedArtifactsToPurge ); |
326 | |
|
327 | 0 | if ( reResolve ) |
328 | |
{ |
329 | |
try |
330 | |
{ |
331 | 0 | reResolveArtifacts( project, resolvedArtifactsToPurge, artifactFilter ); |
332 | |
} |
333 | 0 | catch ( ArtifactResolutionException e ) |
334 | |
{ |
335 | 0 | String failureMessage = "Failed to refresh project dependencies for: " + project.getId(); |
336 | 0 | MojoFailureException failure = new MojoFailureException( failureMessage ); |
337 | 0 | failure.initCause( e ); |
338 | |
|
339 | 0 | throw failure; |
340 | |
} |
341 | 0 | catch ( ArtifactNotFoundException e ) |
342 | |
{ |
343 | 0 | String failureMessage = "Failed to refresh project dependencies for: " + project.getId(); |
344 | 0 | MojoFailureException failure = new MojoFailureException( failureMessage ); |
345 | 0 | failure.initCause( e ); |
346 | |
|
347 | 0 | throw failure; |
348 | 0 | } |
349 | |
} |
350 | 0 | } |
351 | |
|
352 | |
|
353 | |
|
354 | |
|
355 | |
|
356 | |
|
357 | |
|
358 | |
private void manualPurge( List<String> includes ) |
359 | |
throws MojoExecutionException |
360 | |
{ |
361 | 0 | for ( String gavPattern : includes ) |
362 | |
{ |
363 | 0 | if ( StringUtils.isEmpty( gavPattern ) ) |
364 | |
{ |
365 | 0 | getLog().debug( "Skipping empty gav pattern: " + gavPattern ); |
366 | 0 | continue; |
367 | |
} |
368 | |
|
369 | 0 | String relativePath = gavToPath( gavPattern ); |
370 | 0 | if ( StringUtils.isEmpty( relativePath ) ) |
371 | |
{ |
372 | 0 | continue; |
373 | |
} |
374 | |
|
375 | 0 | File purgeDir = new File( localRepository.getBasedir(), relativePath ); |
376 | 0 | if ( purgeDir.exists() ) |
377 | |
{ |
378 | 0 | getLog().debug( "Deleting directory: " + purgeDir ); |
379 | |
try |
380 | |
{ |
381 | 0 | FileUtils.deleteDirectory( purgeDir ); |
382 | |
} |
383 | 0 | catch ( IOException e ) |
384 | |
{ |
385 | 0 | throw new MojoExecutionException( "Unable to purge directory: " + purgeDir ); |
386 | 0 | } |
387 | |
} |
388 | 0 | } |
389 | 0 | } |
390 | |
|
391 | |
|
392 | |
|
393 | |
|
394 | |
|
395 | |
|
396 | |
|
397 | |
private String gavToPath( String gav ) |
398 | |
{ |
399 | 0 | if ( StringUtils.isEmpty( gav ) ) |
400 | |
{ |
401 | 0 | return null; |
402 | |
} |
403 | |
|
404 | 0 | String[] pathComponents = gav.split( ":" ); |
405 | |
|
406 | 0 | StringBuffer path = new StringBuffer( pathComponents[0].replace( '.', '/' ) ); |
407 | |
|
408 | 0 | for ( int i = 1; i < pathComponents.length; ++i ) |
409 | |
{ |
410 | 0 | path.append( "/" + pathComponents[i] ); |
411 | |
} |
412 | |
|
413 | 0 | return path.toString(); |
414 | |
} |
415 | |
|
416 | |
|
417 | |
|
418 | |
|
419 | |
|
420 | |
|
421 | |
|
422 | |
|
423 | |
|
424 | |
private ArtifactFilter createPurgeArtifactsFilter( Set<Artifact> dependencyArtifacts ) |
425 | |
{ |
426 | 0 | AndArtifactFilter andFilter = new AndArtifactFilter(); |
427 | |
|
428 | |
|
429 | 0 | andFilter.add( new SystemScopeExcludeFilter() ); |
430 | |
|
431 | 0 | if ( this.snapshotsOnly ) |
432 | |
{ |
433 | 0 | andFilter.add( new SnapshotsFilter() ); |
434 | |
} |
435 | |
|
436 | |
|
437 | 0 | if ( !StringUtils.isEmpty( this.include ) ) |
438 | |
{ |
439 | 0 | this.includes = parseIncludes( this.include ); |
440 | |
} |
441 | 0 | if ( this.includes != null ) |
442 | |
{ |
443 | 0 | andFilter.add( new PatternIncludesArtifactFilter( includes ) ); |
444 | |
} |
445 | |
|
446 | 0 | if ( !StringUtils.isEmpty( this.exclude ) ) |
447 | |
{ |
448 | 0 | this.excludes = parseIncludes( this.exclude ); |
449 | |
} |
450 | 0 | if ( this.excludes != null ) |
451 | |
{ |
452 | 0 | andFilter.add( new PatternExcludesArtifactFilter( excludes ) ); |
453 | |
} |
454 | |
|
455 | 0 | if ( !actTransitively ) |
456 | |
{ |
457 | 0 | andFilter.add( new DirectDependencyFilter( project.getArtifact(), dependencyArtifacts ) ); |
458 | |
} |
459 | |
|
460 | 0 | return andFilter; |
461 | |
} |
462 | |
|
463 | |
|
464 | |
|
465 | |
|
466 | |
|
467 | |
|
468 | |
|
469 | |
private List<String> parseIncludes( String include ) |
470 | |
{ |
471 | 0 | List<String> includes = new ArrayList<String>(); |
472 | |
|
473 | 0 | if ( include != null ) |
474 | |
{ |
475 | 0 | String[] elements = include.split( "," ); |
476 | 0 | includes.addAll( Arrays.asList( elements ) ); |
477 | |
} |
478 | |
|
479 | 0 | return includes; |
480 | |
} |
481 | |
|
482 | |
private Set<Artifact> getFilteredResolvedArtifacts( MavenProject project, Set<Artifact> artifacts, |
483 | |
ArtifactFilter filter ) |
484 | |
{ |
485 | |
try |
486 | |
{ |
487 | 0 | ArtifactResolutionResult result = |
488 | |
resolver.resolveTransitively( artifacts, project.getArtifact(), localRepository, remoteRepositories, |
489 | |
metadataSource, filter ); |
490 | |
|
491 | |
@SuppressWarnings( "unchecked" ) |
492 | 0 | Set<Artifact> resolvedArtifacts = result.getArtifacts(); |
493 | |
|
494 | 0 | return resolvedArtifacts; |
495 | |
} |
496 | 0 | catch ( ArtifactResolutionException e ) |
497 | |
{ |
498 | 0 | getLog().info( "Unable to resolve all dependencies for : " + e.getGroupId() + ":" + e.getArtifactId() + ":" |
499 | |
+ e.getVersion() |
500 | |
+ ". Falling back to non-transitive mode for initial artifact resolution." ); |
501 | |
} |
502 | 0 | catch ( ArtifactNotFoundException e ) |
503 | |
{ |
504 | 0 | getLog().info( "Unable to resolve all dependencies for : " + e.getGroupId() + ":" + e.getArtifactId() + ":" |
505 | |
+ e.getVersion() |
506 | |
+ ". Falling back to non-transitive mode for initial artifact resolution." ); |
507 | 0 | } |
508 | |
|
509 | 0 | Set<Artifact> resolvedArtifacts = new LinkedHashSet<Artifact>(); |
510 | |
|
511 | |
|
512 | 0 | for ( Artifact artifact : artifacts ) |
513 | |
{ |
514 | 0 | if ( filter.include( artifact ) ) |
515 | |
{ |
516 | |
try |
517 | |
{ |
518 | 0 | resolvedArtifacts.add( artifact ); |
519 | 0 | resolver.resolve( artifact, remoteRepositories, localRepository ); |
520 | |
} |
521 | 0 | catch ( ArtifactResolutionException e ) |
522 | |
{ |
523 | 0 | getLog().debug( "Unable to resolve artifact: " + artifact ); |
524 | |
} |
525 | 0 | catch ( ArtifactNotFoundException e ) |
526 | |
{ |
527 | 0 | getLog().debug( "Unable to resolve artifact: " + artifact ); |
528 | 0 | } |
529 | |
} |
530 | |
} |
531 | 0 | return resolvedArtifacts; |
532 | |
} |
533 | |
|
534 | |
private void purgeArtifacts( Set<Artifact> artifacts ) |
535 | |
throws MojoFailureException |
536 | |
{ |
537 | 0 | for ( Artifact artifact : artifacts ) |
538 | |
{ |
539 | 0 | verbose( "Purging artifact: " + artifact.getId() ); |
540 | |
|
541 | 0 | File deleteTarget = findDeleteTarget( artifact ); |
542 | |
|
543 | 0 | verbose( "Deleting: " + deleteTarget ); |
544 | |
|
545 | 0 | if ( deleteTarget.isDirectory() ) |
546 | |
{ |
547 | |
try |
548 | |
{ |
549 | 0 | FileUtils.deleteDirectory( deleteTarget ); |
550 | |
} |
551 | 0 | catch ( IOException e ) |
552 | |
{ |
553 | 0 | getLog().warn( "Unable to purge local repository location: " + deleteTarget, e ); |
554 | 0 | } |
555 | |
} |
556 | |
else |
557 | |
{ |
558 | 0 | if ( !deleteTarget.delete() ) |
559 | |
{ |
560 | 0 | getLog().warn( "Unable to purge local repository location: " + deleteTarget ); |
561 | |
} |
562 | |
} |
563 | 0 | artifact.setResolved( false ); |
564 | 0 | } |
565 | 0 | } |
566 | |
|
567 | |
private void reResolveArtifacts( MavenProject project, Set<Artifact> artifacts, ArtifactFilter filter ) |
568 | |
throws ArtifactResolutionException, ArtifactNotFoundException |
569 | |
{ |
570 | |
|
571 | |
|
572 | |
|
573 | 0 | for ( Artifact artifact : artifacts ) |
574 | |
{ |
575 | |
try |
576 | |
{ |
577 | 0 | Artifact pomArtifact = |
578 | |
factory.createArtifact( artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), |
579 | |
null, "pom" ); |
580 | 0 | resolver.resolveAlways( pomArtifact, remoteRepositories, localRepository ); |
581 | |
} |
582 | 0 | catch ( ArtifactResolutionException e ) |
583 | |
{ |
584 | 0 | verbose( e.getMessage() ); |
585 | |
} |
586 | 0 | catch ( ArtifactNotFoundException e ) |
587 | |
{ |
588 | 0 | verbose( e.getMessage() ); |
589 | 0 | } |
590 | |
} |
591 | |
|
592 | 0 | List<Artifact> missingArtifacts = new ArrayList<Artifact>(); |
593 | |
|
594 | 0 | for ( Artifact artifact : artifacts ) |
595 | |
{ |
596 | 0 | verbose( "Resolving artifact: " + artifact.getId() ); |
597 | |
|
598 | |
try |
599 | |
{ |
600 | 0 | resolver.resolveAlways( artifact, project.getRemoteArtifactRepositories(), localRepository ); |
601 | |
} |
602 | 0 | catch ( ArtifactResolutionException e ) |
603 | |
{ |
604 | 0 | verbose( e.getMessage() ); |
605 | 0 | missingArtifacts.add( artifact ); |
606 | |
} |
607 | 0 | catch ( ArtifactNotFoundException e ) |
608 | |
{ |
609 | 0 | verbose( e.getMessage() ); |
610 | 0 | missingArtifacts.add( artifact ); |
611 | 0 | } |
612 | |
} |
613 | |
|
614 | 0 | if ( missingArtifacts.size() > 0 ) |
615 | |
{ |
616 | 0 | String message = "required artifacts missing:\n"; |
617 | 0 | for ( Artifact missingArtifact : missingArtifacts ) |
618 | |
{ |
619 | 0 | message += " " + missingArtifact.getId() + "\n"; |
620 | |
} |
621 | 0 | message += "\nfor the artifact:"; |
622 | |
|
623 | 0 | throw new ArtifactResolutionException( message, project.getArtifact(), |
624 | |
project.getRemoteArtifactRepositories() ); |
625 | |
} |
626 | 0 | } |
627 | |
|
628 | |
private File findDeleteTarget( Artifact artifact ) |
629 | |
{ |
630 | |
|
631 | 0 | File deleteTarget = new File( localRepository.getBasedir(), localRepository.pathOf( artifact ) ); |
632 | |
|
633 | 0 | if ( GROUP_ID_FUZZINESS.equals( resolutionFuzziness ) ) |
634 | |
{ |
635 | |
|
636 | 0 | deleteTarget = deleteTarget.getParentFile().getParentFile().getParentFile(); |
637 | |
} |
638 | 0 | else if ( ARTIFACT_ID_FUZZINESS.equals( resolutionFuzziness ) ) |
639 | |
{ |
640 | |
|
641 | 0 | deleteTarget = deleteTarget.getParentFile().getParentFile(); |
642 | |
} |
643 | 0 | else if ( VERSION_FUZZINESS.equals( resolutionFuzziness ) ) |
644 | |
{ |
645 | |
|
646 | 0 | deleteTarget = deleteTarget.getParentFile(); |
647 | |
} |
648 | |
|
649 | 0 | return deleteTarget; |
650 | |
} |
651 | |
|
652 | |
private void verbose( String message ) |
653 | |
{ |
654 | 0 | if ( verbose || getLog().isDebugEnabled() ) |
655 | |
{ |
656 | 0 | getLog().info( message ); |
657 | |
} |
658 | 0 | } |
659 | |
|
660 | |
} |