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