View Javadoc
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 org.apache.maven.artifact.Artifact;
23  import org.apache.maven.artifact.factory.ArtifactFactory;
24  import org.apache.maven.artifact.repository.ArtifactRepository;
25  import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
26  import org.apache.maven.artifact.resolver.ArtifactResolutionException;
27  import org.apache.maven.artifact.resolver.ArtifactResolver;
28  
29  import java.io.IOException;
30  import java.util.List;
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 1645084 2014-12-12 22:28:31Z khmarbaise $
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      {
69          this.resolver = resolver;
70          this.factory = factory;
71          this.localRepository = localRepository;
72          this.remoteRepositories = remoteRepositories;
73      }
74  
75      /**
76       * @param resolver {@link ArtifactResolver}
77       * @param factory {@link ArtifactFactory}
78       * @param localRepository {@link ArtifactRepository}
79       * @param remoteRepositories {@link XX}.
80       * @return an instance of ArtifactResolverWrapper
81       */
82      public static ArtifactResolverWrapper getInstance( ArtifactResolver resolver, ArtifactFactory factory,
83                                                         ArtifactRepository localRepository, List remoteRepositories )
84      {
85          return new ArtifactResolverWrapper( resolver, factory, localRepository, remoteRepositories );
86      }
87  
88      /**
89       * @return {@link #factory}
90       */
91      protected ArtifactFactory getFactory()
92      {
93          return factory;
94      }
95  
96      /**
97       * @param factory {@link ArtifactFactory}
98       */
99      protected void setFactory( ArtifactFactory factory )
100     {
101         this.factory = factory;
102     }
103 
104     /**
105      * @return {@link #localRepository}
106      */
107     protected ArtifactRepository getLocalRepository()
108     {
109         return localRepository;
110     }
111 
112     /**
113      * @param localRepository set {@link #localRepository}
114      */
115     protected void setLocalRepository( ArtifactRepository localRepository )
116     {
117         this.localRepository = localRepository;
118     }
119 
120     /**
121      * @return {@link #remoteRepositories}
122      */
123     protected List getRemoteRepositories()
124     {
125         return remoteRepositories;
126     }
127 
128     /**
129      * @param remoteRepositories {@link #remoteRepositories}
130      */
131     protected void setRemoteRepositories( List remoteRepositories )
132     {
133         this.remoteRepositories = remoteRepositories;
134     }
135 
136     /**
137      * @return {@link #resolver}
138      */
139     protected ArtifactResolver getResolver()
140     {
141         return resolver;
142     }
143 
144     /**
145      * @param resolver {@link #resolver}
146      */
147     protected void setResolver( ArtifactResolver resolver )
148     {
149         this.resolver = resolver;
150     }
151 
152     /**
153      * Return the artifact path in the local repository for an artifact defined by its <code>groupId</code>,
154      * its <code>artifactId</code> and its <code>version</code>.
155      *
156      * @param groupId The groupId.
157      * @param artifactId The artifactId.
158      * @param version The version.
159      * @return the locale artifact path
160      * @throws IOException if any
161      */
162     public String getArtifactAbsolutePath( String groupId, String artifactId, String version )
163         throws IOException
164     {
165         Artifact artifact = factory.createArtifact( groupId, artifactId, version, "compile", "jar" );
166         try
167         {
168             resolver.resolve( artifact, remoteRepositories, localRepository );
169 
170             return artifact.getFile().getAbsolutePath();
171         }
172         catch ( ArtifactResolutionException e )
173         {
174             throw new IOException( "Unable to resolve artifact: " + groupId + ":" + artifactId + ":" + version );
175         }
176         catch ( ArtifactNotFoundException e )
177         {
178             throw new IOException( "Unable to find artifact: " + groupId + ":" + artifactId + ":" + version );
179         }
180     }
181 
182     /**
183      * Gets the path to the specified artifact relative to the local repository's base directory. Note that this method
184      * does not actually resolve the artifact, it merely calculates the path at which the artifact is or would be stored
185      * in the local repository.
186      *
187      * @param artifact The artifact whose path should be determined, must not be <code>null</code>.
188      * @return The path to the artifact, never <code>null</code>.
189      */
190     public String getLocalArtifactPath( Artifact artifact )
191     {
192         /*
193          * NOTE: Don't use Artifact.getFile() here because this method could return the path to a JAR from the build
194          * output, e.g. ".../target/some-0.1.jar". The other special case are system-scope artifacts that reside
195          * somewhere outside of the local repository.
196          */
197         return localRepository.pathOf( artifact );
198     }
199 
200 }