1 package org.apache.maven.repository.internal;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.HashSet;
26 import java.util.Iterator;
27 import java.util.List;
28 import java.util.Set;
29
30 import org.apache.maven.model.Dependency;
31 import org.apache.maven.model.Parent;
32 import org.apache.maven.model.Repository;
33 import org.apache.maven.model.building.FileModelSource;
34 import org.apache.maven.model.building.ModelSource;
35 import org.apache.maven.model.resolution.InvalidRepositoryException;
36 import org.apache.maven.model.resolution.ModelResolver;
37 import org.apache.maven.model.resolution.UnresolvableModelException;
38 import org.eclipse.aether.RepositorySystemSession;
39 import org.eclipse.aether.RequestTrace;
40 import org.eclipse.aether.artifact.Artifact;
41 import org.eclipse.aether.artifact.DefaultArtifact;
42 import org.eclipse.aether.impl.ArtifactResolver;
43 import org.eclipse.aether.impl.RemoteRepositoryManager;
44 import org.eclipse.aether.impl.VersionRangeResolver;
45 import org.eclipse.aether.repository.RemoteRepository;
46 import org.eclipse.aether.resolution.ArtifactRequest;
47 import org.eclipse.aether.resolution.ArtifactResolutionException;
48 import org.eclipse.aether.resolution.VersionRangeRequest;
49 import org.eclipse.aether.resolution.VersionRangeResolutionException;
50 import org.eclipse.aether.resolution.VersionRangeResult;
51
52
53
54
55
56
57
58
59 class DefaultModelResolver
60 implements ModelResolver
61 {
62
63 private final RepositorySystemSession session;
64
65 private final RequestTrace trace;
66
67 private final String context;
68
69 private List<RemoteRepository> repositories;
70
71 private final List<RemoteRepository> externalRepositories;
72
73 private final ArtifactResolver resolver;
74
75 private final VersionRangeResolver versionRangeResolver;
76
77 private final RemoteRepositoryManager remoteRepositoryManager;
78
79 private final Set<String> repositoryIds;
80
81 DefaultModelResolver( RepositorySystemSession session, RequestTrace trace, String context,
82 ArtifactResolver resolver, VersionRangeResolver versionRangeResolver,
83 RemoteRepositoryManager remoteRepositoryManager, List<RemoteRepository> repositories )
84 {
85 this.session = session;
86 this.trace = trace;
87 this.context = context;
88 this.resolver = resolver;
89 this.versionRangeResolver = versionRangeResolver;
90 this.remoteRepositoryManager = remoteRepositoryManager;
91 this.repositories = repositories;
92 List<RemoteRepository> externalRepositories = new ArrayList<>();
93 externalRepositories.addAll( repositories );
94 this.externalRepositories = Collections.unmodifiableList( externalRepositories );
95
96 this.repositoryIds = new HashSet<>();
97 }
98
99 private DefaultModelResolver( DefaultModelResolver original )
100 {
101 this.session = original.session;
102 this.trace = original.trace;
103 this.context = original.context;
104 this.resolver = original.resolver;
105 this.versionRangeResolver = original.versionRangeResolver;
106 this.remoteRepositoryManager = original.remoteRepositoryManager;
107 this.repositories = new ArrayList<>( original.repositories );
108 this.externalRepositories = original.externalRepositories;
109 this.repositoryIds = new HashSet<>( original.repositoryIds );
110 }
111
112 @Override
113 public void addRepository( Repository repository )
114 throws InvalidRepositoryException
115 {
116 addRepository( repository, false );
117 }
118
119 @Override
120 public void addRepository( final Repository repository, boolean replace )
121 throws InvalidRepositoryException
122 {
123 if ( session.isIgnoreArtifactDescriptorRepositories() )
124 {
125 return;
126 }
127
128 if ( !repositoryIds.add( repository.getId() ) )
129 {
130 if ( !replace )
131 {
132 return;
133 }
134
135 removeMatchingRepository( repositories, repository.getId() );
136 }
137
138 List<RemoteRepository> newRepositories =
139 Collections.singletonList( ArtifactDescriptorUtils.toRemoteRepository( repository ) );
140
141 this.repositories =
142 remoteRepositoryManager.aggregateRepositories( session, repositories, newRepositories, true );
143 }
144
145 private static void removeMatchingRepository( Iterable<RemoteRepository> repositories, final String id )
146 {
147 Iterator<RemoteRepository> iterator = repositories.iterator();
148 while ( iterator.hasNext() )
149 {
150 RemoteRepository remoteRepository = iterator.next();
151 if ( remoteRepository.getId().equals( id ) )
152 {
153 iterator.remove();
154 }
155 }
156 }
157
158 @Override
159 public ModelResolver newCopy()
160 {
161 return new DefaultModelResolver( this );
162 }
163
164 @Override
165 public ModelSource resolveModel( String groupId, String artifactId, String version )
166 throws UnresolvableModelException
167 {
168 Artifact pomArtifact = new DefaultArtifact( groupId, artifactId, "", "pom", version );
169
170 try
171 {
172 ArtifactRequest request = new ArtifactRequest( pomArtifact, repositories, context );
173 request.setTrace( trace );
174 pomArtifact = resolver.resolveArtifact( session, request ).getArtifact();
175 }
176 catch ( ArtifactResolutionException e )
177 {
178 throw new UnresolvableModelException( e.getMessage(), groupId, artifactId, version, e );
179 }
180
181 File pomFile = pomArtifact.getFile();
182
183 return new FileModelSource( pomFile );
184 }
185
186 @Override
187 public ModelSource resolveModel( final Parent parent )
188 throws UnresolvableModelException
189 {
190 try
191 {
192 final Artifact artifact = new DefaultArtifact( parent.getGroupId(), parent.getArtifactId(), "", "pom",
193 parent.getVersion() );
194
195 final VersionRangeRequest versionRangeRequest = new VersionRangeRequest( artifact, repositories, context );
196 versionRangeRequest.setTrace( trace );
197
198 final VersionRangeResult versionRangeResult =
199 versionRangeResolver.resolveVersionRange( session, versionRangeRequest );
200
201 if ( versionRangeResult.getHighestVersion() == null )
202 {
203 throw new UnresolvableModelException(
204 String.format( "No versions matched the requested parent version range '%s'",
205 parent.getVersion() ),
206 parent.getGroupId(), parent.getArtifactId(), parent.getVersion() );
207
208 }
209
210 if ( versionRangeResult.getVersionConstraint() != null
211 && versionRangeResult.getVersionConstraint().getRange() != null
212 && versionRangeResult.getVersionConstraint().getRange().getUpperBound() == null )
213 {
214
215 throw new UnresolvableModelException(
216 String.format( "The requested parent version range '%s' does not specify an upper bound",
217 parent.getVersion() ),
218 parent.getGroupId(), parent.getArtifactId(), parent.getVersion() );
219
220 }
221
222 parent.setVersion( versionRangeResult.getHighestVersion().toString() );
223
224 return resolveModel( parent.getGroupId(), parent.getArtifactId(), parent.getVersion() );
225 }
226 catch ( final VersionRangeResolutionException e )
227 {
228 throw new UnresolvableModelException( e.getMessage(), parent.getGroupId(), parent.getArtifactId(),
229 parent.getVersion(), e );
230
231 }
232 }
233
234 @Override
235 public ModelSource resolveModel( final Dependency dependency )
236 throws UnresolvableModelException
237 {
238 try
239 {
240 final Artifact artifact = new DefaultArtifact( dependency.getGroupId(), dependency.getArtifactId(), "",
241 "pom", dependency.getVersion() );
242
243 final VersionRangeRequest versionRangeRequest = new VersionRangeRequest( artifact, repositories, context );
244 versionRangeRequest.setTrace( trace );
245
246 final VersionRangeResult versionRangeResult =
247 versionRangeResolver.resolveVersionRange( session, versionRangeRequest );
248
249 if ( versionRangeResult.getHighestVersion() == null )
250 {
251 throw new UnresolvableModelException(
252 String.format( "No versions matched the requested dependency version range '%s'",
253 dependency.getVersion() ),
254 dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion() );
255
256 }
257
258 if ( versionRangeResult.getVersionConstraint() != null
259 && versionRangeResult.getVersionConstraint().getRange() != null
260 && versionRangeResult.getVersionConstraint().getRange().getUpperBound() == null )
261 {
262
263 throw new UnresolvableModelException(
264 String.format( "The requested dependency version range '%s' does not specify an upper bound",
265 dependency.getVersion() ),
266 dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion() );
267
268 }
269
270 dependency.setVersion( versionRangeResult.getHighestVersion().toString() );
271
272 return resolveModel( dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion() );
273 }
274 catch ( VersionRangeResolutionException e )
275 {
276 throw new UnresolvableModelException( e.getMessage(), dependency.getGroupId(), dependency.getArtifactId(),
277 dependency.getVersion(), e );
278
279 }
280 }
281 }