1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.resolver.internal.ant;
20
21 import java.io.File;
22 import java.util.Collections;
23 import java.util.HashSet;
24 import java.util.List;
25 import java.util.Set;
26
27 import org.apache.maven.model.Dependency;
28 import org.apache.maven.model.Parent;
29 import org.apache.maven.model.Repository;
30 import org.apache.maven.model.building.FileModelSource;
31 import org.apache.maven.model.building.ModelSource;
32 import org.apache.maven.model.resolution.InvalidRepositoryException;
33 import org.apache.maven.model.resolution.ModelResolver;
34 import org.apache.maven.model.resolution.UnresolvableModelException;
35 import org.eclipse.aether.RepositorySystem;
36 import org.eclipse.aether.RepositorySystemSession;
37 import org.eclipse.aether.artifact.Artifact;
38 import org.eclipse.aether.artifact.DefaultArtifact;
39 import org.eclipse.aether.impl.RemoteRepositoryManager;
40 import org.eclipse.aether.repository.RemoteRepository;
41 import org.eclipse.aether.repository.RepositoryPolicy;
42 import org.eclipse.aether.resolution.ArtifactRequest;
43 import org.eclipse.aether.resolution.ArtifactResolutionException;
44
45
46
47
48
49
50 class AntModelResolver implements ModelResolver {
51
52 private final RepositorySystemSession session;
53
54 private final String context;
55
56 private List<org.eclipse.aether.repository.RemoteRepository> repositories;
57
58 private final RepositorySystem repoSys;
59
60 private final RemoteRepositoryManager remoteRepositoryManager;
61
62 private final Set<String> repositoryIds;
63
64 AntModelResolver(
65 final RepositorySystemSession session,
66 final String context,
67 final RepositorySystem repoSys,
68 final RemoteRepositoryManager remoteRepositoryManager,
69 final List<RemoteRepository> repositories) {
70 this.session = session;
71 this.context = context;
72 this.repoSys = repoSys;
73 this.remoteRepositoryManager = remoteRepositoryManager;
74 this.repositories = repositories;
75 this.repositoryIds = new HashSet<>();
76 }
77
78 private AntModelResolver(final AntModelResolver original) {
79 this.session = original.session;
80 this.context = original.context;
81 this.repoSys = original.repoSys;
82 this.remoteRepositoryManager = original.remoteRepositoryManager;
83 this.repositories = original.repositories;
84 this.repositoryIds = new HashSet<>(original.repositoryIds);
85 }
86
87 public void addRepository(final Repository repository) throws InvalidRepositoryException {
88 if (!repositoryIds.add(repository.getId())) {
89 return;
90 }
91
92 final List<RemoteRepository> newRepositories = Collections.singletonList(convert(repository));
93
94 this.repositories = remoteRepositoryManager.aggregateRepositories(session, repositories, newRepositories, true);
95 }
96
97 static RemoteRepository convert(final Repository repository) {
98 final RemoteRepository.Builder builder =
99 new RemoteRepository.Builder(repository.getId(), repository.getLayout(), repository.getUrl());
100 builder.setSnapshotPolicy(convert(repository.getSnapshots()));
101 builder.setReleasePolicy(convert(repository.getReleases()));
102 return builder.build();
103 }
104
105 private static RepositoryPolicy convert(final org.apache.maven.model.RepositoryPolicy policy) {
106 boolean enabled = true;
107 String checksums = RepositoryPolicy.CHECKSUM_POLICY_WARN;
108 String updates = RepositoryPolicy.UPDATE_POLICY_DAILY;
109
110 if (policy != null) {
111 enabled = policy.isEnabled();
112 if (policy.getUpdatePolicy() != null) {
113 updates = policy.getUpdatePolicy();
114 }
115 if (policy.getChecksumPolicy() != null) {
116 checksums = policy.getChecksumPolicy();
117 }
118 }
119
120 return new RepositoryPolicy(enabled, updates, checksums);
121 }
122
123 public ModelResolver newCopy() {
124 return new AntModelResolver(this);
125 }
126
127 public ModelSource resolveModel(final String groupId, final String artifactId, final String version)
128 throws UnresolvableModelException {
129 Artifact pomArtifact = new DefaultArtifact(groupId, artifactId, "", "pom", version);
130
131 try {
132 final ArtifactRequest request = new ArtifactRequest(pomArtifact, repositories, context);
133 pomArtifact = repoSys.resolveArtifact(session, request).getArtifact();
134 } catch (final ArtifactResolutionException e) {
135 throw new UnresolvableModelException(
136 "Failed to resolve POM for " + groupId + ":" + artifactId + ":" + version + " due to "
137 + e.getMessage(),
138 groupId,
139 artifactId,
140 version,
141 e);
142 }
143
144 final File pomFile = pomArtifact.getFile();
145
146 return new FileModelSource(pomFile);
147 }
148
149 @Override
150 public ModelSource resolveModel(final Parent parent) throws UnresolvableModelException {
151 return resolveModel(parent.getGroupId(), parent.getArtifactId(), parent.getVersion());
152 }
153
154 @Override
155 public ModelSource resolveModel(final Dependency dependency) throws UnresolvableModelException {
156 return resolveModel(dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion());
157 }
158
159 @Override
160 public void addRepository(final Repository repository, final boolean replace) throws InvalidRepositoryException {
161 addRepository(repository);
162 }
163 }