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