Coverage Report - org.apache.maven.plugin.ant.ArtifactResolverWrapper
 
Classes in this File Line Coverage Branch Coverage Complexity
ArtifactResolverWrapper
33%
9/27
N/A
1,417
 
 1  
 package org.apache.maven.plugin.ant;
 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.IOException;
 23  
 import java.util.List;
 24  
 
 25  
 import org.apache.maven.artifact.Artifact;
 26  
 import org.apache.maven.artifact.factory.ArtifactFactory;
 27  
 import org.apache.maven.artifact.repository.ArtifactRepository;
 28  
 import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
 29  
 import org.apache.maven.artifact.resolver.ArtifactResolutionException;
 30  
 import org.apache.maven.artifact.resolver.ArtifactResolver;
 31  
 
 32  
 /**
 33  
  * Wrapper object to resolve artifact.
 34  
  *
 35  
  * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
 36  
  * @version $Id: ArtifactResolverWrapper.java 782444 2009-06-07 19:45:20Z bentmann $
 37  
  */
 38  
 public class ArtifactResolverWrapper
 39  
 {
 40  
     /**
 41  
      * Used for resolving artifacts
 42  
      */
 43  
     private ArtifactResolver resolver;
 44  
 
 45  
     /**
 46  
      * Factory for creating artifact objects
 47  
      */
 48  
     private ArtifactFactory factory;
 49  
 
 50  
     /**
 51  
      * The local repository where the artifacts are located
 52  
      */
 53  
     private ArtifactRepository localRepository;
 54  
 
 55  
     /**
 56  
      * The remote repositories where artifacts are located
 57  
      */
 58  
     private List remoteRepositories;
 59  
 
 60  
     /**
 61  
      * @param resolver
 62  
      * @param factory
 63  
      * @param localRepository
 64  
      * @param remoteRepositories
 65  
      */
 66  
     private ArtifactResolverWrapper( ArtifactResolver resolver, ArtifactFactory factory,
 67  
                                     ArtifactRepository localRepository, List remoteRepositories )
 68  3
     {
 69  3
         this.resolver = resolver;
 70  3
         this.factory = factory;
 71  3
         this.localRepository = localRepository;
 72  3
         this.remoteRepositories = remoteRepositories;
 73  3
     }
 74  
 
 75  
     /**
 76  
      * @param resolver
 77  
      * @param factory
 78  
      * @param localRepository
 79  
      * @param remoteRepositories
 80  
      * @return an instance of ArtifactResolverWrapper
 81  
      */
 82  
     public static ArtifactResolverWrapper getInstance( ArtifactResolver resolver, ArtifactFactory factory,
 83  
                                                       ArtifactRepository localRepository, List remoteRepositories )
 84  
     {
 85  3
         return new ArtifactResolverWrapper( resolver, factory, localRepository, remoteRepositories );
 86  
     }
 87  
 
 88  
     protected ArtifactFactory getFactory()
 89  
     {
 90  0
         return factory;
 91  
     }
 92  
 
 93  
     protected void setFactory( ArtifactFactory factory )
 94  
     {
 95  0
         this.factory = factory;
 96  0
     }
 97  
 
 98  
     protected ArtifactRepository getLocalRepository()
 99  
     {
 100  3
         return localRepository;
 101  
     }
 102  
 
 103  
     protected void setLocalRepository( ArtifactRepository localRepository )
 104  
     {
 105  0
         this.localRepository = localRepository;
 106  0
     }
 107  
 
 108  
     protected List getRemoteRepositories()
 109  
     {
 110  0
         return remoteRepositories;
 111  
     }
 112  
 
 113  
     protected void setRemoteRepositories( List remoteRepositories )
 114  
     {
 115  0
         this.remoteRepositories = remoteRepositories;
 116  0
     }
 117  
 
 118  
     protected ArtifactResolver getResolver()
 119  
     {
 120  0
         return resolver;
 121  
     }
 122  
 
 123  
     protected void setResolver( ArtifactResolver resolver )
 124  
     {
 125  0
         this.resolver = resolver;
 126  0
     }
 127  
 
 128  
     /**
 129  
      * Return the artifact path in the local repository for an artifact defined by its <code>groupId</code>,
 130  
      * its <code>artifactId</code> and its <code>version</code>.
 131  
      *
 132  
      * @param groupId
 133  
      * @param artifactId
 134  
      * @param version
 135  
      * @return the locale artifact path
 136  
      * @throws IOException if any
 137  
      */
 138  
     public String getArtifactAbsolutePath( String groupId, String artifactId, String version )
 139  
         throws IOException
 140  
     {
 141  0
         Artifact artifact = factory.createArtifact( groupId, artifactId, version, "compile", "jar" );
 142  
         try
 143  
         {
 144  0
             resolver.resolve( artifact, remoteRepositories, localRepository );
 145  
 
 146  0
             return artifact.getFile().getAbsolutePath();
 147  
         }
 148  0
         catch ( ArtifactResolutionException e )
 149  
         {
 150  0
             throw new IOException( "Unable to resolve artifact: " + groupId + ":" + artifactId + ":" + version );
 151  
         }
 152  0
         catch ( ArtifactNotFoundException e )
 153  
         {
 154  0
             throw new IOException( "Unable to find artifact: " + groupId + ":" + artifactId + ":" + version );
 155  
         }
 156  
     }
 157  
 
 158  
     /**
 159  
      * Gets the path to the specified artifact relative to the local repository's base directory. Note that this method
 160  
      * does not actually resolve the artifact, it merely calculates the path at which the artifact is or would be stored
 161  
      * in the local repository.
 162  
      * 
 163  
      * @param artifact The artifact whose path should be determined, must not be <code>null</code>.
 164  
      * @return The path to the artifact, never <code>null</code>.
 165  
      */
 166  
     public String getLocalArtifactPath( Artifact artifact )
 167  
     {
 168  
         /*
 169  
          * NOTE: Don't use Artifact.getFile() here because this method could return the path to a JAR from the build
 170  
          * output, e.g. ".../target/some-0.1.jar". The other special case are system-scope artifacts that reside
 171  
          * somewhere outside of the local repository.
 172  
          */
 173  9
         return localRepository.pathOf( artifact );
 174  
     }
 175  
 
 176  
 }