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