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