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 | |
|
219 | |
|
220 | |
|
221 | |
|
222 | |
private String finalName; |
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
|
228 | |
|
229 | |
|
230 | |
|
231 | |
|
232 | |
private String shadedArtifactId; |
233 | |
|
234 | |
|
235 | |
|
236 | |
|
237 | |
|
238 | |
|
239 | |
|
240 | |
private String shadedGroupFilter; |
241 | |
|
242 | |
|
243 | |
|
244 | |
|
245 | |
|
246 | |
|
247 | |
|
248 | |
|
249 | |
private boolean shadedArtifactAttached; |
250 | |
|
251 | |
|
252 | |
|
253 | |
|
254 | |
|
255 | |
|
256 | |
|
257 | |
|
258 | |
|
259 | |
private boolean createDependencyReducedPom; |
260 | |
|
261 | |
|
262 | |
|
263 | |
|
264 | |
|
265 | |
|
266 | |
|
267 | |
private boolean keepDependenciesWithProvidedScope; |
268 | |
|
269 | |
|
270 | |
|
271 | |
|
272 | |
|
273 | |
|
274 | |
|
275 | |
|
276 | |
private boolean promoteTransitiveDependencies; |
277 | |
|
278 | |
|
279 | |
|
280 | |
|
281 | |
|
282 | |
|
283 | |
private String shadedClassifierName; |
284 | |
|
285 | |
|
286 | |
|
287 | |
|
288 | |
|
289 | |
|
290 | |
private boolean createSourcesJar; |
291 | |
|
292 | |
|
293 | |
|
294 | |
public void execute() |
295 | |
throws MojoExecutionException |
296 | |
{ |
297 | 0 | Set artifacts = new LinkedHashSet(); |
298 | 0 | Set artifactIds = new LinkedHashSet(); |
299 | 0 | Set sourceArtifacts = new LinkedHashSet(); |
300 | |
|
301 | 0 | if ( project.getArtifact().getFile() == null ) |
302 | |
{ |
303 | 0 | getLog().error( "The project main artifact does not exist. This could have the following" ); |
304 | 0 | getLog().error( "reasons:" ); |
305 | 0 | getLog().error( "- You have invoked the goal directly from the command line. This is not" ); |
306 | 0 | getLog().error( " supported. Please add the goal to the default lifecycle via an" ); |
307 | 0 | getLog().error( " <execution> element in your POM and use \"mvn package\" to have it run." ); |
308 | 0 | getLog().error( "- You have bound the goal to a lifecycle phase before \"package\". Please" ); |
309 | 0 | getLog().error( " remove this binding from your POM such that the goal will be run in" ); |
310 | 0 | getLog().error( " the proper phase." ); |
311 | 0 | throw new MojoExecutionException( "Failed to create shaded artifact.", |
312 | |
new IllegalStateException( "Project main artifact does not exist." ) ); |
313 | |
} |
314 | 0 | artifacts.add( project.getArtifact().getFile() ); |
315 | |
|
316 | 0 | if ( createSourcesJar ) |
317 | |
{ |
318 | 0 | File file = shadedSourcesArtifactFile(); |
319 | 0 | if ( file.exists() ) |
320 | |
{ |
321 | 0 | sourceArtifacts.add( file ); |
322 | |
} |
323 | |
} |
324 | |
|
325 | 0 | for ( Iterator it = project.getArtifacts().iterator(); it.hasNext(); ) |
326 | |
{ |
327 | 0 | Artifact artifact = (Artifact) it.next(); |
328 | |
|
329 | 0 | if ( excludeArtifact( artifact ) ) |
330 | |
{ |
331 | 0 | getLog().info( "Excluding " + artifact.getId() + " from the shaded jar." ); |
332 | |
|
333 | 0 | continue; |
334 | |
} |
335 | |
|
336 | 0 | if ( artifact.getType().equals( "pom" ) ) |
337 | |
{ |
338 | 0 | getLog().info( "Skipping pom dependency " + artifact.getId() + " in the shaded jar." ); |
339 | 0 | continue; |
340 | |
} |
341 | |
|
342 | 0 | getLog().info( "Including " + artifact.getId() + " in the shaded jar." ); |
343 | |
|
344 | 0 | artifacts.add( artifact.getFile() ); |
345 | |
|
346 | 0 | artifactIds.add( getId( artifact ) ); |
347 | |
|
348 | 0 | if ( createSourcesJar ) |
349 | |
{ |
350 | 0 | File file = resolveArtifactSources( artifact ); |
351 | 0 | if ( file != null ) |
352 | |
{ |
353 | 0 | sourceArtifacts.add( file ); |
354 | |
} |
355 | |
} |
356 | 0 | } |
357 | |
|
358 | |
|
359 | 0 | File outputJar = shadedArtifactFileWithClassifier(); |
360 | 0 | File sourcesJar = shadedSourceArtifactFileWithClassifier(); |
361 | |
|
362 | |
|
363 | |
try |
364 | |
{ |
365 | 0 | List filters = getFilters(); |
366 | |
|
367 | 0 | List relocators = getRelocators(); |
368 | |
|
369 | 0 | List resourceTransformers = getResourceTransformers(); |
370 | |
|
371 | 0 | shader.shade( artifacts, outputJar, filters, relocators, resourceTransformers ); |
372 | |
|
373 | 0 | if ( createSourcesJar ) |
374 | |
{ |
375 | 0 | shader.shade( sourceArtifacts, sourcesJar, filters, relocators, resourceTransformers ); |
376 | |
} |
377 | |
|
378 | 0 | boolean renamed=false; |
379 | |
|
380 | |
|
381 | |
|
382 | |
|
383 | 0 | if ( finalName != null && finalName.length() > 0 && |
384 | |
!finalName.equals( project.getBuild().getFinalName() ) ) |
385 | |
{ |
386 | 0 | String finalFileName = finalName + "." + project.getArtifact().getArtifactHandler().getExtension(); |
387 | 0 | File finalFile = new File( outputDirectory, finalFileName ); |
388 | 0 | replaceFile( finalFile, outputJar ); |
389 | 0 | outputJar = finalFile; |
390 | |
|
391 | 0 | renamed=true; |
392 | |
} |
393 | |
|
394 | 0 | if ( shadedArtifactAttached ) |
395 | |
{ |
396 | 0 | getLog().info( "Attaching shaded artifact." ); |
397 | 0 | projectHelper.attachArtifact( project, project.getArtifact().getType(), shadedClassifierName, outputJar ); |
398 | 0 | if ( createSourcesJar ) |
399 | |
{ |
400 | 0 | projectHelper.attachArtifact( project, "jar", shadedClassifierName + "-sources", sourcesJar ); |
401 | |
} |
402 | |
} |
403 | 0 | else if ( !renamed ) |
404 | |
{ |
405 | 0 | getLog().info( "Replacing original artifact with shaded artifact." ); |
406 | 0 | File originalArtifact = project.getArtifact().getFile(); |
407 | 0 | replaceFile( originalArtifact, outputJar ); |
408 | |
|
409 | 0 | if ( createSourcesJar ) |
410 | |
{ |
411 | 0 | File shadedSources = shadedSourcesArtifactFile(); |
412 | |
|
413 | 0 | replaceFile( shadedSources, sourcesJar ); |
414 | |
|
415 | 0 | projectHelper.attachArtifact( project, "jar", |
416 | |
"sources", shadedSources ); |
417 | |
} |
418 | |
|
419 | 0 | if ( createDependencyReducedPom ) |
420 | |
{ |
421 | 0 | createDependencyReducedPom( artifactIds ); |
422 | |
} |
423 | |
} |
424 | |
} |
425 | 0 | catch ( Exception e ) |
426 | |
{ |
427 | 0 | throw new MojoExecutionException( "Error creating shaded jar.", e ); |
428 | 0 | } |
429 | 0 | } |
430 | |
|
431 | |
private void replaceFile( File oldFile, File newFile ) throws MojoExecutionException |
432 | |
{ |
433 | 0 | getLog().info( "Replacing " + oldFile + " with " + newFile ); |
434 | |
|
435 | 0 | File origFile = new File( outputDirectory, "original-" + oldFile.getName() ); |
436 | 0 | if ( oldFile.exists() && !oldFile.renameTo( origFile ) ) |
437 | |
{ |
438 | |
|
439 | 0 | System.gc(); |
440 | 0 | System.gc(); |
441 | |
|
442 | 0 | if ( !oldFile.renameTo( origFile ) ) |
443 | |
{ |
444 | |
|
445 | |
try |
446 | |
{ |
447 | 0 | FileOutputStream fout = new FileOutputStream( origFile ); |
448 | 0 | FileInputStream fin = new FileInputStream( oldFile ); |
449 | |
try |
450 | |
{ |
451 | 0 | IOUtil.copy( fin, fout ); |
452 | |
} |
453 | |
finally |
454 | |
{ |
455 | 0 | IOUtil.close( fin ); |
456 | 0 | IOUtil.close( fout ); |
457 | 0 | } |
458 | |
} |
459 | 0 | catch ( IOException ex ) |
460 | |
{ |
461 | |
|
462 | 0 | getLog().warn( ex ); |
463 | 0 | } |
464 | |
} |
465 | |
} |
466 | 0 | if ( !newFile.renameTo( oldFile ) ) |
467 | |
{ |
468 | |
|
469 | 0 | System.gc(); |
470 | 0 | System.gc(); |
471 | |
|
472 | 0 | if ( !newFile.renameTo( oldFile ) ) |
473 | |
{ |
474 | |
|
475 | |
try |
476 | |
{ |
477 | 0 | FileOutputStream fout = new FileOutputStream( oldFile ); |
478 | 0 | FileInputStream fin = new FileInputStream( newFile ); |
479 | |
try |
480 | |
{ |
481 | 0 | IOUtil.copy( fin, fout ); |
482 | |
} |
483 | |
finally |
484 | |
{ |
485 | 0 | IOUtil.close( fin ); |
486 | 0 | IOUtil.close( fout ); |
487 | 0 | } |
488 | |
} |
489 | 0 | catch ( IOException ex ) |
490 | |
{ |
491 | 0 | throw new MojoExecutionException( "Could not replace original artifact with shaded artifact!", ex ); |
492 | 0 | } |
493 | |
} |
494 | |
} |
495 | 0 | } |
496 | |
|
497 | |
private File resolveArtifactSources( Artifact artifact ) |
498 | |
{ |
499 | |
|
500 | 0 | Artifact resolvedArtifact = |
501 | |
artifactFactory.createArtifactWithClassifier( artifact.getGroupId(), |
502 | |
artifact.getArtifactId(), |
503 | |
artifact.getVersion(), |
504 | |
"java-source", |
505 | |
"sources" ); |
506 | |
|
507 | |
try |
508 | |
{ |
509 | 0 | artifactResolver.resolve( resolvedArtifact, remoteArtifactRepositories, localRepository ); |
510 | |
} |
511 | 0 | catch ( ArtifactNotFoundException e ) |
512 | |
{ |
513 | |
|
514 | |
} |
515 | 0 | catch ( ArtifactResolutionException e ) |
516 | |
{ |
517 | 0 | getLog().warn( "Could not get sources for " + artifact ); |
518 | 0 | } |
519 | |
|
520 | 0 | if ( resolvedArtifact.isResolved() ) |
521 | |
{ |
522 | 0 | return resolvedArtifact.getFile(); |
523 | |
} |
524 | 0 | return null; |
525 | |
} |
526 | |
|
527 | |
private boolean excludeArtifact( Artifact artifact ) |
528 | |
{ |
529 | 0 | String id = getId( artifact ); |
530 | |
|
531 | |
|
532 | |
|
533 | 0 | if ( artifactSet != null && ( artifactSet.getExcludes() == null && artifactSet.getIncludes() != null ) |
534 | |
&& !includedArtifacts().contains( id ) ) |
535 | |
{ |
536 | 0 | return true; |
537 | |
} |
538 | |
|
539 | 0 | if ( excludedArtifacts().contains( id ) ) |
540 | |
{ |
541 | 0 | return true; |
542 | |
} |
543 | |
|
544 | 0 | if ( shadedGroupFilter != null && !artifact.getGroupId().startsWith( shadedGroupFilter ) ) |
545 | |
{ |
546 | 0 | return true; |
547 | |
} |
548 | |
|
549 | 0 | return false; |
550 | |
} |
551 | |
|
552 | |
private Set excludedArtifacts() |
553 | |
{ |
554 | 0 | if ( artifactSet != null && artifactSet.getExcludes() != null ) |
555 | |
{ |
556 | 0 | return artifactSet.getExcludes(); |
557 | |
} |
558 | |
|
559 | 0 | return Collections.EMPTY_SET; |
560 | |
} |
561 | |
|
562 | |
private Set includedArtifacts() |
563 | |
{ |
564 | 0 | if ( artifactSet != null && artifactSet.getIncludes() != null ) |
565 | |
{ |
566 | 0 | return artifactSet.getIncludes(); |
567 | |
} |
568 | |
|
569 | 0 | return Collections.EMPTY_SET; |
570 | |
} |
571 | |
|
572 | |
private List getRelocators() |
573 | |
{ |
574 | 0 | List relocators = new ArrayList(); |
575 | |
|
576 | 0 | if ( relocations == null ) |
577 | |
{ |
578 | 0 | return relocators; |
579 | |
} |
580 | |
|
581 | 0 | for ( int i = 0; i < relocations.length; i++ ) |
582 | |
{ |
583 | 0 | PackageRelocation r = relocations[i]; |
584 | |
|
585 | 0 | relocators.add( new SimpleRelocator( r.getPattern(), r.getShadedPattern(), r.getExcludes() ) ); |
586 | |
} |
587 | |
|
588 | 0 | return relocators; |
589 | |
} |
590 | |
|
591 | |
private List getResourceTransformers() |
592 | |
{ |
593 | 0 | if ( transformers == null ) |
594 | |
{ |
595 | 0 | return Collections.EMPTY_LIST; |
596 | |
} |
597 | |
|
598 | 0 | return Arrays.asList( transformers ); |
599 | |
} |
600 | |
|
601 | |
private List getFilters() |
602 | |
{ |
603 | 0 | List filters = new ArrayList(); |
604 | |
|
605 | 0 | if ( this.filters == null ) |
606 | |
{ |
607 | 0 | return filters; |
608 | |
} |
609 | |
|
610 | 0 | Map artifacts = new HashMap(); |
611 | |
|
612 | 0 | artifacts.put( getId( project.getArtifact() ), project.getArtifact().getFile() ); |
613 | |
|
614 | 0 | for ( Iterator it = project.getArtifacts().iterator(); it.hasNext(); ) |
615 | |
{ |
616 | 0 | Artifact artifact = (Artifact) it.next(); |
617 | |
|
618 | 0 | artifacts.put( getId( artifact ), artifact.getFile() ); |
619 | 0 | } |
620 | |
|
621 | 0 | for ( int i = 0; i < this.filters.length; i++ ) |
622 | |
{ |
623 | 0 | ArchiveFilter f = this.filters[i]; |
624 | |
|
625 | 0 | File jar = (File) artifacts.get( f.getArtifact() ); |
626 | |
|
627 | 0 | if ( jar == null ) |
628 | |
{ |
629 | 0 | getLog().info( "No artifact matching filter " + f.getArtifact() ); |
630 | |
|
631 | 0 | continue; |
632 | |
} |
633 | |
|
634 | 0 | filters.add( new SimpleFilter( jar, f.getIncludes(), f.getExcludes() ) ); |
635 | |
|
636 | |
} |
637 | |
|
638 | 0 | return filters; |
639 | |
} |
640 | |
|
641 | |
private File shadedArtifactFileWithClassifier() |
642 | |
{ |
643 | 0 | Artifact artifact = project.getArtifact(); |
644 | 0 | final String shadedName = |
645 | |
shadedArtifactId + "-" + artifact.getVersion() + "-" + shadedClassifierName + "." |
646 | |
+ artifact.getArtifactHandler().getExtension(); |
647 | 0 | return new File( outputDirectory, shadedName ); |
648 | |
} |
649 | |
|
650 | |
private File shadedSourceArtifactFileWithClassifier() |
651 | |
{ |
652 | 0 | Artifact artifact = project.getArtifact(); |
653 | 0 | final String shadedName = |
654 | |
shadedArtifactId + "-" + artifact.getVersion() + "-" + shadedClassifierName + "-sources." |
655 | |
+ artifact.getArtifactHandler().getExtension(); |
656 | 0 | return new File( outputDirectory, shadedName ); |
657 | |
} |
658 | |
|
659 | |
private File shadedSourcesArtifactFile() |
660 | |
{ |
661 | 0 | Artifact artifact = project.getArtifact(); |
662 | |
|
663 | |
String shadedName; |
664 | |
|
665 | 0 | if ( project.getBuild().getFinalName() != null ) |
666 | |
{ |
667 | 0 | shadedName = project.getBuild().getFinalName() + "-sources." + artifact.getArtifactHandler().getExtension(); |
668 | |
} |
669 | |
else |
670 | |
{ |
671 | 0 | shadedName = shadedArtifactId + "-" + artifact.getVersion() + "-sources." |
672 | |
+ artifact.getArtifactHandler().getExtension(); |
673 | |
} |
674 | |
|
675 | 0 | return new File( outputDirectory, shadedName ); |
676 | |
} |
677 | |
|
678 | |
|
679 | |
|
680 | |
private void createDependencyReducedPom( Set artifactsToRemove ) |
681 | |
throws IOException, DependencyTreeBuilderException, ProjectBuildingException |
682 | |
{ |
683 | 0 | Model model = project.getOriginalModel(); |
684 | 0 | List dependencies = new ArrayList(); |
685 | |
|
686 | 0 | boolean modified = false; |
687 | |
|
688 | 0 | List transitiveDeps = new ArrayList(); |
689 | |
|
690 | 0 | for ( Iterator it = project.getArtifacts().iterator(); it.hasNext(); ) |
691 | |
{ |
692 | 0 | Artifact artifact = (Artifact) it.next(); |
693 | |
|
694 | |
|
695 | 0 | Dependency dep = new Dependency(); |
696 | 0 | dep.setArtifactId( artifact.getArtifactId() ); |
697 | 0 | if ( artifact.hasClassifier() ) |
698 | |
{ |
699 | 0 | dep.setClassifier( artifact.getClassifier() ); |
700 | |
} |
701 | 0 | dep.setGroupId( artifact.getGroupId() ); |
702 | 0 | dep.setOptional( artifact.isOptional() ); |
703 | 0 | dep.setScope( artifact.getScope() ); |
704 | 0 | dep.setType( artifact.getType() ); |
705 | 0 | dep.setVersion( artifact.getVersion() ); |
706 | |
|
707 | |
|
708 | |
|
709 | 0 | transitiveDeps.add( dep ); |
710 | 0 | } |
711 | 0 | List origDeps = project.getDependencies(); |
712 | |
|
713 | 0 | if ( promoteTransitiveDependencies ) |
714 | |
{ |
715 | 0 | origDeps = transitiveDeps; |
716 | |
} |
717 | |
|
718 | 0 | for ( Iterator i = origDeps.iterator(); i.hasNext(); ) |
719 | |
{ |
720 | 0 | Dependency d = (Dependency) i.next(); |
721 | |
|
722 | 0 | dependencies.add( d ); |
723 | |
|
724 | 0 | String id = getId( d ); |
725 | |
|
726 | 0 | if ( artifactsToRemove.contains( id ) ) |
727 | |
{ |
728 | 0 | modified = true; |
729 | |
|
730 | 0 | if ( keepDependenciesWithProvidedScope ) |
731 | |
{ |
732 | 0 | d.setScope( "provided" ); |
733 | |
} |
734 | |
else |
735 | |
{ |
736 | 0 | dependencies.remove( d ); |
737 | |
} |
738 | |
} |
739 | 0 | } |
740 | |
|
741 | |
|
742 | 0 | if ( modified ) |
743 | |
{ |
744 | 0 | while ( modified ) |
745 | |
{ |
746 | |
|
747 | 0 | model.setDependencies( dependencies ); |
748 | |
|
749 | |
|
750 | |
|
751 | |
|
752 | |
|
753 | 0 | File f = new File( project.getBasedir(), "dependency-reduced-pom.xml" ); |
754 | 0 | if ( f.exists() ) |
755 | |
{ |
756 | 0 | f.delete(); |
757 | |
} |
758 | |
|
759 | 0 | Writer w = WriterFactory.newXmlWriter( f ); |
760 | |
|
761 | |
try |
762 | |
{ |
763 | 0 | PomWriter.write( w, model, true ); |
764 | |
} |
765 | |
finally |
766 | |
{ |
767 | 0 | w.close(); |
768 | 0 | } |
769 | |
|
770 | 0 | MavenProject p2 = mavenProjectBuilder.build( f, localRepository, null ); |
771 | 0 | modified = updateExcludesInDeps( p2, dependencies, transitiveDeps ); |
772 | |
|
773 | 0 | } |
774 | |
|
775 | |
|
776 | |
|
777 | |
|
778 | |
|
779 | |
|
780 | |
|
781 | 0 | File f = new File( project.getBasedir(), "dependency-reduced-pom.xml" ); |
782 | 0 | File f2 = new File( outputDirectory, "dependency-reduced-pom.xml" ); |
783 | 0 | FileUtils.copyFile( f, f2 ); |
784 | 0 | FileUtils.forceDeleteOnExit( f ); |
785 | 0 | project.setFile( f ); |
786 | |
} |
787 | 0 | } |
788 | |
|
789 | |
private String getId( Artifact artifact ) |
790 | |
{ |
791 | 0 | return getId( artifact.getGroupId(), artifact.getArtifactId(), artifact.getClassifier() ); |
792 | |
} |
793 | |
|
794 | |
private String getId( Dependency dependency ) |
795 | |
{ |
796 | 0 | return getId( dependency.getGroupId(), dependency.getArtifactId(), dependency.getClassifier() ); |
797 | |
} |
798 | |
|
799 | |
private String getId( String groupId, String artifactId, String classifier ) |
800 | |
{ |
801 | 0 | if ( classifier == null || "jar".equals( classifier ) ) |
802 | |
{ |
803 | 0 | return groupId + ":" + artifactId; |
804 | |
} |
805 | |
else |
806 | |
{ |
807 | 0 | return groupId + ":" + artifactId + ":" + classifier; |
808 | |
} |
809 | |
} |
810 | |
|
811 | |
public boolean updateExcludesInDeps( MavenProject project, |
812 | |
List dependencies, |
813 | |
List transitiveDeps ) |
814 | |
throws DependencyTreeBuilderException |
815 | |
{ |
816 | 0 | DependencyNode node = dependencyTreeBuilder.buildDependencyTree( |
817 | |
project, |
818 | |
localRepository, |
819 | |
artifactFactory, |
820 | |
artifactMetadataSource, |
821 | |
null, |
822 | |
artifactCollector ); |
823 | 0 | boolean modified = false; |
824 | 0 | Iterator it = node.getChildren().listIterator(); |
825 | 0 | while ( it.hasNext() ) |
826 | |
{ |
827 | 0 | DependencyNode n2 = (DependencyNode) it.next(); |
828 | 0 | Iterator it2 = n2.getChildren().listIterator(); |
829 | 0 | while ( it2.hasNext() ) |
830 | |
{ |
831 | 0 | DependencyNode n3 = (DependencyNode) it2.next(); |
832 | |
|
833 | |
|
834 | |
|
835 | 0 | if ( n3.getState() == DependencyNode.INCLUDED ) |
836 | |
{ |
837 | |
|
838 | |
|
839 | |
|
840 | |
|
841 | |
|
842 | |
|
843 | 0 | boolean found = false; |
844 | 0 | for ( int x = 0; x < transitiveDeps.size(); x++ ) |
845 | |
{ |
846 | 0 | Dependency dep = (Dependency) transitiveDeps.get( x ); |
847 | 0 | if ( dep.getArtifactId().equals( n3.getArtifact().getArtifactId() ) |
848 | |
&& dep.getGroupId().equals( n3.getArtifact().getGroupId() ) ) |
849 | |
{ |
850 | 0 | found = true; |
851 | |
} |
852 | |
|
853 | |
} |
854 | |
|
855 | 0 | if ( !found ) |
856 | |
{ |
857 | 0 | for ( int x = 0; x < dependencies.size(); x++ ) |
858 | |
{ |
859 | 0 | Dependency dep = (Dependency) dependencies.get( x ); |
860 | 0 | if ( dep.getArtifactId().equals( n2.getArtifact().getArtifactId() ) |
861 | |
&& dep.getGroupId().equals( n2.getArtifact().getGroupId() ) ) |
862 | |
{ |
863 | 0 | Exclusion exclusion = new Exclusion(); |
864 | 0 | exclusion.setArtifactId( n3.getArtifact().getArtifactId() ); |
865 | 0 | exclusion.setGroupId( n3.getArtifact().getGroupId() ); |
866 | 0 | dep.addExclusion( exclusion ); |
867 | 0 | modified = true; |
868 | 0 | break; |
869 | |
} |
870 | |
} |
871 | |
} |
872 | |
} |
873 | 0 | } |
874 | 0 | } |
875 | 0 | return modified; |
876 | |
} |
877 | |
} |