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