Coverage Report - org.apache.maven.plugin.dependency.GetMojo
 
Classes in this File Line Coverage Branch Coverage Complexity
GetMojo
73%
49/67
60%
24/40
10.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 org.apache.commons.io.FileUtils;
 23  
 import org.apache.maven.artifact.Artifact;
 24  
 import org.apache.maven.artifact.factory.ArtifactFactory;
 25  
 import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
 26  
 import org.apache.maven.artifact.repository.ArtifactRepository;
 27  
 import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
 28  
 import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
 29  
 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
 30  
 import org.apache.maven.artifact.resolver.AbstractArtifactResolutionException;
 31  
 import org.apache.maven.artifact.resolver.ArtifactResolver;
 32  
 import org.apache.maven.plugin.AbstractMojo;
 33  
 import org.apache.maven.plugin.MojoExecutionException;
 34  
 import org.apache.maven.plugin.MojoFailureException;
 35  
 import org.apache.maven.plugins.annotations.Component;
 36  
 import org.apache.maven.plugins.annotations.Mojo;
 37  
 import org.apache.maven.plugins.annotations.Parameter;
 38  
 import org.codehaus.plexus.util.StringUtils;
 39  
 
 40  
 import java.io.File;
 41  
 import java.io.IOException;
 42  
 import java.util.ArrayList;
 43  
 import java.util.Arrays;
 44  
 import java.util.Collections;
 45  
 import java.util.List;
 46  
 import java.util.Map;
 47  
 import java.util.regex.Matcher;
 48  
 import java.util.regex.Pattern;
 49  
 
 50  
 /**
 51  
  * Downloads a single artifact transitively from the specified remote repositories. Caveat: will always check the
 52  
  * central repository defined in the super pom. You could use a mirror entry in your settings.xml
 53  
  */
 54  
 @Mojo( name = "get", requiresProject = false, threadSafe = true )
 55  4
 public class GetMojo
 56  
     extends AbstractMojo
 57  
 {
 58  1
     private static final Pattern ALT_REPO_SYNTAX_PATTERN = Pattern.compile( "(.+)::(.*)::(.+)" );
 59  
 
 60  
     /**
 61  
      *
 62  
      */
 63  
     @Component
 64  
     private ArtifactFactory artifactFactory;
 65  
 
 66  
     /**
 67  
      *
 68  
      */
 69  
     @Component
 70  
     private ArtifactResolver artifactResolver;
 71  
 
 72  
     /**
 73  
      *
 74  
      */
 75  
     @Component
 76  
     private ArtifactRepositoryFactory artifactRepositoryFactory;
 77  
 
 78  
     /**
 79  
      * Map that contains the layouts.
 80  
      */
 81  
     @Component( role = ArtifactRepositoryLayout.class )
 82  
     private Map<String, ArtifactRepositoryLayout> repositoryLayouts;
 83  
 
 84  
     /**
 85  
      *
 86  
      */
 87  
     @Component
 88  
     private ArtifactMetadataSource source;
 89  
 
 90  
     /**
 91  
      *
 92  
      */
 93  
     @Parameter( defaultValue = "${localRepository}", readonly = true )
 94  
     private ArtifactRepository localRepository;
 95  
 
 96  
     /**
 97  
      * The groupId of the artifact to download. Ignored if {@link #artifact} is used.
 98  
      */
 99  
     @Parameter( property = "groupId" )
 100  
     private String groupId;
 101  
 
 102  
     /**
 103  
      * The artifactId of the artifact to download. Ignored if {@link #artifact} is used.
 104  
      */
 105  
     @Parameter( property = "artifactId" )
 106  
     private String artifactId;
 107  
 
 108  
     /**
 109  
      * The version of the artifact to download. Ignored if {@link #artifact} is used.
 110  
      */
 111  
     @Parameter( property = "version" )
 112  
     private String version;
 113  
 
 114  
     /**
 115  
      * The classifier of the artifact to download. Ignored if {@link #artifact} is used.
 116  
      *
 117  
      * @since 2.3
 118  
      */
 119  
     @Parameter( property = "classifier" )
 120  
     private String classifier;
 121  
 
 122  
     /**
 123  
      * The packaging of the artifact to download. Ignored if {@link #artifact} is used.
 124  
      */
 125  4
     @Parameter( property = "packaging", defaultValue = "jar" )
 126  
     private String packaging = "jar";
 127  
 
 128  
     /**
 129  
      * The id of the repository from which we'll download the artifact
 130  
      *
 131  
      * @deprecated Use remoteRepositories
 132  
      */
 133  4
     @Parameter( property = "repoId", defaultValue = "temp" )
 134  
     private String repositoryId = "temp";
 135  
 
 136  
     /**
 137  
      * The url of the repository from which we'll download the artifact. DEPRECATED Use remoteRepositories
 138  
      *
 139  
      * @deprecated Use remoteRepositories
 140  
      */
 141  
     @Parameter( property = "repoUrl" )
 142  
     private String repositoryUrl;
 143  
 
 144  
     /**
 145  
      * Repositories in the format id::[layout]::url or just url, separated by comma.
 146  
      * ie. central::default::http://repo1.maven.apache.org/maven2,myrepo::::http://repo.acme.com,http://repo.acme2.com
 147  
      */
 148  
     @Parameter( property = "remoteRepositories" )
 149  
     private String remoteRepositories;
 150  
 
 151  
     /**
 152  
      * A string of the form groupId:artifactId:version[:packaging][:classifier].
 153  
      */
 154  
     @Parameter( property = "artifact" )
 155  
     private String artifact;
 156  
 
 157  
     /**
 158  
      * The destination file or directory to copy the artifact to, if other than the local repository
 159  
      *
 160  
      * @since 2.4
 161  
      */
 162  
     @Parameter( property = "dest" )
 163  
     private String destination;
 164  
 
 165  
     /**
 166  
      *
 167  
      */
 168  
     @Parameter( defaultValue = "${project.remoteArtifactRepositories}", readonly = true, required = true )
 169  
     private List<ArtifactRepository> pomRemoteRepositories;
 170  
 
 171  
     /**
 172  
      * Download transitively, retrieving the specified artifact and all of its dependencies.
 173  
      */
 174  4
     @Parameter( property = "transitive", defaultValue = "true" )
 175  
     private boolean transitive = true;
 176  
 
 177  
     public void execute()
 178  
         throws MojoExecutionException, MojoFailureException
 179  
     {
 180  
 
 181  5
         if ( artifactId == null && artifact == null )
 182  
         {
 183  0
             throw new MojoFailureException( "You must specify an artifact, "
 184  
                 + "e.g. -Dartifact=org.apache.maven.plugins:maven-downloader-plugin:1.0" );
 185  
         }
 186  5
         if ( artifact != null )
 187  
         {
 188  0
             String[] tokens = StringUtils.split( artifact, ":" );
 189  0
             if ( tokens.length < 3 || tokens.length > 5 )
 190  
             {
 191  0
                 throw new MojoFailureException(
 192  
                     "Invalid artifact, you must specify groupId:artifactId:version[:packaging][:classifier] "
 193  
                         + artifact );
 194  
             }
 195  0
             groupId = tokens[0];
 196  0
             artifactId = tokens[1];
 197  0
             version = tokens[2];
 198  0
             if ( tokens.length >= 4 )
 199  
             {
 200  0
                 packaging = tokens[3];
 201  
             }
 202  0
             if ( tokens.length == 5 )
 203  
             {
 204  0
                 classifier = tokens[4];
 205  
             }
 206  
             else
 207  
             {
 208  0
                 classifier = null;
 209  
             }
 210  
         }
 211  
 
 212  5
         Artifact toDownload = classifier == null
 213  
             ? artifactFactory.createBuildArtifact( groupId, artifactId, version, packaging )
 214  
             : artifactFactory.createArtifactWithClassifier( groupId, artifactId, version, packaging, classifier );
 215  5
         Artifact dummyOriginatingArtifact =
 216  
             artifactFactory.createBuildArtifact( "org.apache.maven.plugins", "maven-downloader-plugin", "1.0", "jar" );
 217  
 
 218  5
         ArtifactRepositoryPolicy always =
 219  
             new ArtifactRepositoryPolicy( true, ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS,
 220  
                                           ArtifactRepositoryPolicy.CHECKSUM_POLICY_WARN );
 221  
 
 222  5
         List<ArtifactRepository> repoList = new ArrayList<ArtifactRepository>();
 223  
 
 224  5
         if ( pomRemoteRepositories != null )
 225  
         {
 226  0
             repoList.addAll( pomRemoteRepositories );
 227  
         }
 228  
 
 229  5
         if ( remoteRepositories != null )
 230  
         {
 231  
             // Use the same format as in the deploy plugin id::layout::url
 232  1
             List<String> repos = Arrays.asList( StringUtils.split( remoteRepositories, "," ) );
 233  1
             for ( String repo : repos )
 234  
             {
 235  3
                 repoList.add( parseRepository( repo, always ) );
 236  
             }
 237  
         }
 238  
 
 239  5
         if ( repositoryUrl != null )
 240  
         {
 241  4
             getLog().warn( "repositoryUrl parameter is deprecated. Use remoteRepositories instead" );
 242  4
             ArtifactRepository remoteRepo =
 243  
                 artifactRepositoryFactory.createArtifactRepository( repositoryId, repositoryUrl, getLayout( "default" ),
 244  
                                                                     always, always );
 245  4
             repoList.add( remoteRepo );
 246  
         }
 247  
 
 248  
         try
 249  
         {
 250  5
             if ( transitive )
 251  
             {
 252  1
                 artifactResolver.resolveTransitively( Collections.singleton( toDownload ), dummyOriginatingArtifact,
 253  
                                                       repoList, localRepository, source );
 254  
             }
 255  
             else
 256  
             {
 257  4
                 artifactResolver.resolve( toDownload, repoList, localRepository );
 258  
             }
 259  
         }
 260  0
         catch ( AbstractArtifactResolutionException e )
 261  
         {
 262  0
             throw new MojoExecutionException( "Couldn't download artifact: " + e.getMessage(), e );
 263  5
         }
 264  
 
 265  5
         if ( destination != null )
 266  
         {
 267  2
             File src = toDownload.getFile();
 268  2
             File dest = new File( destination );
 269  2
             if ( getLog().isInfoEnabled() )
 270  
             {
 271  2
                 getLog().info( "Copying " + src.getAbsolutePath() + " to " + dest.getAbsolutePath() );
 272  
             }
 273  
             try
 274  
             {
 275  2
                 if ( dest.isDirectory() )
 276  
                 {
 277  1
                     FileUtils.copyFileToDirectory( src, dest );
 278  
                 }
 279  
                 else
 280  
                 {
 281  1
                     FileUtils.copyFile( src, dest );
 282  
                 } 
 283  
             }
 284  0
             catch ( IOException e )
 285  
             {
 286  0
                 throw new MojoExecutionException(
 287  
                     "Couldn't copy downloaded artifact from " + src.getAbsolutePath() + " to " + dest.getAbsolutePath()
 288  
                         + " : " + e.getMessage(), e );
 289  2
             }
 290  
         }
 291  5
     }
 292  
 
 293  
     ArtifactRepository parseRepository( String repo, ArtifactRepositoryPolicy policy )
 294  
         throws MojoFailureException
 295  
     {
 296  
         // if it's a simple url
 297  9
         String id = repositoryId;
 298  9
         ArtifactRepositoryLayout layout = getLayout( "default" );
 299  9
         String url = repo;
 300  
 
 301  
         // if it's an extended repo URL of the form id::layout::url
 302  9
         if ( repo.indexOf( "::" ) >= 0 )
 303  
         {
 304  7
             Matcher matcher = ALT_REPO_SYNTAX_PATTERN.matcher( repo );
 305  7
             if ( !matcher.matches() )
 306  
             {
 307  2
                 throw new MojoFailureException( repo, "Invalid syntax for repository: " + repo,
 308  
                                                 "Invalid syntax for repository. Use \"id::layout::url\" or \"URL\"." );
 309  
             }
 310  
 
 311  5
             id = matcher.group( 1 ).trim();
 312  5
             if ( !StringUtils.isEmpty( matcher.group( 2 ) ) )
 313  
             {
 314  3
                 layout = getLayout( matcher.group( 2 ).trim() );
 315  
             }
 316  5
             url = matcher.group( 3 ).trim();
 317  
         }
 318  7
         return artifactRepositoryFactory.createArtifactRepository( id, url, layout, policy, policy );
 319  
     }
 320  
 
 321  
     private ArtifactRepositoryLayout getLayout( String id )
 322  
         throws MojoFailureException
 323  
     {
 324  16
         ArtifactRepositoryLayout layout = repositoryLayouts.get( id );
 325  
 
 326  16
         if ( layout == null )
 327  
         {
 328  0
             throw new MojoFailureException( id, "Invalid repository layout", "Invalid repository layout: " + id );
 329  
         }
 330  
 
 331  16
         return layout;
 332  
     }
 333  
 }