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.List;
27 import java.util.Set;
28
29 import com.google.common.base.Predicate;
30 import com.google.common.collect.Iterables;
31 import org.apache.maven.model.Dependency;
32 import org.apache.maven.model.Parent;
33 import org.apache.maven.model.Repository;
34 import org.apache.maven.model.building.FileModelSource;
35 import org.apache.maven.model.building.ModelSource;
36 import org.apache.maven.model.resolution.InvalidRepositoryException;
37 import org.apache.maven.model.resolution.ModelResolver;
38 import org.apache.maven.model.resolution.UnresolvableModelException;
39 import org.eclipse.aether.RepositorySystemSession;
40 import org.eclipse.aether.RequestTrace;
41 import org.eclipse.aether.artifact.Artifact;
42 import org.eclipse.aether.artifact.DefaultArtifact;
43 import org.eclipse.aether.impl.ArtifactResolver;
44 import org.eclipse.aether.impl.RemoteRepositoryManager;
45 import org.eclipse.aether.impl.VersionRangeResolver;
46 import org.eclipse.aether.repository.RemoteRepository;
47 import org.eclipse.aether.resolution.ArtifactRequest;
48 import org.eclipse.aether.resolution.ArtifactResolutionException;
49 import org.eclipse.aether.resolution.VersionRangeRequest;
50 import org.eclipse.aether.resolution.VersionRangeResolutionException;
51 import org.eclipse.aether.resolution.VersionRangeResult;
52
53
54
55
56
57
58
59
60 class DefaultModelResolver
61 implements ModelResolver
62 {
63
64 private final RepositorySystemSession session;
65
66 private final RequestTrace trace;
67
68 private final String context;
69
70 private List<RemoteRepository> repositories;
71
72 private final List<RemoteRepository> externalRepositories;
73
74 private final ArtifactResolver resolver;
75
76 private final VersionRangeResolver versionRangeResolver;
77
78 private final RemoteRepositoryManager remoteRepositoryManager;
79
80 private final Set<String> repositoryIds;
81
82 public DefaultModelResolver( RepositorySystemSession session, RequestTrace trace, String context,
83 ArtifactResolver resolver, VersionRangeResolver versionRangeResolver,
84 RemoteRepositoryManager remoteRepositoryManager, List<RemoteRepository> repositories )
85 {
86 this.session = session;
87 this.trace = trace;
88 this.context = context;
89 this.resolver = resolver;
90 this.versionRangeResolver = versionRangeResolver;
91 this.remoteRepositoryManager = remoteRepositoryManager;
92 this.repositories = repositories;
93 List<RemoteRepository> externalRepositories = new ArrayList<>();
94 externalRepositories.addAll( repositories );
95 this.externalRepositories = Collections.unmodifiableList( externalRepositories );
96
97 this.repositoryIds = new HashSet<>();
98 }
99
100 private DefaultModelResolver( DefaultModelResolver original )
101 {
102 this.session = original.session;
103 this.trace = original.trace;
104 this.context = original.context;
105 this.resolver = original.resolver;
106 this.versionRangeResolver = original.versionRangeResolver;
107 this.remoteRepositoryManager = original.remoteRepositoryManager;
108 this.repositories = new ArrayList<>( original.repositories );
109 this.externalRepositories = original.externalRepositories;
110 this.repositoryIds = new HashSet<>( original.repositoryIds );
111 }
112
113 @Override
114 public void addRepository( Repository repository )
115 throws InvalidRepositoryException
116 {
117 addRepository( repository, false );
118 }
119
120 @Override
121 public void addRepository( final Repository repository, boolean replace )
122 throws InvalidRepositoryException
123 {
124 if ( session.isIgnoreArtifactDescriptorRepositories() )
125 {
126 return;
127 }
128
129 if ( !repositoryIds.add( repository.getId() ) )
130 {
131 if ( !replace )
132 {
133 return;
134 }
135
136 removeMatchingRepository( repositories, repository.getId() );
137 }
138
139 List<RemoteRepository> newRepositories =
140 Collections.singletonList( ArtifactDescriptorUtils.toRemoteRepository( repository ) );
141
142 this.repositories =
143 remoteRepositoryManager.aggregateRepositories( session, repositories, newRepositories, true );
144 }
145
146 private static void removeMatchingRepository( Iterable<RemoteRepository> repositories, final String id )
147 {
148 Iterables.removeIf( repositories, new Predicate<RemoteRepository>()
149 {
150 @Override
151 public boolean apply( RemoteRepository remoteRepository )
152 {
153 return remoteRepository.getId().equals( id );
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 }