Coverage Report - org.apache.maven.plugin.dependency.CopyDependenciesMojo
 
Classes in this File Line Coverage Branch Coverage Complexity
CopyDependenciesMojo
89 %
51/57
76 %
23/30
3,714
 
 1  
 package org.apache.maven.plugin.dependency;
 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 org.apache.maven.artifact.Artifact;
 23  
 import org.apache.maven.artifact.installer.ArtifactInstallationException;
 24  
 import org.apache.maven.artifact.installer.ArtifactInstaller;
 25  
 import org.apache.maven.artifact.repository.ArtifactRepository;
 26  
 import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
 27  
 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
 28  
 import org.apache.maven.plugin.MojoExecutionException;
 29  
 import org.apache.maven.plugin.dependency.utils.DependencyStatusSets;
 30  
 import org.apache.maven.plugin.dependency.utils.DependencyUtil;
 31  
 import org.apache.maven.plugin.dependency.utils.filters.DestFileFilter;
 32  
 import org.apache.maven.plugins.annotations.Component;
 33  
 import org.apache.maven.plugins.annotations.LifecyclePhase;
 34  
 import org.apache.maven.plugins.annotations.Mojo;
 35  
 import org.apache.maven.plugins.annotations.ResolutionScope;
 36  
 import org.apache.maven.shared.artifact.filter.collection.ArtifactsFilter;
 37  
 
 38  
 import java.io.File;
 39  
 import java.net.MalformedURLException;
 40  
 import java.util.Map;
 41  
 import java.util.Set;
 42  
 
 43  
 /**
 44  
  * Goal that copies the project dependencies from the repository to a defined
 45  
  * location.
 46  
  *
 47  
  * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
 48  
  * @version $Id: CopyDependenciesMojo.java 1367274 2012-07-30 20:32:05Z hboutemy $
 49  
  * @since 1.0
 50  
  */
 51  
 @Mojo( name = "copy-dependencies", requiresDependencyResolution = ResolutionScope.TEST,
 52  
        defaultPhase = LifecyclePhase.PROCESS_SOURCES )
 53  42
 public class CopyDependenciesMojo
 54  
     extends AbstractFromDependenciesMojo
 55  
 {
 56  
 
 57  
     /**
 58  
      *
 59  
      */
 60  
     @Component
 61  
     protected ArtifactInstaller installer;
 62  
 
 63  
     /**
 64  
      *
 65  
      */
 66  
     @Component
 67  
     protected ArtifactRepositoryFactory repositoryFactory;
 68  
 
 69  
     /**
 70  
      *
 71  
      */
 72  
     @Component( role = ArtifactRepositoryLayout.class )
 73  
     private Map<String, ArtifactRepositoryLayout> repositoryLayouts;
 74  
 
 75  
     /**
 76  
      * Main entry into mojo. Gets the list of dependencies and iterates through
 77  
      * calling copyArtifact.
 78  
      *
 79  
      * @throws MojoExecutionException with a message if an error occurs.
 80  
      * @see #getDependencies
 81  
      * @see #copyArtifact(Artifact, boolean)
 82  
      */
 83  
     public void execute()
 84  
         throws MojoExecutionException
 85  
     {
 86  48
         DependencyStatusSets dss = getDependencySets( this.failOnMissingClassifierArtifact );
 87  45
         Set<Artifact> artifacts = dss.getResolvedDependencies();
 88  
 
 89  45
         if ( !useRepositoryLayout )
 90  
         {
 91  44
             for ( Artifact artifact : artifacts )
 92  
             {
 93  129
                 copyArtifact( artifact, this.stripVersion, this.prependGroupId );
 94  
             }
 95  
         }
 96  
         else
 97  
         {
 98  
             try
 99  
             {
 100  1
                 ArtifactRepository targetRepository =
 101  
                     repositoryFactory.createDeploymentArtifactRepository( "local",
 102  
                                                                           outputDirectory.toURL().toExternalForm(),
 103  
                                                                           (ArtifactRepositoryLayout) repositoryLayouts.get( "default" ),
 104  
                                                                           false /* uniqueVersion */);
 105  1
                 for ( Artifact artifact : artifacts )
 106  
                 {
 107  9
                     installArtifact( artifact, targetRepository );
 108  
                 }
 109  
             }
 110  0
             catch ( MalformedURLException e )
 111  
             {
 112  0
                 throw new MojoExecutionException( "Could not create outputDirectory repository", e );
 113  1
             }
 114  
         }
 115  
 
 116  45
         Set<Artifact> skippedArtifacts = dss.getSkippedDependencies();
 117  45
         for ( Artifact artifact : skippedArtifacts )
 118  
         {
 119  2
             getLog().info( artifact.getFile().getName() + " already exists in destination." );
 120  
         }
 121  
 
 122  45
         if ( isCopyPom() )
 123  
         {
 124  45
             copyPoms( getOutputDirectory(), artifacts, this.stripVersion );
 125  45
             copyPoms( getOutputDirectory(), skippedArtifacts,
 126  
                       this.stripVersion );  // Artifacts that already exist may not already have poms.
 127  
         }
 128  45
     }
 129  
 
 130  
     private void installArtifact( Artifact artifact, ArtifactRepository targetRepository )
 131  
     {
 132  
         try
 133  
         {
 134  9
             if ( "pom".equals( artifact.getType() ) )
 135  
             {
 136  1
                 installer.install( artifact.getFile(), artifact, targetRepository );
 137  1
                 installBaseSnapshot( artifact, targetRepository );
 138  
             }
 139  
             else
 140  
             {
 141  8
                 installer.install( artifact.getFile(), artifact, targetRepository );
 142  8
                 installBaseSnapshot( artifact, targetRepository );
 143  
 
 144  8
                 if ( isCopyPom() )
 145  
                 {
 146  8
                     Artifact pomArtifact = getResolvedPomArtifact( artifact );
 147  8
                     if ( pomArtifact.getFile() != null && pomArtifact.getFile().exists() )
 148  
                     {
 149  0
                         installer.install( pomArtifact.getFile(), pomArtifact, targetRepository );
 150  0
                         installBaseSnapshot( pomArtifact, targetRepository );
 151  
                     }
 152  
                 }
 153  
             }
 154  
         }
 155  0
         catch ( ArtifactInstallationException e )
 156  
         {
 157  0
             getLog().info( e.getMessage() );
 158  9
         }
 159  9
     }
 160  
 
 161  
     private void installBaseSnapshot( Artifact artifact, ArtifactRepository targetRepository )
 162  
         throws ArtifactInstallationException
 163  
     {
 164  9
         if ( artifact.isSnapshot() && !artifact.getBaseVersion().equals( artifact.getVersion() ) )
 165  
         {
 166  2
             Artifact baseArtifact =
 167  
                 this.factory.createArtifact( artifact.getGroupId(), artifact.getArtifactId(), artifact.getBaseVersion(),
 168  
                                              artifact.getScope(), artifact.getType() );
 169  2
             installer.install( artifact.getFile(), baseArtifact, targetRepository );
 170  
         }
 171  9
     }
 172  
 
 173  
     /**
 174  
      * Copies the Artifact after building the destination file name if
 175  
      * overridden. This method also checks if the classifier is set and adds it
 176  
      * to the destination file name if needed.
 177  
      *
 178  
      * @param artifact       representing the object to be copied.
 179  
      * @param removeVersion  specifies if the version should be removed from the file name
 180  
      *                       when copying.
 181  
      * @param prependGroupId specifies if the groupId should be prepend to the file while copying.
 182  
      * @throws MojoExecutionException with a message if an error occurs.
 183  
      * @see DependencyUtil#copyFile(File, File, Log)
 184  
      * @see DependencyUtil#getFormattedFileName(Artifact, boolean)
 185  
      */
 186  
     protected void copyArtifact( Artifact artifact, boolean removeVersion, boolean prependGroupId )
 187  
         throws MojoExecutionException
 188  
     {
 189  
 
 190  129
         String destFileName = DependencyUtil.getFormattedFileName( artifact, removeVersion, prependGroupId );
 191  
 
 192  
         File destDir;
 193  129
         destDir = DependencyUtil.getFormattedOutputDirectory( useSubDirectoryPerScope, useSubDirectoryPerType,
 194  
                                                               useSubDirectoryPerArtifact, useRepositoryLayout,
 195  
                                                               stripVersion, outputDirectory, artifact );
 196  129
         File destFile = new File( destDir, destFileName );
 197  
 
 198  129
         copyFile( artifact.getFile(), destFile );
 199  129
     }
 200  
 
 201  
     /**
 202  
      * Copy the pom files associated with the artifacts.
 203  
      */
 204  
     public void copyPoms( File destDir, Set<Artifact> artifacts, boolean removeVersion )
 205  
         throws MojoExecutionException
 206  
 
 207  
     {
 208  90
         for ( Artifact artifact : artifacts )
 209  
         {
 210  140
             Artifact pomArtifact = getResolvedPomArtifact( artifact );
 211  
 
 212  
             // Copy the pom
 213  140
             if ( pomArtifact.getFile() != null && pomArtifact.getFile().exists() )
 214  
             {
 215  22
                 File pomDestFile = new File( destDir, DependencyUtil.getFormattedFileName( pomArtifact, removeVersion,
 216  
                                                                                            prependGroupId ) );
 217  22
                 if ( !pomDestFile.exists() )
 218  
                 {
 219  22
                     copyFile( pomArtifact.getFile(), pomDestFile );
 220  
                 }
 221  
             }
 222  140
         }
 223  90
     }
 224  
 
 225  
     protected Artifact getResolvedPomArtifact( Artifact artifact )
 226  
     {
 227  148
         Artifact pomArtifact =
 228  
             this.factory.createArtifact( artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), "",
 229  
                                          "pom" );
 230  
         // Resolve the pom artifact using repos
 231  
         try
 232  
         {
 233  148
             this.resolver.resolve( pomArtifact, this.remoteRepos, this.getLocal() );
 234  
         }
 235  126
         catch ( Exception e )
 236  
         {
 237  126
             getLog().info( e.getMessage() );
 238  22
         }
 239  148
         return pomArtifact;
 240  
     }
 241  
 
 242  
     protected ArtifactsFilter getMarkedArtifactFilter()
 243  
     {
 244  49
         return new DestFileFilter( this.overWriteReleases, this.overWriteSnapshots, this.overWriteIfNewer,
 245  
                                    this.useSubDirectoryPerArtifact, this.useSubDirectoryPerType,
 246  
                                    this.useSubDirectoryPerScope, this.useRepositoryLayout, this.stripVersion,
 247  
                                    this.outputDirectory );
 248  
     }
 249  
 }