1 package org.apache.maven.project;
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
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.apache.maven.repository.internal.ArtifactDescriptorUtils;
40 import org.eclipse.aether.RepositorySystem;
41 import org.eclipse.aether.RepositorySystemSession;
42 import org.eclipse.aether.RequestTrace;
43 import org.eclipse.aether.artifact.Artifact;
44 import org.eclipse.aether.artifact.DefaultArtifact;
45 import org.eclipse.aether.impl.RemoteRepositoryManager;
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 public class ProjectModelResolver
60 implements ModelResolver
61 {
62
63 private final RepositorySystemSession session;
64
65 private final RequestTrace trace;
66
67 private final String context = "project";
68
69 private List<RemoteRepository> repositories;
70
71 private List<RemoteRepository> pomRepositories;
72
73 private final List<RemoteRepository> externalRepositories;
74
75 private final RepositorySystem resolver;
76
77 private final RemoteRepositoryManager remoteRepositoryManager;
78
79 private final Set<String> repositoryIds;
80
81 private final ReactorModelPool modelPool;
82
83 private final ProjectBuildingRequest.RepositoryMerging repositoryMerging;
84
85 public ProjectModelResolver( RepositorySystemSession session, RequestTrace trace, RepositorySystem resolver,
86 RemoteRepositoryManager remoteRepositoryManager, List<RemoteRepository> repositories,
87 ProjectBuildingRequest.RepositoryMerging repositoryMerging,
88 ReactorModelPool modelPool )
89 {
90 this.session = session;
91 this.trace = trace;
92 this.resolver = resolver;
93 this.remoteRepositoryManager = remoteRepositoryManager;
94 this.pomRepositories = new ArrayList<RemoteRepository>();
95 List<RemoteRepository> externalRepositories = new ArrayList<RemoteRepository>();
96 externalRepositories.addAll( repositories );
97 this.externalRepositories = Collections.unmodifiableList( externalRepositories );
98 this.repositories = new ArrayList<RemoteRepository>();
99 this.repositories.addAll( externalRepositories );
100 this.repositoryMerging = repositoryMerging;
101 this.repositoryIds = new HashSet<String>();
102 this.modelPool = modelPool;
103 }
104
105 private ProjectModelResolver( ProjectModelResolver original )
106 {
107 this.session = original.session;
108 this.trace = original.trace;
109 this.resolver = original.resolver;
110 this.remoteRepositoryManager = original.remoteRepositoryManager;
111 this.pomRepositories = new ArrayList<RemoteRepository>( original.pomRepositories );
112 this.externalRepositories = original.externalRepositories;
113 this.repositories = new ArrayList<RemoteRepository>( original.repositories );
114 this.repositoryMerging = original.repositoryMerging;
115 this.repositoryIds = new HashSet<String>( original.repositoryIds );
116 this.modelPool = original.modelPool;
117 }
118
119 public void addRepository( Repository repository )
120 throws InvalidRepositoryException
121 {
122 addRepository( repository, false );
123 }
124
125 @Override
126 public void addRepository( final Repository repository, boolean replace )
127 throws InvalidRepositoryException
128 {
129 if ( !repositoryIds.add( repository.getId() ) )
130 {
131 if ( !replace )
132 {
133 return;
134 }
135
136
137 removeMatchingRepository( repositories, repository.getId() );
138 removeMatchingRepository( pomRepositories, repository.getId() );
139 }
140
141 List<RemoteRepository> newRepositories =
142 Collections.singletonList( ArtifactDescriptorUtils.toRemoteRepository( repository ) );
143
144 if ( ProjectBuildingRequest.RepositoryMerging.REQUEST_DOMINANT.equals( repositoryMerging ) )
145 {
146 repositories = remoteRepositoryManager.aggregateRepositories( session, repositories, newRepositories,
147 true );
148 }
149 else
150 {
151 pomRepositories =
152 remoteRepositoryManager.aggregateRepositories( session, pomRepositories, newRepositories, true );
153 repositories =
154 remoteRepositoryManager.aggregateRepositories( session, pomRepositories, externalRepositories, false );
155 }
156 }
157
158 private static void removeMatchingRepository( Iterable<RemoteRepository> repositories, final String id )
159 {
160 Iterables.removeIf( repositories, new Predicate<RemoteRepository>()
161 {
162 @Override
163 public boolean apply( RemoteRepository remoteRepository )
164 {
165 return remoteRepository.getId().equals( id );
166 }
167 } );
168 }
169
170 public ModelResolver newCopy()
171 {
172 return new ProjectModelResolver( this );
173 }
174
175 public ModelSource resolveModel( String groupId, String artifactId, String version )
176 throws UnresolvableModelException
177 {
178 File pomFile = null;
179
180 if ( modelPool != null )
181 {
182 pomFile = modelPool.get( groupId, artifactId, version );
183 }
184
185 if ( pomFile == null )
186 {
187 Artifact pomArtifact = new DefaultArtifact( groupId, artifactId, "", "pom", version );
188
189 try
190 {
191 ArtifactRequest request = new ArtifactRequest( pomArtifact, repositories, context );
192 request.setTrace( trace );
193 pomArtifact = resolver.resolveArtifact( session, request ).getArtifact();
194 }
195 catch ( ArtifactResolutionException e )
196 {
197 throw new UnresolvableModelException( e.getMessage(), groupId, artifactId, version, e );
198 }
199
200 pomFile = pomArtifact.getFile();
201 }
202
203 return new FileModelSource( pomFile );
204 }
205
206 public ModelSource resolveModel( Parent parent )
207 throws UnresolvableModelException
208 {
209 Artifact artifact = new DefaultArtifact( parent.getGroupId(), parent.getArtifactId(), "", "pom",
210 parent.getVersion() );
211
212 VersionRangeRequest versionRangeRequest = new VersionRangeRequest( artifact, repositories, context );
213 versionRangeRequest.setTrace( trace );
214
215 try
216 {
217 VersionRangeResult versionRangeResult = resolver.resolveVersionRange( session, versionRangeRequest );
218
219 if ( versionRangeResult.getHighestVersion() == null )
220 {
221 throw new UnresolvableModelException( "No versions matched the requested range '" + parent.getVersion()
222 + "'", parent.getGroupId(), parent.getArtifactId(),
223 parent.getVersion() );
224
225 }
226
227 if ( versionRangeResult.getVersionConstraint() != null
228 && versionRangeResult.getVersionConstraint().getRange() != null
229 && versionRangeResult.getVersionConstraint().getRange().getUpperBound() == null )
230 {
231 throw new UnresolvableModelException( "The requested version range '" + parent.getVersion()
232 + "' does not specify an upper bound", parent.getGroupId(),
233 parent.getArtifactId(), parent.getVersion() );
234
235 }
236
237 parent.setVersion( versionRangeResult.getHighestVersion().toString() );
238 }
239 catch ( VersionRangeResolutionException e )
240 {
241 throw new UnresolvableModelException( e.getMessage(), parent.getGroupId(), parent.getArtifactId(),
242 parent.getVersion(), e );
243
244 }
245
246 return resolveModel( parent.getGroupId(), parent.getArtifactId(), parent.getVersion() );
247 }
248 }