Coverage Report - com.simpligility.maven.plugins.androidndk.common.ArtifactResolverHelper
 
Classes in this File Line Coverage Branch Coverage Complexity
ArtifactResolverHelper
0%
0/45
0%
0/22
3
 
 1  
 /*******************************************************************************
 2  
  * Copyright (c) 2008, 2011 Sonatype Inc. and others.
 3  
  * All rights reserved. This program and the accompanying materials
 4  
  * are made available under the terms of the Eclipse Public License v1.0
 5  
  * which accompanies this distribution, and is available at
 6  
  * http://www.eclipse.org/legal/epl-v10.html
 7  
  *
 8  
  * Contributors:
 9  
  *    Sonatype Inc. - initial API and implementation
 10  
  *******************************************************************************/
 11  
 package com.simpligility.maven.plugins.androidndk.common;
 12  
 
 13  
 import org.apache.maven.artifact.Artifact;
 14  
 import org.apache.maven.artifact.repository.ArtifactRepository;
 15  
 import org.apache.maven.artifact.resolver.ArtifactResolutionRequest;
 16  
 import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
 17  
 import org.apache.maven.artifact.resolver.ArtifactResolver;
 18  
 import org.apache.maven.plugin.MojoExecutionException;
 19  
 import org.codehaus.plexus.logging.Logger;
 20  
 
 21  
 import java.io.File;
 22  
 import java.util.Arrays;
 23  
 import java.util.Collection;
 24  
 import java.util.Collections;
 25  
 import java.util.LinkedHashSet;
 26  
 import java.util.List;
 27  
 import java.util.Set;
 28  
 
 29  
 /**
 30  
  * Provides convenient functions for resolving artifacts.
 31  
  */
 32  
 public final class ArtifactResolverHelper
 33  
 {
 34  
     /**
 35  
      * Which dependency scopes should be excluded when packing dependencies into the apk.
 36  
      */
 37  0
     public static final List<String> EXCLUDE_NON_PACKAGED_SCOPES = Arrays.asList(
 38  
             Artifact.SCOPE_PROVIDED, Artifact.SCOPE_IMPORT
 39  
     );
 40  
 
 41  
     private final ArtifactResolver artifactResolver;
 42  
     private final Logger log;
 43  
     private final List<ArtifactRepository> remoteArtifactRepositories;
 44  
 
 45  
     /**
 46  
      * Creates an ArtifactResolver that has no remote repositories to resolve against.
 47  
      */
 48  
     public ArtifactResolverHelper( ArtifactResolver artifactResolver, Logger log )
 49  
     {
 50  0
         this( artifactResolver, log, Collections.<ArtifactRepository>emptyList() );
 51  0
     }
 52  
 
 53  
     public ArtifactResolverHelper( ArtifactResolver artifactResolver, Logger log,
 54  
                                   final List<ArtifactRepository> remoteArtifactRepositories )
 55  0
     {
 56  0
         this.artifactResolver = artifactResolver;
 57  0
         this.log = log;
 58  0
         this.remoteArtifactRepositories = remoteArtifactRepositories;
 59  0
     }
 60  
 
 61  
     /**
 62  
      * Filters provided artifacts and selects only defined types based on {@code types} argument
 63  
      * or all types if {@code types} argument is empty.
 64  
      *
 65  
      * @param allArtifacts artifacts to be filtered
 66  
      * @param types artifact types to be selected
 67  
      * @return a {@code List} of all project dependencies. Never {@code null}.
 68  
      *      This excludes artifacts of the {@code EXCLUDED_DEPENDENCY_SCOPES} scopes.
 69  
      *      And this should maintain dependency order to comply with library project resource precedence.
 70  
      */
 71  
     public Set<Artifact> getFilteredArtifacts( Iterable<Artifact> allArtifacts, String... types )
 72  
     {
 73  0
         return getFilteredArtifacts( EXCLUDE_NON_PACKAGED_SCOPES, allArtifacts, types );
 74  
     }
 75  
 
 76  
     /**
 77  
      * Filters provided artifacts and selects only defined types based on {@code types} argument
 78  
      * or all types if {@code types} argument is empty.
 79  
      *
 80  
      * @param filteredScopes scopes to be filtered
 81  
      * @param allArtifacts artifacts to be filtered
 82  
      * @param types artifact types to be selected
 83  
      * @return a {@code List} of all project dependencies. Never {@code null}.
 84  
      *      This should maintain dependency order to comply with library project resource precedence.
 85  
      */
 86  
     public Set<Artifact> getFilteredArtifacts( List<String> filteredScopes,
 87  
                                                Iterable<Artifact> allArtifacts, String... types )
 88  
     {
 89  0
         final List<String> acceptTypeList = Arrays.asList( types );
 90  0
         boolean acceptAllArtifacts = acceptTypeList.isEmpty();
 91  0
         final Set<Artifact> results = new LinkedHashSet<Artifact>();
 92  0
         for ( Artifact artifact : allArtifacts )
 93  
         {
 94  0
             if ( artifact == null )
 95  
             {
 96  0
                 continue;
 97  
             }
 98  
 
 99  0
             if ( filteredScopes.contains( artifact.getScope() ) )
 100  
             {
 101  0
                 continue;
 102  
             }
 103  
 
 104  0
             if ( acceptAllArtifacts || acceptTypeList.contains( artifact.getType() ) )
 105  
             {
 106  0
                 results.add( artifact );
 107  
             }
 108  0
         }
 109  0
         return results;
 110  
     }
 111  
 
 112  
     /**
 113  
      * Attempts to resolve an {@link org.apache.maven.artifact.Artifact} to a {@link java.io.File}.
 114  
      *
 115  
      * @param artifact to resolve
 116  
      * @return a {@link java.io.File} to the resolved artifact, never <code>null</code>.
 117  
      * @throws org.apache.maven.plugin.MojoExecutionException if the artifact could not be resolved.
 118  
      */
 119  
     public File resolveArtifactToFile( Artifact artifact ) throws MojoExecutionException
 120  
     {
 121  0
         final Artifact resolvedArtifact = resolveArtifact( artifact );
 122  0
         final File jar = resolvedArtifact.getFile();
 123  0
         if ( jar == null )
 124  
         {
 125  0
             throw new MojoExecutionException( "Could not resolve artifact " + artifact.getId()
 126  
                     + ". Please install it with \"mvn install:install-file ...\" or deploy it to a repository "
 127  
                     + "with \"mvn deploy:deploy-file ...\"" );
 128  
         }
 129  0
         return jar;
 130  
     }
 131  
 
 132  
     public Set<Artifact> resolveArtifacts( Collection<Artifact> artifacts ) throws MojoExecutionException
 133  
     {
 134  0
         final Set<Artifact> resolvedArtifacts = new LinkedHashSet<Artifact>();
 135  0
         for ( final Artifact artifact : artifacts )
 136  
         {
 137  0
             resolvedArtifacts.add( resolveArtifact( artifact ) );
 138  0
         }
 139  0
         return resolvedArtifacts;
 140  
     }
 141  
 
 142  
     /**
 143  
      * Resolves an artifact to a particular repository.
 144  
      *
 145  
      * @param artifact  Artifact to resolve
 146  
      * @return fully resolved artifact.
 147  
      */
 148  
     private Artifact resolveArtifact( Artifact artifact ) throws MojoExecutionException
 149  
     {
 150  0
         final ArtifactResolutionRequest artifactResolutionRequest = new ArtifactResolutionRequest();
 151  0
         artifactResolutionRequest.setArtifact( artifact );
 152  0
         if ( remoteArtifactRepositories != null && !remoteArtifactRepositories.isEmpty() )
 153  
         {
 154  0
             artifactResolutionRequest.setRemoteRepositories( remoteArtifactRepositories );
 155  
         }
 156  0
         final ArtifactResolutionResult resolutionResult = this.artifactResolver.resolve( artifactResolutionRequest );
 157  
 
 158  0
         log.debug( "Resolving : " + artifact );
 159  0
         if ( resolutionResult.getArtifacts().size() == 0 )
 160  
         {
 161  0
             throw new MojoExecutionException( "Could not resolve artifact " + artifact
 162  
                     + ". Please install it with \"mvn install:install-file ...\" or deploy it to a repository "
 163  
                     + "with \"mvn deploy:deploy-file ...\"" );
 164  
         }
 165  0
         if ( resolutionResult.getArtifacts().size() > 1 )
 166  
         {
 167  0
             log.debug( "Resolved artifacts : " + resolutionResult.getArtifacts() );
 168  0
             throw new MojoExecutionException( "Could not resolve artifact " + artifact
 169  
                     + " to single target. Found the following possible options : " + resolutionResult.getArtifacts() );
 170  
         }
 171  
 
 172  0
         final Artifact resolvedArtifact = resolutionResult.getArtifacts().iterator().next();
 173  0
         log.debug( "Resolved : " + resolvedArtifact );
 174  0
         return resolvedArtifact;
 175  
     }
 176  
 }