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 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 639646 2008-03-21 13:58:11Z 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      {
69          this.resolver = resolver;
70          this.factory = factory;
71          this.localRepository = localRepository;
72          this.remoteRepositories = remoteRepositories;
73      }
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          return new ArtifactResolverWrapper( resolver, factory, localRepository, remoteRepositories );
86      }
87  
88      protected ArtifactFactory getFactory()
89      {
90          return factory;
91      }
92  
93      protected void setFactory( ArtifactFactory factory )
94      {
95          this.factory = factory;
96      }
97  
98      protected ArtifactRepository getLocalRepository()
99      {
100         return localRepository;
101     }
102 
103     protected void setLocalRepository( ArtifactRepository localRepository )
104     {
105         this.localRepository = localRepository;
106     }
107 
108     protected List getRemoteRepositories()
109     {
110         return remoteRepositories;
111     }
112 
113     protected void setRemoteRepositories( List remoteRepositories )
114     {
115         this.remoteRepositories = remoteRepositories;
116     }
117 
118     protected ArtifactResolver getResolver()
119     {
120         return resolver;
121     }
122 
123     protected void setResolver( ArtifactResolver resolver )
124     {
125         this.resolver = resolver;
126     }
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         Artifact artifact = factory.createArtifact( groupId, artifactId, version, "compile", "jar" );
142         try
143         {
144             resolver.resolve( artifact, remoteRepositories, localRepository );
145 
146             return artifact.getFile().getAbsolutePath();
147         }
148         catch ( ArtifactResolutionException e )
149         {
150             throw new IOException( "Unable to resolve artifact: " + groupId + ":" + artifactId + ":" + version );
151         }
152         catch ( ArtifactNotFoundException e )
153         {
154             throw new IOException( "Unable to find artifact: " + groupId + ":" + artifactId + ":" + version );
155         }
156     }
157 }