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