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