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