1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 @Override
82 public ArtifactRepository buildArtifactRepository(Repository repository) throws InvalidRepositoryException {
83 return new MavenArtifactRepository(
84 repository.getId(),
85 repository.getUrl(),
86 new DefaultRepositoryLayout(),
87 new ArtifactRepositoryPolicy(),
88 new ArtifactRepositoryPolicy());
89 }
90
91 @Override
92 public Artifact createArtifact(String groupId, String artifactId, String version, String packaging) {
93 return createArtifact(groupId, artifactId, version, null, packaging);
94 }
95
96 @Override
97 public Artifact createArtifact(String groupId, String artifactId, String version, String scope, String type) {
98 return new DefaultArtifact(groupId, artifactId, version, scope, type, null, new TestArtifactHandler(type));
99 }
100
101 @Override
102 public ArtifactRepository createArtifactRepository(
103 String id,
104 String url,
105 ArtifactRepositoryLayout repositoryLayout,
106 ArtifactRepositoryPolicy snapshots,
107 ArtifactRepositoryPolicy releases) {
108 return new MavenArtifactRepository(id, url, repositoryLayout, snapshots, releases);
109 }
110
111 @Override
112 public Artifact createArtifactWithClassifier(
113 String groupId, String artifactId, String version, String type, String classifier) {
114 return new DefaultArtifact(groupId, artifactId, version, null, type, classifier, new TestArtifactHandler(type));
115 }
116
117 @Override
118 public ArtifactRepository createDefaultLocalRepository() throws InvalidRepositoryException {
119 return createLocalRepository(
120 new File(System.getProperty("basedir", "."), "target/local-repo").getAbsoluteFile());
121 }
122
123 @Override
124 public ArtifactRepository createDefaultRemoteRepository() throws InvalidRepositoryException {
125 return new MavenArtifactRepository(
126 DEFAULT_REMOTE_REPO_ID,
127 "file://"
128 + new File(System.getProperty("basedir", "."), "src/test/remote-repo")
129 .getAbsoluteFile()
130 .toURI()
131 .getPath(),
132 new DefaultRepositoryLayout(),
133 new ArtifactRepositoryPolicy(),
134 new ArtifactRepositoryPolicy());
135 }
136
137 @Override
138 public Artifact createDependencyArtifact(Dependency dependency) {
139 Artifact artifact = new DefaultArtifact(
140 dependency.getGroupId(),
141 dependency.getArtifactId(),
142 dependency.getVersion(),
143 dependency.getScope(),
144 dependency.getType(),
145 dependency.getClassifier(),
146 new TestArtifactHandler(dependency.getType()));
147
148 if (Artifact.SCOPE_SYSTEM.equals(dependency.getScope())) {
149 artifact.setFile(new File(dependency.getSystemPath()));
150 artifact.setResolved(true);
151 }
152
153 return artifact;
154 }
155
156 @Override
157 public ArtifactRepository createLocalRepository(File localRepository) throws InvalidRepositoryException {
158 return new MavenArtifactRepository(
159 MavenRepositorySystem.DEFAULT_LOCAL_REPO_ID,
160 "file://" + localRepository.toURI().getPath(),
161 new DefaultRepositoryLayout(),
162 new ArtifactRepositoryPolicy(),
163 new ArtifactRepositoryPolicy());
164 }
165
166 @Override
167 public Artifact createPluginArtifact(Plugin plugin) {
168 VersionRange versionRange;
169 try {
170 String version = plugin.getVersion();
171 if (version == null || version.isEmpty()) {
172 version = "RELEASE";
173 }
174 versionRange = VersionRange.createFromVersionSpec(version);
175 } catch (InvalidVersionSpecificationException e) {
176 return null;
177 }
178
179 return artifactFactory.createPluginArtifact(plugin.getGroupId(), plugin.getArtifactId(), versionRange);
180 }
181
182 @Override
183 public Artifact createProjectArtifact(String groupId, String artifactId, String version) {
184 return createArtifact(groupId, artifactId, version, "pom");
185 }
186
187 @Override
188 public List<ArtifactRepository> getEffectiveRepositories(List<ArtifactRepository> repositories) {
189 return repositories;
190 }
191
192 @Override
193 public Mirror getMirror(ArtifactRepository repository, List<Mirror> mirrors) {
194 return null;
195 }
196
197 @Override
198 public void injectAuthentication(List<ArtifactRepository> repositories, List<Server> servers) {}
199
200 @Override
201 public void injectMirror(List<ArtifactRepository> repositories, List<Mirror> mirrors) {}
202
203 @Override
204 public void injectProxy(List<ArtifactRepository> repositories, List<Proxy> proxies) {}
205
206 @Override
207 public void publish(
208 ArtifactRepository repository, File source, String remotePath, ArtifactTransferListener transferListener)
209 throws ArtifactTransferFailedException {
210
211
212 }
213
214 @Override
215 public ArtifactResolutionResult resolve(ArtifactResolutionRequest request) {
216 ArtifactResolutionResult result = new ArtifactResolutionResult();
217
218 if (request.isResolveRoot()) {
219 try {
220 resolve(request.getArtifact(), request);
221 result.addArtifact(request.getArtifact());
222 } catch (IOException e) {
223 result.addMissingArtifact(request.getArtifact());
224 }
225 }
226
227 if (request.isResolveTransitively()) {
228 Map<String, Artifact> artifacts = new LinkedHashMap<>();
229
230 if (request.getArtifactDependencies() != null) {
231 for (Artifact artifact : request.getArtifactDependencies()) {
232 artifacts.put(artifact.getDependencyConflictId(), artifact);
233 }
234 }
235
236 List<Dependency> dependencies = new ArrayList<>();
237 if (request.getArtifact() instanceof ArtifactWithDependencies) {
238 dependencies = ((ArtifactWithDependencies) request.getArtifact()).getDependencies();
239 } else {
240 Artifact pomArtifact = createProjectArtifact(
241 request.getArtifact().getGroupId(),
242 request.getArtifact().getArtifactId(),
243 request.getArtifact().getVersion());
244 File pomFile = new File(
245 request.getLocalRepository().getBasedir(),
246 request.getLocalRepository().pathOf(pomArtifact));
247
248 try {
249 Model model = modelReader.read(pomFile, null).getDelegate();
250
251 dependencies = Dependency.dependencyToApiV3(model.getDependencies());
252 } catch (IOException e) {
253 e.printStackTrace();
254 }
255 }
256
257 for (Dependency dependency : dependencies) {
258 Artifact artifact = createDependencyArtifact(dependency);
259 if (!artifacts.containsKey(artifact.getDependencyConflictId())) {
260 artifacts.put(artifact.getDependencyConflictId(), artifact);
261 }
262 }
263
264 for (Artifact artifact : artifacts.values()) {
265 try {
266 resolve(artifact, request);
267 result.addArtifact(artifact);
268 } catch (IOException e) {
269 result.addMissingArtifact(artifact);
270 }
271 }
272 }
273
274 return result;
275 }
276
277 private void resolve(Artifact artifact, ArtifactResolutionRequest request) throws IOException {
278 if (Artifact.SCOPE_SYSTEM.equals(artifact.getScope())) {
279 return;
280 }
281
282 ArtifactRepository localRepo = request.getLocalRepository();
283
284 File localFile = new File(localRepo.getBasedir(), localRepo.pathOf(artifact));
285
286 artifact.setFile(localFile);
287
288 if (!localFile.exists()) {
289 if (request.getRemoteRepositories().isEmpty()) {
290 throw new IOException(localFile + " does not exist and no remote repositories are configured");
291 }
292
293 ArtifactRepository remoteRepo = request.getRemoteRepositories().get(0);
294
295 File remoteFile = new File(remoteRepo.getBasedir(), remoteRepo.pathOf(artifact));
296
297 Files.createDirectories(localFile.toPath().getParent());
298 Files.copy(remoteFile.toPath(), localFile.toPath());
299 }
300
301 artifact.setResolved(true);
302 }
303
304 @Override
305 public void retrieve(
306 ArtifactRepository repository,
307 File destination,
308 String remotePath,
309 ArtifactTransferListener transferListener)
310 throws ArtifactTransferFailedException, ArtifactDoesNotExistException {
311
312
313 }
314
315 @Override
316 public void injectMirror(RepositorySystemSession session, List<ArtifactRepository> repositories) {}
317
318 @Override
319 public void injectProxy(RepositorySystemSession session, List<ArtifactRepository> repositories) {}
320
321 @Override
322 public void injectAuthentication(RepositorySystemSession session, List<ArtifactRepository> repositories) {}
323 }