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