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