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