Coverage Report - org.apache.maven.plugins.shade.mojo.ShadeMojo
 
Classes in this File Line Coverage Branch Coverage Complexity
ShadeMojo
0%
0/226
0%
0/120
5,25
 
 1  
 package org.apache.maven.plugins.shade.mojo;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  *   http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 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  
  * Mojo that performs shading delegating to the Shader component.
 68  
  *
 69  
  * @author Jason van Zyl
 70  
  * @author Mauro Talevi
 71  
  * @author David Blevins
 72  
  * @author Hiram Chirino
 73  
  * @goal shade
 74  
  * @phase package
 75  
  * @requiresDependencyResolution runtime
 76  
  */
 77  0
 public class ShadeMojo
 78  
     extends AbstractMojo
 79  
 {
 80  
     /**
 81  
      * @parameter expression="${project}"
 82  
      * @readonly
 83  
      * @required
 84  
      */
 85  
     private MavenProject project;
 86  
 
 87  
     /**
 88  
      * @component
 89  
      * @required
 90  
      * @readonly
 91  
      */
 92  
     private MavenProjectHelper projectHelper;
 93  
 
 94  
     /**
 95  
      * @component
 96  
      * @required
 97  
      * @readonly
 98  
      */
 99  
     private Shader shader;
 100  
 
 101  
     /**
 102  
      * The dependency tree builder to use.
 103  
      *
 104  
      * @component
 105  
      * @required
 106  
      * @readonly
 107  
      */
 108  
     private DependencyTreeBuilder dependencyTreeBuilder;
 109  
 
 110  
     /**
 111  
      * ProjectBuilder, needed to create projects from the artifacts.
 112  
      *
 113  
      * @component role="org.apache.maven.project.MavenProjectBuilder"
 114  
      * @required
 115  
      * @readonly
 116  
      */
 117  
     private MavenProjectBuilder mavenProjectBuilder;
 118  
 
 119  
     /**
 120  
      * The artifact metadata source to use.
 121  
      *
 122  
      * @component
 123  
      * @required
 124  
      * @readonly
 125  
      */
 126  
     private ArtifactMetadataSource artifactMetadataSource;
 127  
 
 128  
     /**
 129  
      * The artifact collector to use.
 130  
      *
 131  
      * @component
 132  
      * @required
 133  
      * @readonly
 134  
      */
 135  
     private ArtifactCollector artifactCollector;
 136  
 
 137  
     /**
 138  
      * Remote repositories which will be searched for source attachments.
 139  
      *
 140  
      * @parameter expression="${project.remoteArtifactRepositories}"
 141  
      * @required
 142  
      * @readonly
 143  
      */
 144  
     protected List remoteArtifactRepositories;
 145  
 
 146  
     /**
 147  
      * Local maven repository.
 148  
      *
 149  
      * @parameter expression="${localRepository}"
 150  
      * @required
 151  
      * @readonly
 152  
      */
 153  
     protected ArtifactRepository localRepository;
 154  
 
 155  
     /**
 156  
      * Artifact factory, needed to download source jars for inclusion in classpath.
 157  
      *
 158  
      * @component role="org.apache.maven.artifact.factory.ArtifactFactory"
 159  
      * @required
 160  
      * @readonly
 161  
      */
 162  
     protected ArtifactFactory artifactFactory;
 163  
 
 164  
     /**
 165  
      * Artifact resolver, needed to download source jars for inclusion in classpath.
 166  
      *
 167  
      * @component role="org.apache.maven.artifact.resolver.ArtifactResolver"
 168  
      * @required
 169  
      * @readonly
 170  
      */
 171  
     protected ArtifactResolver artifactResolver;
 172  
 
 173  
     /**
 174  
      * Artifacts to include/exclude from the final artifact.
 175  
      *
 176  
      * @parameter
 177  
      */
 178  
     private ArtifactSet artifactSet;
 179  
 
 180  
     /**
 181  
      * Packages to be relocated.
 182  
      *
 183  
      * @parameter
 184  
      */
 185  
     private PackageRelocation[] relocations;
 186  
 
 187  
     /**
 188  
      * Resource transformers to be used.
 189  
      *
 190  
      * @parameter
 191  
      */
 192  
     private ResourceTransformer[] transformers;
 193  
 
 194  
     /**
 195  
      * Archive Filters to be used.  Allows you to specify an artifact in the form of
 196  
      * groupId:artifactId and a set of include/exclude file patterns for filtering which
 197  
      * contents of the archive are added to the shaded jar.  From a logical perspective,
 198  
      * includes are processed before excludes, thus it's possible to use an include to
 199  
      * collect a set of files from the archive then use excludes to further reduce the set.
 200  
      * By default, all files are included and no files are excluded.
 201  
      *
 202  
      * @parameter
 203  
      */
 204  
     private ArchiveFilter[] filters;
 205  
 
 206  
     /**
 207  
      * The destination directory for the shaded artifact.
 208  
      *
 209  
      * @parameter default-value="${project.build.directory}"
 210  
      */
 211  
     private File outputDirectory;
 212  
 
 213  
     /**
 214  
      * The name of the shaded artifactId
 215  
      *
 216  
      * @parameter expression="${finalName}"
 217  
      */
 218  
     private String finalName;
 219  
 
 220  
     /**
 221  
      * The name of the shaded artifactId. So you may want to use a different artifactId and keep
 222  
      * the standard version. If the original artifactId was "foo" then the final artifact would
 223  
      * be something like foo-1.0.jar. So if you change the artifactId you might have something
 224  
      * like foo-special-1.0.jar.
 225  
      *
 226  
      * @parameter expression="${shadedArtifactId}" default-value="${project.artifactId}"
 227  
      */
 228  
     private String shadedArtifactId;
 229  
 
 230  
     /**
 231  
      * If specified, this will include only artifacts which have groupIds which
 232  
      * start with this.
 233  
      *
 234  
      * @parameter expression="${shadedGroupFilter}"
 235  
      */
 236  
     private String shadedGroupFilter;
 237  
 
 238  
     /**
 239  
      * Defines whether the shaded artifact should be attached as classifier to
 240  
      * the original artifact.  If false, the shaded jar will be the main artifact
 241  
      * of the project
 242  
      *
 243  
      * @parameter expression="${shadedArtifactAttached}" default-value="false"
 244  
      */
 245  
     private boolean shadedArtifactAttached;
 246  
 
 247  
     /**
 248  
      * Flag whether to generate a simplified POM for the shaded artifact. If set to <code>true</code>, dependencies that
 249  
      * have been included into the uber JAR will be removed from the <code>&lt;dependencies&gt;</code> section of the
 250  
      * generated POM. The reduced POM will be named <code>dependency-reduced-pom.xml</code> and is stored into the same
 251  
      * directory as the shaded artifact.
 252  
      *
 253  
      * @parameter expression="${createDependencyReducedPom}" default-value="true"
 254  
      */
 255  
     private boolean createDependencyReducedPom;
 256  
 
 257  
     /**
 258  
      * When true, dependencies are kept in the pom but with scope 'provided'; when false,
 259  
      * the dependency is removed.
 260  
      *
 261  
      * @parameter expression="${keepDependenciesWithProvidedScope}" default-value="false"
 262  
      */
 263  
     private boolean keepDependenciesWithProvidedScope;
 264  
 
 265  
     /**
 266  
      * When true, transitive deps of removed dependencies are promoted to direct dependencies.
 267  
      * This should allow the drop in replacement of the removed deps with the new shaded
 268  
      * jar and everything should still work.
 269  
      *
 270  
      * @parameter expression="${promoteTransitiveDependencies}" default-value="false"
 271  
      */
 272  
     private boolean promoteTransitiveDependencies;
 273  
 
 274  
     /**
 275  
      * The name of the classifier used in case the shaded artifact is attached.
 276  
      *
 277  
      * @parameter expression="${shadedClassifierName}" default-value="shaded"
 278  
      */
 279  
     private String shadedClassifierName;
 280  
 
 281  
     /**
 282  
      * When true, it will attempt to create a sources jar as well
 283  
      *
 284  
      * @parameter expression="${createSourcesJar}" default-value="false"
 285  
      */
 286  
     private boolean createSourcesJar;
 287  
 
 288  
 
 289  
     /** @throws MojoExecutionException  */
 290  
     public void execute()
 291  
         throws MojoExecutionException
 292  
     {
 293  0
         Set artifacts = new LinkedHashSet();
 294  0
         Set artifactIds = new LinkedHashSet();
 295  0
         Set sourceArtifacts = new LinkedHashSet();
 296  
 
 297  0
         if ( project.getArtifact().getFile() == null )
 298  
         {
 299  0
             getLog().error( "The project main artifact does not exist. This could have the following" );
 300  0
             getLog().error( "reasons:" );
 301  0
             getLog().error( "- You have invoked the goal directly from the command line. This is not" );
 302  0
             getLog().error( "  supported. Please add the goal to the default lifecycle via an" );
 303  0
             getLog().error( "  <execution> element in your POM and use \"mvn package\" to have it run." );
 304  0
             getLog().error( "- You have bound the goal to a lifecycle phase before \"package\". Please" );
 305  0
             getLog().error( "  remove this binding from your POM such that the goal will be run in" );
 306  0
             getLog().error( "  the proper phase." );
 307  0
             throw new MojoExecutionException( "Failed to create shaded artifact.",
 308  
                                               new IllegalStateException( "Project main artifact does not exist." ) );
 309  
         }
 310  0
         artifacts.add( project.getArtifact().getFile() );
 311  
 
 312  0
         if ( createSourcesJar )
 313  
         {
 314  0
             File file = shadedSourcesArtifactFile();
 315  0
             if ( file.exists() )
 316  
             {
 317  0
                 sourceArtifacts.add( file );
 318  
             }
 319  
         }
 320  
 
 321  0
         for ( Iterator it = project.getArtifacts().iterator(); it.hasNext(); )
 322  
         {
 323  0
             Artifact artifact = (Artifact) it.next();
 324  
 
 325  0
             if ( excludeArtifact( artifact ) )
 326  
             {
 327  0
                 getLog().info( "Excluding " + artifact.getId() + " from the shaded jar." );
 328  
 
 329  0
                 continue;
 330  
             }
 331  
 
 332  0
             getLog().info( "Including " + artifact.getId() + " in the shaded jar." );
 333  
 
 334  0
             artifacts.add( artifact.getFile() );
 335  
 
 336  0
             artifactIds.add( getId( artifact ) );
 337  
 
 338  0
             if ( createSourcesJar )
 339  
             {
 340  0
                 File file = resolveArtifactSources( artifact );
 341  0
                 if ( file != null )
 342  
                 {
 343  0
                     sourceArtifacts.add( file );
 344  
                 }
 345  
             }
 346  
         }
 347  
 
 348  
 
 349  0
         File outputJar = shadedArtifactFileWithClassifier();
 350  0
         File sourcesJar = shadedSourceArtifactFileWithClassifier();
 351  
 
 352  
         // Now add our extra resources
 353  
         try
 354  
         {
 355  0
             List filters = getFilters();
 356  
 
 357  0
             List relocators = getRelocators();
 358  
 
 359  0
             List resourceTransformers = getResourceTransformers();
 360  
 
 361  0
             shader.shade( artifacts, outputJar, filters, relocators, resourceTransformers );
 362  
 
 363  0
             if ( createSourcesJar )
 364  
             {
 365  0
                 shader.shade( sourceArtifacts, sourcesJar, filters, relocators, resourceTransformers );
 366  
             }
 367  
 
 368  0
             if ( shadedArtifactAttached )
 369  
             {
 370  0
                 getLog().info( "Attaching shaded artifact." );
 371  0
                 projectHelper.attachArtifact( project, project.getArtifact().getType(), shadedClassifierName, outputJar );
 372  0
                 if ( createSourcesJar )
 373  
                 {
 374  0
                     projectHelper.attachArtifact( project, "jar", shadedClassifierName + "-sources", sourcesJar );
 375  
                 }
 376  
             }
 377  
 
 378  
             else
 379  
             {
 380  0
                 getLog().info( "Replacing original artifact with shaded artifact." );
 381  0
                 File file = shadedArtifactFile();
 382  0
                 replaceFile( file, outputJar );
 383  
 
 384  0
                 if ( createSourcesJar )
 385  
                 {
 386  0
                     file = shadedSourcesArtifactFile();
 387  
 
 388  0
                     replaceFile( file, sourcesJar );
 389  
 
 390  0
                     projectHelper.attachArtifact( project, "jar",
 391  
                                                   "sources", file );
 392  
                 }
 393  
 
 394  0
                 if ( createDependencyReducedPom )
 395  
                 {
 396  0
                     createDependencyReducedPom( artifactIds );
 397  
                 }
 398  
             }
 399  
         }
 400  0
         catch ( Exception e )
 401  
         {
 402  0
             throw new MojoExecutionException( "Error creating shaded jar.", e );
 403  0
         }
 404  0
     }
 405  
 
 406  
     private void replaceFile( File oldFile, File newFile ) throws MojoExecutionException
 407  
     {
 408  0
         getLog().info( "Replacing " + oldFile + " with " + newFile );
 409  
 
 410  0
         File origFile = new File( outputDirectory, "original-" + oldFile.getName() );
 411  0
         if ( oldFile.exists() && !oldFile.renameTo( origFile ) )
 412  
         {
 413  
             //try a gc to see if an unclosed stream needs garbage collecting
 414  0
             System.gc();
 415  0
             System.gc();
 416  
 
 417  0
             if ( !oldFile.renameTo( origFile ) )
 418  
             {
 419  
                 // Still didn't work.   We'll do a copy
 420  
                 try
 421  
                 {
 422  0
                     FileOutputStream fout = new FileOutputStream( origFile );
 423  0
                     FileInputStream fin = new FileInputStream( oldFile );
 424  
                     try
 425  
                     {
 426  0
                         IOUtil.copy( fin, fout );
 427  
                     }
 428  
                     finally
 429  
                     {
 430  0
                         IOUtil.close( fin );
 431  0
                         IOUtil.close( fout );
 432  0
                     }
 433  
                 }
 434  0
                 catch ( IOException ex )
 435  
                 {
 436  
                     //kind of ignorable here.   We're just trying to save the original
 437  0
                     getLog().warn( ex );
 438  0
                 }
 439  
             }
 440  
         }
 441  0
         if ( !newFile.renameTo( oldFile ) )
 442  
         {
 443  
             //try a gc to see if an unclosed stream needs garbage collecting
 444  0
             System.gc();
 445  0
             System.gc();
 446  
 
 447  0
             if ( !newFile.renameTo( oldFile ) )
 448  
             {
 449  
                 // Still didn't work.   We'll do a copy
 450  
                 try
 451  
                 {
 452  0
                     FileOutputStream fout = new FileOutputStream( oldFile );
 453  0
                     FileInputStream fin = new FileInputStream( newFile );
 454  
                     try
 455  
                     {
 456  0
                         IOUtil.copy( fin, fout );
 457  
                     }
 458  
                     finally
 459  
                     {
 460  0
                         IOUtil.close( fin );
 461  0
                         IOUtil.close( fout );
 462  0
                     }
 463  
                 }
 464  0
                 catch ( IOException ex )
 465  
                 {
 466  0
                     throw new MojoExecutionException( "Could not replace original artifact with shaded artifact!", ex );
 467  0
                 }
 468  
             }
 469  
         }
 470  0
     }
 471  
 
 472  
     private File resolveArtifactSources( Artifact artifact )
 473  
     {
 474  
 
 475  0
         Artifact resolvedArtifact =
 476  
             artifactFactory.createArtifactWithClassifier( artifact.getGroupId(),
 477  
                                                           artifact.getArtifactId(),
 478  
                                                           artifact.getVersion(),
 479  
                                                           "java-source",
 480  
                                                           "sources" );
 481  
 
 482  
         try
 483  
         {
 484  0
             artifactResolver.resolve( resolvedArtifact, remoteArtifactRepositories, localRepository );
 485  
         }
 486  0
         catch ( ArtifactNotFoundException e )
 487  
         {
 488  
             // ignore, the jar has not been found
 489  
         }
 490  0
         catch ( ArtifactResolutionException e )
 491  
         {
 492  0
             getLog().warn( "Could not get sources for " + artifact );
 493  0
         }
 494  
 
 495  0
         if ( resolvedArtifact.isResolved() )
 496  
         {
 497  0
             return resolvedArtifact.getFile();
 498  
         }
 499  0
         return null;
 500  
     }
 501  
 
 502  
     private boolean excludeArtifact( Artifact artifact )
 503  
     {
 504  0
         String id = getId( artifact );
 505  
 
 506  
         // This is the case where we have only stated artifacts to include and no exclusions
 507  
         // have been listed. We just want what we have asked to include.
 508  0
         if ( artifactSet != null && ( artifactSet.getExcludes() == null && artifactSet.getIncludes() != null )
 509  
             && !includedArtifacts().contains( id ) )
 510  
         {
 511  0
             return true;
 512  
         }
 513  
 
 514  0
         if ( excludedArtifacts().contains( id ) )
 515  
         {
 516  0
             return true;
 517  
         }
 518  
 
 519  0
         if ( shadedGroupFilter != null && !artifact.getGroupId().startsWith( shadedGroupFilter ) )
 520  
         {
 521  0
             return true;
 522  
         }
 523  
 
 524  0
         return false;
 525  
     }
 526  
 
 527  
     private Set excludedArtifacts()
 528  
     {
 529  0
         if ( artifactSet != null && artifactSet.getExcludes() != null )
 530  
         {
 531  0
             return artifactSet.getExcludes();
 532  
         }
 533  
 
 534  0
         return Collections.EMPTY_SET;
 535  
     }
 536  
 
 537  
     private Set includedArtifacts()
 538  
     {
 539  0
         if ( artifactSet != null && artifactSet.getIncludes() != null )
 540  
         {
 541  0
             return artifactSet.getIncludes();
 542  
         }
 543  
 
 544  0
         return Collections.EMPTY_SET;
 545  
     }
 546  
 
 547  
     private List getRelocators()
 548  
     {
 549  0
         List relocators = new ArrayList();
 550  
 
 551  0
         if ( relocations == null )
 552  
         {
 553  0
             return relocators;
 554  
         }
 555  
 
 556  0
         for ( int i = 0; i < relocations.length; i++ )
 557  
         {
 558  0
             PackageRelocation r = relocations[i];
 559  
 
 560  0
             relocators.add( new SimpleRelocator( r.getPattern(), r.getShadedPattern(), r.getExcludes() ) );
 561  
         }
 562  
 
 563  0
         return relocators;
 564  
     }
 565  
 
 566  
     private List getResourceTransformers()
 567  
     {
 568  0
         if ( transformers == null )
 569  
         {
 570  0
             return Collections.EMPTY_LIST;
 571  
         }
 572  
 
 573  0
         return Arrays.asList( transformers );
 574  
     }
 575  
 
 576  
     private List getFilters()
 577  
     {
 578  0
         List filters = new ArrayList();
 579  
 
 580  0
         if ( this.filters == null )
 581  
         {
 582  0
             return filters;
 583  
         }
 584  
 
 585  0
         Map artifacts = new HashMap();
 586  
 
 587  0
         artifacts.put( getId( project.getArtifact() ), project.getArtifact().getFile() );
 588  
 
 589  0
         for ( Iterator it = project.getArtifacts().iterator(); it.hasNext(); )
 590  
         {
 591  0
             Artifact artifact = (Artifact) it.next();
 592  
 
 593  0
             artifacts.put( getId( artifact ), artifact.getFile() );
 594  
         }
 595  
 
 596  0
         for ( int i = 0; i < this.filters.length; i++ )
 597  
         {
 598  0
             ArchiveFilter f = this.filters[i];
 599  
 
 600  0
             File jar = (File) artifacts.get( f.getArtifact() );
 601  
 
 602  0
             if ( jar == null )
 603  
             {
 604  0
                 getLog().info( "No artifact matching filter " + f.getArtifact() );
 605  
 
 606  0
                 continue;
 607  
             }
 608  
 
 609  0
             filters.add( new SimpleFilter( jar, f.getIncludes(), f.getExcludes() ) );
 610  
 
 611  
         }
 612  
 
 613  0
         return filters;
 614  
     }
 615  
 
 616  
     private File shadedArtifactFileWithClassifier()
 617  
     {
 618  0
         Artifact artifact = project.getArtifact();
 619  0
         final String shadedName =
 620  
             shadedArtifactId + "-" + artifact.getVersion() + "-" + shadedClassifierName + "."
 621  
                 + artifact.getArtifactHandler().getExtension();
 622  0
         return new File( outputDirectory, shadedName );
 623  
     }
 624  
 
 625  
     private File shadedSourceArtifactFileWithClassifier()
 626  
     {
 627  0
         Artifact artifact = project.getArtifact();
 628  0
         final String shadedName =
 629  
             shadedArtifactId + "-" + artifact.getVersion() + "-" + shadedClassifierName + "-sources."
 630  
                 + artifact.getArtifactHandler().getExtension();
 631  0
         return new File( outputDirectory, shadedName );
 632  
     }
 633  
 
 634  
     private File shadedArtifactFile()
 635  
     {
 636  0
         Artifact artifact = project.getArtifact();
 637  
 
 638  
         String shadedName;
 639  
 
 640  0
         if ( finalName != null )
 641  
         {
 642  0
             shadedName = finalName + "." + artifact.getArtifactHandler().getExtension();
 643  
         }
 644  
         else
 645  
         {
 646  0
             shadedName = shadedArtifactId + "-" + artifact.getVersion() + "."
 647  
                 + artifact.getArtifactHandler().getExtension();
 648  
         }
 649  
 
 650  0
         return new File( outputDirectory, shadedName );
 651  
     }
 652  
 
 653  
     private File shadedSourcesArtifactFile()
 654  
     {
 655  0
         Artifact artifact = project.getArtifact();
 656  
 
 657  
         String shadedName;
 658  
 
 659  0
         if ( finalName != null )
 660  
         {
 661  0
             shadedName = finalName + "-sources." + artifact.getArtifactHandler().getExtension();
 662  
         }
 663  
         else
 664  
         {
 665  0
             shadedName = shadedArtifactId + "-" + artifact.getVersion() + "-sources."
 666  
                 + artifact.getArtifactHandler().getExtension();
 667  
         }
 668  
 
 669  0
         return new File( outputDirectory, shadedName );
 670  
     }
 671  
 
 672  
     // We need to find the direct dependencies that have been included in the uber JAR so that we can modify the
 673  
     // POM accordingly.
 674  
     private void createDependencyReducedPom( Set artifactsToRemove )
 675  
         throws IOException, DependencyTreeBuilderException, ProjectBuildingException
 676  
     {
 677  0
         Model model = project.getOriginalModel();
 678  0
         List dependencies = new ArrayList();
 679  
 
 680  0
         boolean modified = false;
 681  
 
 682  0
         List transitiveDeps = new ArrayList();
 683  
 
 684  0
         for ( Iterator it = project.getArtifacts().iterator(); it.hasNext(); )
 685  
         {
 686  0
             Artifact artifact = (Artifact) it.next();
 687  
 
 688  
             //promote
 689  0
             Dependency dep = new Dependency();
 690  0
             dep.setArtifactId( artifact.getArtifactId() );
 691  0
             if ( artifact.hasClassifier() )
 692  
             {
 693  0
                 dep.setClassifier( artifact.getClassifier() );
 694  
             }
 695  0
             dep.setGroupId( artifact.getGroupId() );
 696  0
             dep.setOptional( artifact.isOptional() );
 697  0
             dep.setScope( artifact.getScope() );
 698  0
             dep.setType( artifact.getType() );
 699  0
             dep.setVersion( artifact.getVersion() );
 700  
 
 701  
             //we'll figure out the exclusions in a bit.
 702  
 
 703  0
             transitiveDeps.add( dep );
 704  
         }
 705  0
         List origDeps = project.getDependencies();
 706  
 
 707  0
         if ( promoteTransitiveDependencies )
 708  
         {
 709  0
             origDeps = transitiveDeps;
 710  
         }
 711  
 
 712  0
         for ( Iterator i = origDeps.iterator(); i.hasNext(); )
 713  
         {
 714  0
             Dependency d = (Dependency) i.next();
 715  
 
 716  0
             dependencies.add( d );
 717  
 
 718  0
             String id = d.getGroupId() + ":" + d.getArtifactId();
 719  
 
 720  0
             if ( artifactsToRemove.contains( id ) )
 721  
             {
 722  0
                 modified = true;
 723  
 
 724  0
                 if ( keepDependenciesWithProvidedScope )
 725  
                 {
 726  0
                     d.setScope( "provided" );
 727  
                 }
 728  
                 else
 729  
                 {
 730  0
                     dependencies.remove( d );
 731  
                 }
 732  
             }
 733  
         }
 734  
 
 735  
         // Check to see if we have a reduction and if so rewrite the POM.
 736  0
         if ( modified )
 737  
         {
 738  0
             while ( modified )
 739  
             {
 740  
 
 741  0
                 model.setDependencies( dependencies );
 742  
 
 743  0
                 File f = new File( outputDirectory, "dependency-reduced-pom.xml" );
 744  0
                 if ( f.exists() )
 745  
                 {
 746  0
                     f.delete();
 747  
                 }
 748  
 
 749  0
                 Writer w = WriterFactory.newXmlWriter( f );
 750  
 
 751  0
                 PomWriter.write( w, model, true );
 752  
 
 753  0
                 w.close();
 754  
 
 755  0
                 MavenProject p2 = mavenProjectBuilder.build( f, localRepository, null );
 756  0
                 modified = updateExcludesInDeps( p2, dependencies, transitiveDeps );
 757  
 
 758  
             }
 759  
 
 760  
             //copy the dependecy-reduced-pom.xml to the basedir where
 761  
             //we'll set the file for the project to it.  We cannot set
 762  
             //it to the real version in "target" as then ${basedir} gets
 763  
             //messed up.   We'll delete this file on exit to make
 764  
             //sure it gets cleaned up.
 765  0
             File f = new File( project.getBasedir(), "dependency-reduced-pom.xml" );
 766  0
             File f2 = new File( outputDirectory, "dependency-reduced-pom.xml" );
 767  0
             if ( f.exists() )
 768  
             {
 769  0
                 f.delete();
 770  
             }
 771  0
             FileUtils.copyFile( f2, f );
 772  0
             FileUtils.forceDeleteOnExit( f );
 773  0
             project.setFile( f );
 774  
         }
 775  0
     }
 776  
 
 777  
     private String getId( Artifact artifact )
 778  
     {
 779  0
         if ( artifact.getClassifier() == null || "jar".equals( artifact.getClassifier() ) )
 780  
         {
 781  0
             return artifact.getGroupId() + ":" + artifact.getArtifactId();
 782  
         }
 783  
         else
 784  
         {
 785  0
             return artifact.getGroupId() + ":" + artifact.getArtifactId() + ":" + artifact.getClassifier();
 786  
         }
 787  
     }
 788  
 
 789  
 
 790  
     public boolean updateExcludesInDeps( MavenProject project,
 791  
                                          List dependencies,
 792  
                                          List transitiveDeps )
 793  
         throws DependencyTreeBuilderException
 794  
     {
 795  0
         DependencyNode node = dependencyTreeBuilder.buildDependencyTree(
 796  
                                                   project,
 797  
                                                   localRepository,
 798  
                                                   artifactFactory,
 799  
                                                   artifactMetadataSource,
 800  
                                                   null,
 801  
                                                   artifactCollector );
 802  0
         boolean modified = false;
 803  0
         Iterator it = node.getChildren().listIterator();
 804  0
         while ( it.hasNext() )
 805  
         {
 806  0
             DependencyNode n2 = (DependencyNode) it.next();
 807  0
             Iterator it2 = n2.getChildren().listIterator();
 808  0
             while ( it2.hasNext() )
 809  
             {
 810  0
                 DependencyNode n3 = (DependencyNode) it2.next();
 811  
                 //anything two levels deep that is marked "included"
 812  
                 //is stuff that was excluded by the original poms, make sure it
 813  
                 //remains excluded IF promoting transitives.
 814  0
                 if ( n3.getState() == DependencyNode.INCLUDED )
 815  
                 {
 816  
                     //check if it really isn't in the list of original dependencies.  Maven
 817  
                     //prior to 2.0.8 may grab versions from transients instead of
 818  
                     //from the direct deps in which case they would be marked included
 819  
                     //instead of OMITTED_FOR_DUPLICATE
 820  
 
 821  
                     //also, if not promoting the transitives, level 2's would be included
 822  0
                     boolean found = false;
 823  0
                     for ( int x = 0; x < transitiveDeps.size(); x++ )
 824  
                     {
 825  0
                         Dependency dep = (Dependency) transitiveDeps.get( x );
 826  0
                         if ( dep.getArtifactId().equals( n3.getArtifact().getArtifactId() )
 827  
                             && dep.getGroupId().equals( n3.getArtifact().getGroupId() ) )
 828  
                         {
 829  0
                             found = true;
 830  
                         }
 831  
 
 832  
                     }
 833  
 
 834  0
                     if ( !found )
 835  
                     {
 836  0
                         for ( int x = 0; x < dependencies.size(); x++ )
 837  
                         {
 838  0
                             Dependency dep = (Dependency) dependencies.get( x );
 839  0
                             if ( dep.getArtifactId().equals( n2.getArtifact().getArtifactId() )
 840  
                                 && dep.getGroupId().equals( n2.getArtifact().getGroupId() ) )
 841  
                             {
 842  0
                                 Exclusion exclusion = new Exclusion();
 843  0
                                 exclusion.setArtifactId( n3.getArtifact().getArtifactId() );
 844  0
                                 exclusion.setGroupId( n3.getArtifact().getGroupId() );
 845  0
                                 dep.addExclusion( exclusion );
 846  0
                                 modified = true;
 847  0
                                 break;
 848  
                             }
 849  
                         }
 850  
                     }
 851  
                 }
 852  
             }
 853  
         }
 854  0
         return modified;
 855  
     }
 856  
 }