1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.internal.impl;
20
21 import java.nio.file.Path;
22 import java.util.List;
23
24 import org.apache.maven.api.LocalRepository;
25 import org.apache.maven.api.RemoteRepository;
26 import org.apache.maven.api.Session;
27 import org.apache.maven.api.di.Inject;
28 import org.apache.maven.api.di.Named;
29 import org.apache.maven.api.di.Singleton;
30 import org.apache.maven.api.model.Repository;
31 import org.apache.maven.api.services.RepositoryFactory;
32 import org.eclipse.aether.impl.RemoteRepositoryManager;
33 import org.eclipse.aether.repository.RepositoryPolicy;
34
35 import static org.apache.maven.internal.impl.Utils.nonNull;
36
37 @Named
38 @Singleton
39 public class DefaultRepositoryFactory implements RepositoryFactory {
40
41 final RemoteRepositoryManager remoteRepositoryManager;
42
43 @Inject
44 public DefaultRepositoryFactory(RemoteRepositoryManager remoteRepositoryManager) {
45 this.remoteRepositoryManager = remoteRepositoryManager;
46 }
47
48 @Override
49 public LocalRepository createLocal(Path path) {
50 return new DefaultLocalRepository(new org.eclipse.aether.repository.LocalRepository(path));
51 }
52
53 @Override
54 public RemoteRepository createRemote(String id, String url) {
55 return new DefaultRemoteRepository(
56 new org.eclipse.aether.repository.RemoteRepository.Builder(id, "default", url).build());
57 }
58
59 @Override
60 public RemoteRepository createRemote(Repository repository) throws IllegalArgumentException {
61 return new DefaultRemoteRepository(new org.eclipse.aether.repository.RemoteRepository.Builder(
62 repository.getId(), repository.getLayout(), repository.getUrl())
63 .setReleasePolicy(buildRepositoryPolicy(repository.getReleases()))
64 .setSnapshotPolicy(buildRepositoryPolicy(repository.getSnapshots()))
65 .build());
66 }
67
68 @Override
69 public List<RemoteRepository> aggregate(
70 Session session,
71 List<RemoteRepository> dominant,
72 List<RemoteRepository> recessive,
73 boolean processRecessive) {
74 InternalSession internalSession = InternalSession.from(nonNull(session, "session"));
75 List<org.eclipse.aether.repository.RemoteRepository> repos = remoteRepositoryManager.aggregateRepositories(
76 internalSession.getSession(),
77 internalSession.toRepositories(nonNull(dominant, "dominant")),
78 internalSession.toRepositories(nonNull(recessive, "recessive")),
79 processRecessive);
80 return repos.stream()
81 .<RemoteRepository>map(DefaultRemoteRepository::new)
82 .toList();
83 }
84
85 public static org.eclipse.aether.repository.RepositoryPolicy buildRepositoryPolicy(
86 org.apache.maven.api.model.RepositoryPolicy policy) {
87 boolean enabled = true;
88 String updatePolicy = RepositoryPolicy.UPDATE_POLICY_DAILY;
89 String checksumPolicy = RepositoryPolicy.CHECKSUM_POLICY_FAIL;
90 if (policy != null) {
91 enabled = policy.isEnabled();
92 if (policy.getUpdatePolicy() != null) {
93 updatePolicy = policy.getUpdatePolicy();
94 }
95 if (policy.getChecksumPolicy() != null) {
96 checksumPolicy = policy.getChecksumPolicy();
97 }
98 }
99 return new org.eclipse.aether.repository.RepositoryPolicy(enabled, updatePolicy, checksumPolicy);
100 }
101 }