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.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 public 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<RemoteRepository>();
93 externalRepositories.addAll( repositories );
94 this.externalRepositories = Collections.unmodifiableList( externalRepositories );
95
96 this.repositoryIds = new HashSet<String>();
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<RemoteRepository>(original.repositories);
108 this.externalRepositories = original.externalRepositories;
109 this.repositoryIds = new HashSet<String>( 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) throws InvalidRepositoryException {
121 if ( session.isIgnoreArtifactDescriptorRepositories() )
122 {
123 return;
124 }
125
126 if ( !repositoryIds.add( repository.getId() ) ) {
127 if ( !replace ) {
128 return;
129 }
130
131 removeMatchingRepository( repositories, repository.getId() );
132 }
133
134 List<RemoteRepository> newRepositories =
135 Collections.singletonList( ArtifactDescriptorUtils.toRemoteRepository( repository ) );
136
137 this.repositories =
138 remoteRepositoryManager.aggregateRepositories( session, repositories, newRepositories, true );
139 }
140
141 private static void removeMatchingRepository(Iterable<RemoteRepository> repositories, final String id) {
142 Iterables.removeIf(repositories, new Predicate<RemoteRepository>() {
143 @Override
144 public boolean apply(RemoteRepository remoteRepository) {
145 return remoteRepository.getId().equals(id);
146 }
147 });
148 }
149
150 @Override
151 public ModelResolver newCopy()
152 {
153 return new DefaultModelResolver( this );
154 }
155
156 @Override
157 public ModelSource resolveModel( String groupId, String artifactId, String version )
158 throws UnresolvableModelException
159 {
160 Artifact pomArtifact = new DefaultArtifact( groupId, artifactId, "", "pom", version );
161
162 try
163 {
164 ArtifactRequest request = new ArtifactRequest( pomArtifact, repositories, context );
165 request.setTrace( trace );
166 pomArtifact = resolver.resolveArtifact( session, request ).getArtifact();
167 }
168 catch ( ArtifactResolutionException e )
169 {
170 throw new UnresolvableModelException( e.getMessage(), groupId, artifactId, version, e );
171 }
172
173 File pomFile = pomArtifact.getFile();
174
175 return new FileModelSource( pomFile );
176 }
177
178 public ModelSource resolveModel( Parent parent )
179 throws UnresolvableModelException
180 {
181 Artifact artifact = new DefaultArtifact( parent.getGroupId(), parent.getArtifactId(), "", "pom",
182 parent.getVersion() );
183
184 VersionRangeRequest versionRangeRequest = new VersionRangeRequest( artifact, repositories, context );
185 versionRangeRequest.setTrace( trace );
186
187 try
188 {
189 VersionRangeResult versionRangeResult =
190 versionRangeResolver.resolveVersionRange( session, versionRangeRequest );
191
192 if ( versionRangeResult.getHighestVersion() == null )
193 {
194 throw new UnresolvableModelException( "No versions matched the requested range '" + parent.getVersion()
195 + "'", parent.getGroupId(), parent.getArtifactId(),
196 parent.getVersion() );
197
198 }
199
200 if ( versionRangeResult.getVersionConstraint() != null
201 && versionRangeResult.getVersionConstraint().getRange() != null
202 && versionRangeResult.getVersionConstraint().getRange().getUpperBound() == null )
203 {
204 throw new UnresolvableModelException( "The requested version range '" + parent.getVersion()
205 + "' does not specify an upper bound", parent.getGroupId(),
206 parent.getArtifactId(), parent.getVersion() );
207
208 }
209
210 parent.setVersion( versionRangeResult.getHighestVersion().toString() );
211 }
212 catch ( VersionRangeResolutionException e )
213 {
214 throw new UnresolvableModelException( e.getMessage(), parent.getGroupId(), parent.getArtifactId(),
215 parent.getVersion(), e );
216
217 }
218
219 return resolveModel( parent.getGroupId(), parent.getArtifactId(), parent.getVersion() );
220 }
221
222 }