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