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