View Javadoc

1   package org.apache.maven.repository;
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.File;
23  import java.io.IOException;
24  import java.util.ArrayList;
25  import java.util.LinkedHashMap;
26  import java.util.List;
27  import java.util.Map;
28  
29  import org.apache.maven.artifact.Artifact;
30  import org.apache.maven.artifact.DefaultArtifact;
31  import org.apache.maven.artifact.InvalidRepositoryException;
32  import org.apache.maven.artifact.repository.ArtifactRepository;
33  import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
34  import org.apache.maven.artifact.repository.MavenArtifactRepository;
35  import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
36  import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
37  import org.apache.maven.artifact.resolver.ArtifactResolutionRequest;
38  import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
39  import org.apache.maven.model.Dependency;
40  import org.apache.maven.model.Model;
41  import org.apache.maven.model.Plugin;
42  import org.apache.maven.model.Repository;
43  import org.apache.maven.model.io.ModelReader;
44  import org.apache.maven.project.artifact.ArtifactWithDependencies;
45  import org.apache.maven.settings.Mirror;
46  import org.apache.maven.settings.Proxy;
47  import org.apache.maven.settings.Server;
48  import org.codehaus.plexus.component.annotations.Component;
49  import org.codehaus.plexus.component.annotations.Requirement;
50  import org.codehaus.plexus.util.FileUtils;
51  import org.sonatype.aether.RepositorySystemSession;
52  
53  /**
54   * @author Benjamin Bentmann
55   */
56  @Component( role = RepositorySystem.class )
57  public class TestRepositorySystem
58      implements RepositorySystem
59  {
60  
61      @Requirement
62      private ModelReader modelReader;
63  
64      public ArtifactRepository buildArtifactRepository( Repository repository )
65          throws InvalidRepositoryException
66      {
67          return new MavenArtifactRepository( repository.getId(), repository.getUrl(), new DefaultRepositoryLayout(),
68                                              new ArtifactRepositoryPolicy(), new ArtifactRepositoryPolicy() );
69      }
70  
71      public Artifact createArtifact( String groupId, String artifactId, String version, String packaging )
72      {
73          return createArtifact( groupId, artifactId, version, null, packaging );
74      }
75  
76      public Artifact createArtifact( String groupId, String artifactId, String version, String scope, String type )
77      {
78          return new DefaultArtifact( groupId, artifactId, version, scope, type, null, new TestArtifactHandler( type ) );
79      }
80  
81      public ArtifactRepository createArtifactRepository( String id, String url,
82                                                          ArtifactRepositoryLayout repositoryLayout,
83                                                          ArtifactRepositoryPolicy snapshots,
84                                                          ArtifactRepositoryPolicy releases )
85      {
86          return new MavenArtifactRepository( id, url, repositoryLayout, snapshots, releases );
87      }
88  
89      public Artifact createArtifactWithClassifier( String groupId, String artifactId, String version, String type,
90                                                    String classifier )
91      {
92          return new DefaultArtifact( groupId, artifactId, version, null, type, classifier,
93                                      new TestArtifactHandler( type ) );
94      }
95  
96      public ArtifactRepository createDefaultLocalRepository()
97          throws InvalidRepositoryException
98      {
99          return createLocalRepository( new File( System.getProperty( "basedir", "" ), "target/local-repo" ).getAbsoluteFile() );
100     }
101 
102     public ArtifactRepository createDefaultRemoteRepository()
103         throws InvalidRepositoryException
104     {
105         return new MavenArtifactRepository( DEFAULT_REMOTE_REPO_ID, "file://"
106             + new File( System.getProperty( "basedir", "" ), "src/test/remote-repo" ).toURI().getPath(),
107                                             new DefaultRepositoryLayout(), new ArtifactRepositoryPolicy(),
108                                             new ArtifactRepositoryPolicy() );
109     }
110 
111     public Artifact createDependencyArtifact( Dependency dependency )
112     {
113         Artifact artifact =
114             new DefaultArtifact( dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion(),
115                                  dependency.getScope(), dependency.getType(), dependency.getClassifier(),
116                                  new TestArtifactHandler( dependency.getType() ) );
117 
118         if ( Artifact.SCOPE_SYSTEM.equals( dependency.getScope() ) )
119         {
120             artifact.setFile( new File( dependency.getSystemPath() ) );
121             artifact.setResolved( true );
122         }
123 
124         return artifact;
125     }
126 
127     public ArtifactRepository createLocalRepository( File localRepository )
128         throws InvalidRepositoryException
129     {
130         return new MavenArtifactRepository( DEFAULT_LOCAL_REPO_ID, "file://" + localRepository.toURI().getPath(),
131                                             new DefaultRepositoryLayout(), new ArtifactRepositoryPolicy(),
132                                             new ArtifactRepositoryPolicy() );
133     }
134 
135     public Artifact createPluginArtifact( Plugin plugin )
136     {
137         return new DefaultArtifact( plugin.getGroupId(), plugin.getArtifactId(), plugin.getVersion(), null,
138                                     "maven-plugin", null, new TestArtifactHandler( "maven-plugin", "jar" ) );
139     }
140 
141     public Artifact createProjectArtifact( String groupId, String artifactId, String version )
142     {
143         return createArtifact( groupId, artifactId, version, "pom" );
144     }
145 
146     public List<ArtifactRepository> getEffectiveRepositories( List<ArtifactRepository> repositories )
147     {
148         return repositories;
149     }
150 
151     public Mirror getMirror( ArtifactRepository repository, List<Mirror> mirrors )
152     {
153         return null;
154     }
155 
156     public void injectAuthentication( List<ArtifactRepository> repositories, List<Server> servers )
157     {
158     }
159 
160     public void injectMirror( List<ArtifactRepository> repositories, List<Mirror> mirrors )
161     {
162     }
163 
164     public void injectProxy( List<ArtifactRepository> repositories, List<Proxy> proxies )
165     {
166     }
167 
168     public void publish( ArtifactRepository repository, File source, String remotePath,
169                          ArtifactTransferListener transferListener )
170         throws ArtifactTransferFailedException
171     {
172         // TODO Auto-generated method stub
173 
174     }
175 
176     public ArtifactResolutionResult resolve( ArtifactResolutionRequest request )
177     {
178         ArtifactResolutionResult result = new ArtifactResolutionResult();
179 
180         if ( request.isResolveRoot() )
181         {
182             try
183             {
184                 resolve( request.getArtifact(), request );
185                 result.addArtifact( request.getArtifact() );
186             }
187             catch ( IOException e )
188             {
189                 result.addMissingArtifact( request.getArtifact() );
190             }
191         }
192 
193         if ( request.isResolveTransitively() )
194         {
195             Map<String, Artifact> artifacts = new LinkedHashMap<String, Artifact>();
196 
197             if ( request.getArtifactDependencies() != null )
198             {
199                 for ( Artifact artifact : request.getArtifactDependencies() )
200                 {
201                     artifacts.put( artifact.getDependencyConflictId(), artifact );
202                 }
203             }
204 
205             List<Dependency> dependencies = new ArrayList<Dependency>();
206             if ( request.getArtifact() instanceof ArtifactWithDependencies )
207             {
208                 dependencies = ( (ArtifactWithDependencies) request.getArtifact() ).getDependencies();
209             }
210             else
211             {
212                 Artifact pomArtifact =
213                     createProjectArtifact( request.getArtifact().getGroupId(), request.getArtifact().getArtifactId(),
214                                            request.getArtifact().getVersion() );
215                 File pomFile =
216                     new File( request.getLocalRepository().getBasedir(),
217                               request.getLocalRepository().pathOf( pomArtifact ) );
218 
219                 try
220                 {
221                     Model model = modelReader.read( pomFile, null );
222 
223                     dependencies = model.getDependencies();
224                 }
225                 catch ( IOException e )
226                 {
227                     e.printStackTrace();
228                 }
229             }
230 
231             for ( Dependency dependency : dependencies )
232             {
233                 Artifact artifact = createDependencyArtifact( dependency );
234                 if ( !artifacts.containsKey( artifact.getDependencyConflictId() ) )
235                 {
236                     artifacts.put( artifact.getDependencyConflictId(), artifact );
237                 }
238             }
239 
240             for ( Artifact artifact : artifacts.values() )
241             {
242                 try
243                 {
244                     resolve( artifact, request );
245                     result.addArtifact( artifact );
246                 }
247                 catch ( IOException e )
248                 {
249                     result.addMissingArtifact( artifact );
250                 }
251             }
252         }
253 
254         return result;
255     }
256 
257     private void resolve( Artifact artifact, ArtifactResolutionRequest request )
258         throws IOException
259     {
260         if ( Artifact.SCOPE_SYSTEM.equals( artifact.getScope() ) )
261         {
262             return;
263         }
264 
265         ArtifactRepository localRepo = request.getLocalRepository();
266 
267         File localFile = new File( localRepo.getBasedir(), localRepo.pathOf( artifact ) );
268 
269         artifact.setFile( localFile );
270 
271         if ( !localFile.exists() )
272         {
273             if ( request.getRemoteRepositories().isEmpty() )
274             {
275                 throw new IOException( localFile + " does not exist and no remote repositories are configured" );
276             }
277 
278             ArtifactRepository remoteRepo = request.getRemoteRepositories().get( 0 );
279 
280             File remoteFile = new File( remoteRepo.getBasedir(), remoteRepo.pathOf( artifact ) );
281 
282             FileUtils.copyFile( remoteFile, localFile );
283         }
284 
285         artifact.setResolved( true );
286     }
287 
288     public void retrieve( ArtifactRepository repository, File destination, String remotePath,
289                           ArtifactTransferListener transferListener )
290         throws ArtifactTransferFailedException, ArtifactDoesNotExistException
291     {
292         // TODO Auto-generated method stub
293 
294     }
295 
296     public void injectMirror( RepositorySystemSession session, List<ArtifactRepository> repositories )
297     {
298     }
299 
300     public void injectProxy( RepositorySystemSession session, List<ArtifactRepository> repositories )
301     {
302     }
303 
304     public void injectAuthentication( RepositorySystemSession session, List<ArtifactRepository> repositories )
305     {
306     }
307 
308 }