1 | |
package org.apache.maven.plugins.shade.mojo; |
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.FileInputStream; |
24 | |
import java.io.FileOutputStream; |
25 | |
import java.io.IOException; |
26 | |
import java.io.Writer; |
27 | |
import java.util.ArrayList; |
28 | |
import java.util.Arrays; |
29 | |
import java.util.Collections; |
30 | |
import java.util.HashMap; |
31 | |
import java.util.Iterator; |
32 | |
import java.util.LinkedHashSet; |
33 | |
import java.util.List; |
34 | |
import java.util.Map; |
35 | |
import java.util.Set; |
36 | |
|
37 | |
import org.apache.maven.artifact.Artifact; |
38 | |
import org.apache.maven.artifact.factory.ArtifactFactory; |
39 | |
import org.apache.maven.artifact.metadata.ArtifactMetadataSource; |
40 | |
import org.apache.maven.artifact.repository.ArtifactRepository; |
41 | |
import org.apache.maven.artifact.resolver.ArtifactCollector; |
42 | |
import org.apache.maven.artifact.resolver.ArtifactNotFoundException; |
43 | |
import org.apache.maven.artifact.resolver.ArtifactResolutionException; |
44 | |
import org.apache.maven.artifact.resolver.ArtifactResolver; |
45 | |
import org.apache.maven.model.Dependency; |
46 | |
import org.apache.maven.model.Exclusion; |
47 | |
import org.apache.maven.model.Model; |
48 | |
import org.apache.maven.plugin.AbstractMojo; |
49 | |
import org.apache.maven.plugin.MojoExecutionException; |
50 | |
import org.apache.maven.plugins.shade.Shader; |
51 | |
import org.apache.maven.plugins.shade.filter.SimpleFilter; |
52 | |
import org.apache.maven.plugins.shade.pom.PomWriter; |
53 | |
import org.apache.maven.plugins.shade.relocation.SimpleRelocator; |
54 | |
import org.apache.maven.plugins.shade.resource.ResourceTransformer; |
55 | |
import org.apache.maven.project.MavenProject; |
56 | |
import org.apache.maven.project.MavenProjectBuilder; |
57 | |
import org.apache.maven.project.MavenProjectHelper; |
58 | |
import org.apache.maven.project.ProjectBuildingException; |
59 | |
import org.apache.maven.shared.dependency.tree.DependencyNode; |
60 | |
import org.apache.maven.shared.dependency.tree.DependencyTreeBuilder; |
61 | |
import org.apache.maven.shared.dependency.tree.DependencyTreeBuilderException; |
62 | |
import org.codehaus.plexus.util.FileUtils; |
63 | |
import org.codehaus.plexus.util.IOUtil; |
64 | |
import org.codehaus.plexus.util.WriterFactory; |
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | 0 | public class ShadeMojo |
78 | |
extends AbstractMojo |
79 | |
{ |
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
private MavenProject project; |
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
private MavenProjectHelper projectHelper; |
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
private Shader shader; |
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
private DependencyTreeBuilder dependencyTreeBuilder; |
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
private MavenProjectBuilder mavenProjectBuilder; |
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
private ArtifactMetadataSource artifactMetadataSource; |
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
private ArtifactCollector artifactCollector; |
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
protected List remoteArtifactRepositories; |
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
protected ArtifactRepository localRepository; |
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
protected ArtifactFactory artifactFactory; |
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
protected ArtifactResolver artifactResolver; |
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | |
private ArtifactSet artifactSet; |
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
private PackageRelocation[] relocations; |
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
private ResourceTransformer[] transformers; |
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
private ArchiveFilter[] filters; |
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
private File outputDirectory; |
212 | |
|
213 | |
|
214 | |
|
215 | |
|
216 | |
|
217 | |
|
218 | |
private String finalName; |
219 | |
|
220 | |
|
221 | |
|
222 | |
|
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
|
228 | |
private String shadedArtifactId; |
229 | |
|
230 | |
|
231 | |
|
232 | |
|
233 | |
|
234 | |
|
235 | |
|
236 | |
private String shadedGroupFilter; |
237 | |
|
238 | |
|
239 | |
|
240 | |
|
241 | |
|
242 | |
|
243 | |
|
244 | |
|
245 | |
private boolean shadedArtifactAttached; |
246 | |
|
247 | |
|
248 | |
|
249 | |
|
250 | |
|
251 | |
|
252 | |
|
253 | |
|
254 | |
|
255 | |
private boolean createDependencyReducedPom; |
256 | |
|
257 | |
|
258 | |
|
259 | |
|
260 | |
|
261 | |
|
262 | |
|
263 | |
private boolean keepDependenciesWithProvidedScope; |
264 | |
|
265 | |
|
266 | |
|
267 | |
|
268 | |
|
269 | |
|
270 | |
|
271 | |
|
272 | |
private boolean promoteTransitiveDependencies; |
273 | |
|
274 | |
|
275 | |
|
276 | |
|
277 | |
|
278 | |
|
279 | |
private String shadedClassifierName; |
280 | |
|
281 | |
|
282 | |
|
283 | |
|
284 | |
|
285 | |
|
286 | |
private boolean createSourcesJar; |
287 | |
|
288 | |
|
289 | |
|
290 | |
public void execute() |
291 | |
throws MojoExecutionException |
292 | |
{ |
293 | 0 | Set artifacts = new LinkedHashSet(); |
294 | 0 | Set artifactIds = new LinkedHashSet(); |
295 | 0 | Set sourceArtifacts = new LinkedHashSet(); |
296 | |
|
297 | 0 | if ( project.getArtifact().getFile() == null ) |
298 | |
{ |
299 | 0 | getLog().error( "The project main artifact does not exist. This could have the following" ); |
300 | 0 | getLog().error( "reasons:" ); |
301 | 0 | getLog().error( "- You have invoked the goal directly from the command line. This is not" ); |
302 | 0 | getLog().error( " supported. Please add the goal to the default lifecycle via an" ); |
303 | 0 | getLog().error( " <execution> element in your POM and use \"mvn package\" to have it run." ); |
304 | 0 | getLog().error( "- You have bound the goal to a lifecycle phase before \"package\". Please" ); |
305 | 0 | getLog().error( " remove this binding from your POM such that the goal will be run in" ); |
306 | 0 | getLog().error( " the proper phase." ); |
307 | 0 | throw new MojoExecutionException( "Failed to create shaded artifact.", |
308 | |
new IllegalStateException( "Project main artifact does not exist." ) ); |
309 | |
} |
310 | 0 | artifacts.add( project.getArtifact().getFile() ); |
311 | |
|
312 | 0 | if ( createSourcesJar ) |
313 | |
{ |
314 | 0 | File file = shadedSourcesArtifactFile(); |
315 | 0 | if ( file.exists() ) |
316 | |
{ |
317 | 0 | sourceArtifacts.add( file ); |
318 | |
} |
319 | |
} |
320 | |
|
321 | 0 | for ( Iterator it = project.getArtifacts().iterator(); it.hasNext(); ) |
322 | |
{ |
323 | 0 | Artifact artifact = (Artifact) it.next(); |
324 | |
|
325 | 0 | if ( excludeArtifact( artifact ) ) |
326 | |
{ |
327 | 0 | getLog().info( "Excluding " + artifact.getId() + " from the shaded jar." ); |
328 | |
|
329 | 0 | continue; |
330 | |
} |
331 | |
|
332 | 0 | getLog().info( "Including " + artifact.getId() + " in the shaded jar." ); |
333 | |
|
334 | 0 | artifacts.add( artifact.getFile() ); |
335 | |
|
336 | 0 | artifactIds.add( getId( artifact ) ); |
337 | |
|
338 | 0 | if ( createSourcesJar ) |
339 | |
{ |
340 | 0 | File file = resolveArtifactSources( artifact ); |
341 | 0 | if ( file != null ) |
342 | |
{ |
343 | 0 | sourceArtifacts.add( file ); |
344 | |
} |
345 | |
} |
346 | |
} |
347 | |
|
348 | |
|
349 | 0 | File outputJar = shadedArtifactFileWithClassifier(); |
350 | 0 | File sourcesJar = shadedSourceArtifactFileWithClassifier(); |
351 | |
|
352 | |
|
353 | |
try |
354 | |
{ |
355 | 0 | List filters = getFilters(); |
356 | |
|
357 | 0 | List relocators = getRelocators(); |
358 | |
|
359 | 0 | List resourceTransformers = getResourceTransformers(); |
360 | |
|
361 | 0 | shader.shade( artifacts, outputJar, filters, relocators, resourceTransformers ); |
362 | |
|
363 | 0 | if ( createSourcesJar ) |
364 | |
{ |
365 | 0 | shader.shade( sourceArtifacts, sourcesJar, filters, relocators, resourceTransformers ); |
366 | |
} |
367 | |
|
368 | 0 | if ( shadedArtifactAttached ) |
369 | |
{ |
370 | 0 | getLog().info( "Attaching shaded artifact." ); |
371 | 0 | projectHelper.attachArtifact( project, project.getArtifact().getType(), shadedClassifierName, outputJar ); |
372 | 0 | if ( createSourcesJar ) |
373 | |
{ |
374 | 0 | projectHelper.attachArtifact( project, "jar", shadedClassifierName + "-sources", sourcesJar ); |
375 | |
} |
376 | |
} |
377 | |
|
378 | |
else |
379 | |
{ |
380 | 0 | getLog().info( "Replacing original artifact with shaded artifact." ); |
381 | 0 | File file = shadedArtifactFile(); |
382 | 0 | replaceFile( file, outputJar ); |
383 | |
|
384 | 0 | if ( createSourcesJar ) |
385 | |
{ |
386 | 0 | file = shadedSourcesArtifactFile(); |
387 | |
|
388 | 0 | replaceFile( file, sourcesJar ); |
389 | |
|
390 | 0 | projectHelper.attachArtifact( project, "jar", |
391 | |
"sources", file ); |
392 | |
} |
393 | |
|
394 | 0 | if ( createDependencyReducedPom ) |
395 | |
{ |
396 | 0 | createDependencyReducedPom( artifactIds ); |
397 | |
} |
398 | |
} |
399 | |
} |
400 | 0 | catch ( Exception e ) |
401 | |
{ |
402 | 0 | throw new MojoExecutionException( "Error creating shaded jar.", e ); |
403 | 0 | } |
404 | 0 | } |
405 | |
|
406 | |
private void replaceFile( File oldFile, File newFile ) throws MojoExecutionException |
407 | |
{ |
408 | 0 | getLog().info( "Replacing " + oldFile + " with " + newFile ); |
409 | |
|
410 | 0 | File origFile = new File( outputDirectory, "original-" + oldFile.getName() ); |
411 | 0 | if ( oldFile.exists() && !oldFile.renameTo( origFile ) ) |
412 | |
{ |
413 | |
|
414 | 0 | System.gc(); |
415 | 0 | System.gc(); |
416 | |
|
417 | 0 | if ( !oldFile.renameTo( origFile ) ) |
418 | |
{ |
419 | |
|
420 | |
try |
421 | |
{ |
422 | 0 | FileOutputStream fout = new FileOutputStream( origFile ); |
423 | 0 | FileInputStream fin = new FileInputStream( oldFile ); |
424 | |
try |
425 | |
{ |
426 | 0 | IOUtil.copy( fin, fout ); |
427 | |
} |
428 | |
finally |
429 | |
{ |
430 | 0 | IOUtil.close( fin ); |
431 | 0 | IOUtil.close( fout ); |
432 | 0 | } |
433 | |
} |
434 | 0 | catch ( IOException ex ) |
435 | |
{ |
436 | |
|
437 | 0 | getLog().warn( ex ); |
438 | 0 | } |
439 | |
} |
440 | |
} |
441 | 0 | if ( !newFile.renameTo( oldFile ) ) |
442 | |
{ |
443 | |
|
444 | 0 | System.gc(); |
445 | 0 | System.gc(); |
446 | |
|
447 | 0 | if ( !newFile.renameTo( oldFile ) ) |
448 | |
{ |
449 | |
|
450 | |
try |
451 | |
{ |
452 | 0 | FileOutputStream fout = new FileOutputStream( oldFile ); |
453 | 0 | FileInputStream fin = new FileInputStream( newFile ); |
454 | |
try |
455 | |
{ |
456 | 0 | IOUtil.copy( fin, fout ); |
457 | |
} |
458 | |
finally |
459 | |
{ |
460 | 0 | IOUtil.close( fin ); |
461 | 0 | IOUtil.close( fout ); |
462 | 0 | } |
463 | |
} |
464 | 0 | catch ( IOException ex ) |
465 | |
{ |
466 | 0 | throw new MojoExecutionException( "Could not replace original artifact with shaded artifact!", ex ); |
467 | 0 | } |
468 | |
} |
469 | |
} |
470 | 0 | } |
471 | |
|
472 | |
private File resolveArtifactSources( Artifact artifact ) |
473 | |
{ |
474 | |
|
475 | 0 | Artifact resolvedArtifact = |
476 | |
artifactFactory.createArtifactWithClassifier( artifact.getGroupId(), |
477 | |
artifact.getArtifactId(), |
478 | |
artifact.getVersion(), |
479 | |
"java-source", |
480 | |
"sources" ); |
481 | |
|
482 | |
try |
483 | |
{ |
484 | 0 | artifactResolver.resolve( resolvedArtifact, remoteArtifactRepositories, localRepository ); |
485 | |
} |
486 | 0 | catch ( ArtifactNotFoundException e ) |
487 | |
{ |
488 | |
|
489 | |
} |
490 | 0 | catch ( ArtifactResolutionException e ) |
491 | |
{ |
492 | 0 | getLog().warn( "Could not get sources for " + artifact ); |
493 | 0 | } |
494 | |
|
495 | 0 | if ( resolvedArtifact.isResolved() ) |
496 | |
{ |
497 | 0 | return resolvedArtifact.getFile(); |
498 | |
} |
499 | 0 | return null; |
500 | |
} |
501 | |
|
502 | |
private boolean excludeArtifact( Artifact artifact ) |
503 | |
{ |
504 | 0 | String id = getId( artifact ); |
505 | |
|
506 | |
|
507 | |
|
508 | 0 | if ( artifactSet != null && ( artifactSet.getExcludes() == null && artifactSet.getIncludes() != null ) |
509 | |
&& !includedArtifacts().contains( id ) ) |
510 | |
{ |
511 | 0 | return true; |
512 | |
} |
513 | |
|
514 | 0 | if ( excludedArtifacts().contains( id ) ) |
515 | |
{ |
516 | 0 | return true; |
517 | |
} |
518 | |
|
519 | 0 | if ( shadedGroupFilter != null && !artifact.getGroupId().startsWith( shadedGroupFilter ) ) |
520 | |
{ |
521 | 0 | return true; |
522 | |
} |
523 | |
|
524 | 0 | return false; |
525 | |
} |
526 | |
|
527 | |
private Set excludedArtifacts() |
528 | |
{ |
529 | 0 | if ( artifactSet != null && artifactSet.getExcludes() != null ) |
530 | |
{ |
531 | 0 | return artifactSet.getExcludes(); |
532 | |
} |
533 | |
|
534 | 0 | return Collections.EMPTY_SET; |
535 | |
} |
536 | |
|
537 | |
private Set includedArtifacts() |
538 | |
{ |
539 | 0 | if ( artifactSet != null && artifactSet.getIncludes() != null ) |
540 | |
{ |
541 | 0 | return artifactSet.getIncludes(); |
542 | |
} |
543 | |
|
544 | 0 | return Collections.EMPTY_SET; |
545 | |
} |
546 | |
|
547 | |
private List getRelocators() |
548 | |
{ |
549 | 0 | List relocators = new ArrayList(); |
550 | |
|
551 | 0 | if ( relocations == null ) |
552 | |
{ |
553 | 0 | return relocators; |
554 | |
} |
555 | |
|
556 | 0 | for ( int i = 0; i < relocations.length; i++ ) |
557 | |
{ |
558 | 0 | PackageRelocation r = relocations[i]; |
559 | |
|
560 | 0 | relocators.add( new SimpleRelocator( r.getPattern(), r.getShadedPattern(), r.getExcludes() ) ); |
561 | |
} |
562 | |
|
563 | 0 | return relocators; |
564 | |
} |
565 | |
|
566 | |
private List getResourceTransformers() |
567 | |
{ |
568 | 0 | if ( transformers == null ) |
569 | |
{ |
570 | 0 | return Collections.EMPTY_LIST; |
571 | |
} |
572 | |
|
573 | 0 | return Arrays.asList( transformers ); |
574 | |
} |
575 | |
|
576 | |
private List getFilters() |
577 | |
{ |
578 | 0 | List filters = new ArrayList(); |
579 | |
|
580 | 0 | if ( this.filters == null ) |
581 | |
{ |
582 | 0 | return filters; |
583 | |
} |
584 | |
|
585 | 0 | Map artifacts = new HashMap(); |
586 | |
|
587 | 0 | artifacts.put( getId( project.getArtifact() ), project.getArtifact().getFile() ); |
588 | |
|
589 | 0 | for ( Iterator it = project.getArtifacts().iterator(); it.hasNext(); ) |
590 | |
{ |
591 | 0 | Artifact artifact = (Artifact) it.next(); |
592 | |
|
593 | 0 | artifacts.put( getId( artifact ), artifact.getFile() ); |
594 | |
} |
595 | |
|
596 | 0 | for ( int i = 0; i < this.filters.length; i++ ) |
597 | |
{ |
598 | 0 | ArchiveFilter f = this.filters[i]; |
599 | |
|
600 | 0 | File jar = (File) artifacts.get( f.getArtifact() ); |
601 | |
|
602 | 0 | if ( jar == null ) |
603 | |
{ |
604 | 0 | getLog().info( "No artifact matching filter " + f.getArtifact() ); |
605 | |
|
606 | 0 | continue; |
607 | |
} |
608 | |
|
609 | 0 | filters.add( new SimpleFilter( jar, f.getIncludes(), f.getExcludes() ) ); |
610 | |
|
611 | |
} |
612 | |
|
613 | 0 | return filters; |
614 | |
} |
615 | |
|
616 | |
private File shadedArtifactFileWithClassifier() |
617 | |
{ |
618 | 0 | Artifact artifact = project.getArtifact(); |
619 | 0 | final String shadedName = |
620 | |
shadedArtifactId + "-" + artifact.getVersion() + "-" + shadedClassifierName + "." |
621 | |
+ artifact.getArtifactHandler().getExtension(); |
622 | 0 | return new File( outputDirectory, shadedName ); |
623 | |
} |
624 | |
|
625 | |
private File shadedSourceArtifactFileWithClassifier() |
626 | |
{ |
627 | 0 | Artifact artifact = project.getArtifact(); |
628 | 0 | final String shadedName = |
629 | |
shadedArtifactId + "-" + artifact.getVersion() + "-" + shadedClassifierName + "-sources." |
630 | |
+ artifact.getArtifactHandler().getExtension(); |
631 | 0 | return new File( outputDirectory, shadedName ); |
632 | |
} |
633 | |
|
634 | |
private File shadedArtifactFile() |
635 | |
{ |
636 | 0 | Artifact artifact = project.getArtifact(); |
637 | |
|
638 | |
String shadedName; |
639 | |
|
640 | 0 | if ( finalName != null ) |
641 | |
{ |
642 | 0 | shadedName = finalName + "." + artifact.getArtifactHandler().getExtension(); |
643 | |
} |
644 | |
else |
645 | |
{ |
646 | 0 | shadedName = shadedArtifactId + "-" + artifact.getVersion() + "." |
647 | |
+ artifact.getArtifactHandler().getExtension(); |
648 | |
} |
649 | |
|
650 | 0 | return new File( outputDirectory, shadedName ); |
651 | |
} |
652 | |
|
653 | |
private File shadedSourcesArtifactFile() |
654 | |
{ |
655 | 0 | Artifact artifact = project.getArtifact(); |
656 | |
|
657 | |
String shadedName; |
658 | |
|
659 | 0 | if ( finalName != null ) |
660 | |
{ |
661 | 0 | shadedName = finalName + "-sources." + artifact.getArtifactHandler().getExtension(); |
662 | |
} |
663 | |
else |
664 | |
{ |
665 | 0 | shadedName = shadedArtifactId + "-" + artifact.getVersion() + "-sources." |
666 | |
+ artifact.getArtifactHandler().getExtension(); |
667 | |
} |
668 | |
|
669 | 0 | return new File( outputDirectory, shadedName ); |
670 | |
} |
671 | |
|
672 | |
|
673 | |
|
674 | |
private void createDependencyReducedPom( Set artifactsToRemove ) |
675 | |
throws IOException, DependencyTreeBuilderException, ProjectBuildingException |
676 | |
{ |
677 | 0 | Model model = project.getOriginalModel(); |
678 | 0 | List dependencies = new ArrayList(); |
679 | |
|
680 | 0 | boolean modified = false; |
681 | |
|
682 | 0 | List transitiveDeps = new ArrayList(); |
683 | |
|
684 | 0 | for ( Iterator it = project.getArtifacts().iterator(); it.hasNext(); ) |
685 | |
{ |
686 | 0 | Artifact artifact = (Artifact) it.next(); |
687 | |
|
688 | |
|
689 | 0 | Dependency dep = new Dependency(); |
690 | 0 | dep.setArtifactId( artifact.getArtifactId() ); |
691 | 0 | if ( artifact.hasClassifier() ) |
692 | |
{ |
693 | 0 | dep.setClassifier( artifact.getClassifier() ); |
694 | |
} |
695 | 0 | dep.setGroupId( artifact.getGroupId() ); |
696 | 0 | dep.setOptional( artifact.isOptional() ); |
697 | 0 | dep.setScope( artifact.getScope() ); |
698 | 0 | dep.setType( artifact.getType() ); |
699 | 0 | dep.setVersion( artifact.getVersion() ); |
700 | |
|
701 | |
|
702 | |
|
703 | 0 | transitiveDeps.add( dep ); |
704 | |
} |
705 | 0 | List origDeps = project.getDependencies(); |
706 | |
|
707 | 0 | if ( promoteTransitiveDependencies ) |
708 | |
{ |
709 | 0 | origDeps = transitiveDeps; |
710 | |
} |
711 | |
|
712 | 0 | for ( Iterator i = origDeps.iterator(); i.hasNext(); ) |
713 | |
{ |
714 | 0 | Dependency d = (Dependency) i.next(); |
715 | |
|
716 | 0 | dependencies.add( d ); |
717 | |
|
718 | 0 | String id = d.getGroupId() + ":" + d.getArtifactId(); |
719 | |
|
720 | 0 | if ( artifactsToRemove.contains( id ) ) |
721 | |
{ |
722 | 0 | modified = true; |
723 | |
|
724 | 0 | if ( keepDependenciesWithProvidedScope ) |
725 | |
{ |
726 | 0 | d.setScope( "provided" ); |
727 | |
} |
728 | |
else |
729 | |
{ |
730 | 0 | dependencies.remove( d ); |
731 | |
} |
732 | |
} |
733 | |
} |
734 | |
|
735 | |
|
736 | 0 | if ( modified ) |
737 | |
{ |
738 | 0 | while ( modified ) |
739 | |
{ |
740 | |
|
741 | 0 | model.setDependencies( dependencies ); |
742 | |
|
743 | 0 | File f = new File( outputDirectory, "dependency-reduced-pom.xml" ); |
744 | 0 | if ( f.exists() ) |
745 | |
{ |
746 | 0 | f.delete(); |
747 | |
} |
748 | |
|
749 | 0 | Writer w = WriterFactory.newXmlWriter( f ); |
750 | |
|
751 | 0 | PomWriter.write( w, model, true ); |
752 | |
|
753 | 0 | w.close(); |
754 | |
|
755 | 0 | MavenProject p2 = mavenProjectBuilder.build( f, localRepository, null ); |
756 | 0 | modified = updateExcludesInDeps( p2, dependencies, transitiveDeps ); |
757 | |
|
758 | |
} |
759 | |
|
760 | |
|
761 | |
|
762 | |
|
763 | |
|
764 | |
|
765 | 0 | File f = new File( project.getBasedir(), "dependency-reduced-pom.xml" ); |
766 | 0 | File f2 = new File( outputDirectory, "dependency-reduced-pom.xml" ); |
767 | 0 | if ( f.exists() ) |
768 | |
{ |
769 | 0 | f.delete(); |
770 | |
} |
771 | 0 | FileUtils.copyFile( f2, f ); |
772 | 0 | FileUtils.forceDeleteOnExit( f ); |
773 | 0 | project.setFile( f ); |
774 | |
} |
775 | 0 | } |
776 | |
|
777 | |
private String getId( Artifact artifact ) |
778 | |
{ |
779 | 0 | if ( artifact.getClassifier() == null || "jar".equals( artifact.getClassifier() ) ) |
780 | |
{ |
781 | 0 | return artifact.getGroupId() + ":" + artifact.getArtifactId(); |
782 | |
} |
783 | |
else |
784 | |
{ |
785 | 0 | return artifact.getGroupId() + ":" + artifact.getArtifactId() + ":" + artifact.getClassifier(); |
786 | |
} |
787 | |
} |
788 | |
|
789 | |
|
790 | |
public boolean updateExcludesInDeps( MavenProject project, |
791 | |
List dependencies, |
792 | |
List transitiveDeps ) |
793 | |
throws DependencyTreeBuilderException |
794 | |
{ |
795 | 0 | DependencyNode node = dependencyTreeBuilder.buildDependencyTree( |
796 | |
project, |
797 | |
localRepository, |
798 | |
artifactFactory, |
799 | |
artifactMetadataSource, |
800 | |
null, |
801 | |
artifactCollector ); |
802 | 0 | boolean modified = false; |
803 | 0 | Iterator it = node.getChildren().listIterator(); |
804 | 0 | while ( it.hasNext() ) |
805 | |
{ |
806 | 0 | DependencyNode n2 = (DependencyNode) it.next(); |
807 | 0 | Iterator it2 = n2.getChildren().listIterator(); |
808 | 0 | while ( it2.hasNext() ) |
809 | |
{ |
810 | 0 | DependencyNode n3 = (DependencyNode) it2.next(); |
811 | |
|
812 | |
|
813 | |
|
814 | 0 | if ( n3.getState() == DependencyNode.INCLUDED ) |
815 | |
{ |
816 | |
|
817 | |
|
818 | |
|
819 | |
|
820 | |
|
821 | |
|
822 | 0 | boolean found = false; |
823 | 0 | for ( int x = 0; x < transitiveDeps.size(); x++ ) |
824 | |
{ |
825 | 0 | Dependency dep = (Dependency) transitiveDeps.get( x ); |
826 | 0 | if ( dep.getArtifactId().equals( n3.getArtifact().getArtifactId() ) |
827 | |
&& dep.getGroupId().equals( n3.getArtifact().getGroupId() ) ) |
828 | |
{ |
829 | 0 | found = true; |
830 | |
} |
831 | |
|
832 | |
} |
833 | |
|
834 | 0 | if ( !found ) |
835 | |
{ |
836 | 0 | for ( int x = 0; x < dependencies.size(); x++ ) |
837 | |
{ |
838 | 0 | Dependency dep = (Dependency) dependencies.get( x ); |
839 | 0 | if ( dep.getArtifactId().equals( n2.getArtifact().getArtifactId() ) |
840 | |
&& dep.getGroupId().equals( n2.getArtifact().getGroupId() ) ) |
841 | |
{ |
842 | 0 | Exclusion exclusion = new Exclusion(); |
843 | 0 | exclusion.setArtifactId( n3.getArtifact().getArtifactId() ); |
844 | 0 | exclusion.setGroupId( n3.getArtifact().getGroupId() ); |
845 | 0 | dep.addExclusion( exclusion ); |
846 | 0 | modified = true; |
847 | 0 | break; |
848 | |
} |
849 | |
} |
850 | |
} |
851 | |
} |
852 | |
} |
853 | |
} |
854 | 0 | return modified; |
855 | |
} |
856 | |
} |