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