View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.repository;
20  
21  import javax.inject.Inject;
22  import javax.inject.Named;
23  import javax.inject.Singleton;
24  
25  import java.io.File;
26  import java.io.IOException;
27  import java.util.ArrayList;
28  import java.util.LinkedHashMap;
29  import java.util.List;
30  import java.util.Map;
31  import java.util.stream.Collectors;
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.settings.Mirror;
53  import org.apache.maven.settings.Proxy;
54  import org.apache.maven.settings.Server;
55  import org.codehaus.plexus.util.FileUtils;
56  import org.codehaus.plexus.util.StringUtils;
57  import org.eclipse.aether.RepositorySystemSession;
58  
59  /**
60   * @author Benjamin Bentmann
61   */
62  @Named
63  @Singleton
64  public class TestRepositorySystem implements RepositorySystem {
65  
66      private final ModelReader modelReader;
67  
68      private final ArtifactFactory artifactFactory;
69  
70      @Inject
71      public TestRepositorySystem(ModelReader modelReader, ArtifactFactory artifactFactory) {
72          this.modelReader = modelReader;
73          this.artifactFactory = artifactFactory;
74      }
75  
76      public ArtifactRepository buildArtifactRepository(Repository repository) throws InvalidRepositoryException {
77          return new MavenArtifactRepository(
78                  repository.getId(),
79                  repository.getUrl(),
80                  new DefaultRepositoryLayout(),
81                  new ArtifactRepositoryPolicy(),
82                  new ArtifactRepositoryPolicy());
83      }
84  
85      public Artifact createArtifact(String groupId, String artifactId, String version, String packaging) {
86          return createArtifact(groupId, artifactId, version, null, packaging);
87      }
88  
89      public Artifact createArtifact(String groupId, String artifactId, String version, String scope, String type) {
90          return new DefaultArtifact(groupId, artifactId, version, scope, type, null, new TestArtifactHandler(type));
91      }
92  
93      public ArtifactRepository createArtifactRepository(
94              String id,
95              String url,
96              ArtifactRepositoryLayout repositoryLayout,
97              ArtifactRepositoryPolicy snapshots,
98              ArtifactRepositoryPolicy releases) {
99          return new MavenArtifactRepository(id, url, repositoryLayout, snapshots, releases);
100     }
101 
102     public Artifact createArtifactWithClassifier(
103             String groupId, String artifactId, String version, String type, String classifier) {
104         return new DefaultArtifact(groupId, artifactId, version, null, type, classifier, new TestArtifactHandler(type));
105     }
106 
107     public ArtifactRepository createDefaultLocalRepository() throws InvalidRepositoryException {
108         return createLocalRepository(
109                 new File(System.getProperty("basedir", "."), "target/local-repo").getAbsoluteFile());
110     }
111 
112     public ArtifactRepository createDefaultRemoteRepository() throws InvalidRepositoryException {
113         return new MavenArtifactRepository(
114                 DEFAULT_REMOTE_REPO_ID,
115                 "file://"
116                         + new File(System.getProperty("basedir", "."), "src/test/remote-repo")
117                                 .getAbsoluteFile()
118                                 .toURI()
119                                 .getPath(),
120                 new DefaultRepositoryLayout(),
121                 new ArtifactRepositoryPolicy(),
122                 new ArtifactRepositoryPolicy());
123     }
124 
125     public Artifact createDependencyArtifact(Dependency dependency) {
126         Artifact artifact = new DefaultArtifact(
127                 dependency.getGroupId(),
128                 dependency.getArtifactId(),
129                 dependency.getVersion(),
130                 dependency.getScope(),
131                 dependency.getType(),
132                 dependency.getClassifier(),
133                 new TestArtifactHandler(dependency.getType()));
134 
135         if (Artifact.SCOPE_SYSTEM.equals(dependency.getScope())) {
136             artifact.setFile(new File(dependency.getSystemPath()));
137             artifact.setResolved(true);
138         }
139 
140         return artifact;
141     }
142 
143     public ArtifactRepository createLocalRepository(File localRepository) throws InvalidRepositoryException {
144         return new MavenArtifactRepository(
145                 DEFAULT_LOCAL_REPO_ID,
146                 "file://" + localRepository.toURI().getPath(),
147                 new DefaultRepositoryLayout(),
148                 new ArtifactRepositoryPolicy(),
149                 new ArtifactRepositoryPolicy());
150     }
151 
152     public Artifact createPluginArtifact(Plugin plugin) {
153         VersionRange versionRange;
154         try {
155             String version = plugin.getVersion();
156             if (StringUtils.isEmpty(version)) {
157                 version = "RELEASE";
158             }
159             versionRange = VersionRange.createFromVersionSpec(version);
160         } catch (InvalidVersionSpecificationException e) {
161             return null;
162         }
163 
164         return artifactFactory.createPluginArtifact(plugin.getGroupId(), plugin.getArtifactId(), versionRange);
165     }
166 
167     public Artifact createProjectArtifact(String groupId, String artifactId, String version) {
168         return createArtifact(groupId, artifactId, version, "pom");
169     }
170 
171     public List<ArtifactRepository> getEffectiveRepositories(List<ArtifactRepository> repositories) {
172         return repositories;
173     }
174 
175     public Mirror getMirror(ArtifactRepository repository, List<Mirror> mirrors) {
176         return null;
177     }
178 
179     public void injectAuthentication(List<ArtifactRepository> repositories, List<Server> servers) {}
180 
181     public void injectMirror(List<ArtifactRepository> repositories, List<Mirror> mirrors) {}
182 
183     public void injectProxy(List<ArtifactRepository> repositories, List<Proxy> proxies) {}
184 
185     public void publish(
186             ArtifactRepository repository, File source, String remotePath, ArtifactTransferListener transferListener)
187             throws ArtifactTransferFailedException {
188         // TODO Auto-generated method stub
189 
190     }
191 
192     public ArtifactResolutionResult resolve(ArtifactResolutionRequest request) {
193         ArtifactResolutionResult result = new ArtifactResolutionResult();
194 
195         if (request.isResolveRoot()) {
196             try {
197                 resolve(request.getArtifact(), request);
198                 result.addArtifact(request.getArtifact());
199             } catch (IOException e) {
200                 result.addMissingArtifact(request.getArtifact());
201             }
202         }
203 
204         if (request.isResolveTransitively()) {
205             Map<String, Artifact> artifacts = new LinkedHashMap<>();
206 
207             if (request.getArtifactDependencies() != null) {
208                 for (Artifact artifact : request.getArtifactDependencies()) {
209                     artifacts.put(artifact.getDependencyConflictId(), artifact);
210                 }
211             }
212 
213             List<Dependency> dependencies = new ArrayList<>();
214             if (request.getArtifact() instanceof ArtifactWithDependencies) {
215                 dependencies = ((ArtifactWithDependencies) request.getArtifact()).getDependencies();
216             } else {
217                 Artifact pomArtifact = createProjectArtifact(
218                         request.getArtifact().getGroupId(),
219                         request.getArtifact().getArtifactId(),
220                         request.getArtifact().getVersion());
221                 File pomFile = new File(
222                         request.getLocalRepository().getBasedir(),
223                         request.getLocalRepository().pathOf(pomArtifact));
224 
225                 try {
226                     Model model = modelReader.read(pomFile, null).getDelegate();
227 
228                     dependencies = model.getDependencies().stream()
229                             .map(Dependency::new)
230                             .collect(Collectors.toList());
231                 } catch (IOException e) {
232                     e.printStackTrace();
233                 }
234             }
235 
236             for (Dependency dependency : dependencies) {
237                 Artifact artifact = createDependencyArtifact(dependency);
238                 if (!artifacts.containsKey(artifact.getDependencyConflictId())) {
239                     artifacts.put(artifact.getDependencyConflictId(), artifact);
240                 }
241             }
242 
243             for (Artifact artifact : artifacts.values()) {
244                 try {
245                     resolve(artifact, request);
246                     result.addArtifact(artifact);
247                 } catch (IOException e) {
248                     result.addMissingArtifact(artifact);
249                 }
250             }
251         }
252 
253         return result;
254     }
255 
256     private void resolve(Artifact artifact, ArtifactResolutionRequest request) throws IOException {
257         if (Artifact.SCOPE_SYSTEM.equals(artifact.getScope())) {
258             return;
259         }
260 
261         ArtifactRepository localRepo = request.getLocalRepository();
262 
263         File localFile = new File(localRepo.getBasedir(), localRepo.pathOf(artifact));
264 
265         artifact.setFile(localFile);
266 
267         if (!localFile.exists()) {
268             if (request.getRemoteRepositories().isEmpty()) {
269                 throw new IOException(localFile + " does not exist and no remote repositories are configured");
270             }
271 
272             ArtifactRepository remoteRepo = request.getRemoteRepositories().get(0);
273 
274             File remoteFile = new File(remoteRepo.getBasedir(), remoteRepo.pathOf(artifact));
275 
276             FileUtils.copyFile(remoteFile, localFile);
277         }
278 
279         artifact.setResolved(true);
280     }
281 
282     public void retrieve(
283             ArtifactRepository repository,
284             File destination,
285             String remotePath,
286             ArtifactTransferListener transferListener)
287             throws ArtifactTransferFailedException, ArtifactDoesNotExistException {
288         // TODO Auto-generated method stub
289 
290     }
291 
292     public void injectMirror(RepositorySystemSession session, List<ArtifactRepository> repositories) {}
293 
294     public void injectProxy(RepositorySystemSession session, List<ArtifactRepository> repositories) {}
295 
296     public void injectAuthentication(RepositorySystemSession session, List<ArtifactRepository> repositories) {}
297 }