View Javadoc
1   package org.apache.maven.project;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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.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   * A model resolver to assist building of projects. This resolver gives priority to those repositories that have been
55   * declared in the POM.
56   *
57   * @author Benjamin Bentmann
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<>();
95          this.externalRepositories = Collections.unmodifiableList( new ArrayList<>( repositories ) );
96          this.repositories = new ArrayList<>();
97          this.repositories.addAll( externalRepositories );
98          this.repositoryMerging = repositoryMerging;
99          this.repositoryIds = new HashSet<>();
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<>( original.pomRepositories );
110         this.externalRepositories = original.externalRepositories;
111         this.repositories = new ArrayList<>( original.repositories );
112         this.repositoryMerging = original.repositoryMerging;
113         this.repositoryIds = new HashSet<>( 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             // Remove any previous repository with this Id
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         Iterator iterator = repositories.iterator( );
159         while ( iterator.hasNext() )
160         {
161             RemoteRepository next =  ( RemoteRepository ) iterator.next();
162             if ( next.getId().equals( id ) )
163             {
164                 iterator.remove();
165             }
166         }
167     }
168 
169     public ModelResolver newCopy()
170     {
171         return new ProjectModelResolver( this );
172     }
173 
174     public ModelSource resolveModel( String groupId, String artifactId, String version )
175         throws UnresolvableModelException
176     {
177         File pomFile = null;
178 
179         if ( modelPool != null )
180         {
181             pomFile = modelPool.get( groupId, artifactId, version );
182         }
183 
184         if ( pomFile == null )
185         {
186             Artifact pomArtifact = new DefaultArtifact( groupId, artifactId, "", "pom", version );
187 
188             try
189             {
190                 ArtifactRequest request = new ArtifactRequest( pomArtifact, repositories, context );
191                 request.setTrace( trace );
192                 pomArtifact = resolver.resolveArtifact( session, request ).getArtifact();
193             }
194             catch ( ArtifactResolutionException e )
195             {
196                 throw new UnresolvableModelException( e.getMessage(), groupId, artifactId, version, e );
197             }
198 
199             pomFile = pomArtifact.getFile();
200         }
201 
202         return new FileModelSource( pomFile );
203     }
204 
205     @Override
206     public ModelSource resolveModel( final Parent parent )
207         throws UnresolvableModelException
208     {
209         try
210         {
211             final Artifact artifact = new DefaultArtifact( parent.getGroupId(), parent.getArtifactId(), "", "pom",
212                                                            parent.getVersion() );
213 
214             final VersionRangeRequest versionRangeRequest = new VersionRangeRequest( artifact, repositories, context );
215             versionRangeRequest.setTrace( trace );
216 
217             final VersionRangeResult versionRangeResult = resolver.resolveVersionRange( session, versionRangeRequest );
218 
219             if ( versionRangeResult.getHighestVersion() == null )
220             {
221                 throw new UnresolvableModelException(
222                     String.format( "No versions matched the requested parent version range '%s'",
223                                    parent.getVersion() ),
224                     parent.getGroupId(), parent.getArtifactId(), parent.getVersion() );
225 
226             }
227 
228             if ( versionRangeResult.getVersionConstraint() != null
229                      && versionRangeResult.getVersionConstraint().getRange() != null
230                      && versionRangeResult.getVersionConstraint().getRange().getUpperBound() == null )
231             {
232                 // Message below is checked for in the MNG-2199 core IT.
233                 throw new UnresolvableModelException(
234                     String.format( "The requested parent version range '%s' does not specify an upper bound",
235                                    parent.getVersion() ),
236                     parent.getGroupId(), parent.getArtifactId(), parent.getVersion() );
237 
238             }
239 
240             parent.setVersion( versionRangeResult.getHighestVersion().toString() );
241 
242             return resolveModel( parent.getGroupId(), parent.getArtifactId(), parent.getVersion() );
243         }
244         catch ( final VersionRangeResolutionException e )
245         {
246             throw new UnresolvableModelException( e.getMessage(), parent.getGroupId(), parent.getArtifactId(),
247                                                   parent.getVersion(), e );
248 
249         }
250     }
251 
252     @Override
253     public ModelSource resolveModel( final Dependency dependency )
254         throws UnresolvableModelException
255     {
256         try
257         {
258             final Artifact artifact = new DefaultArtifact( dependency.getGroupId(), dependency.getArtifactId(), "",
259                                                            "pom", dependency.getVersion() );
260 
261             final VersionRangeRequest versionRangeRequest = new VersionRangeRequest( artifact, repositories, context );
262             versionRangeRequest.setTrace( trace );
263 
264             final VersionRangeResult versionRangeResult = resolver.resolveVersionRange( session, versionRangeRequest );
265 
266             if ( versionRangeResult.getHighestVersion() == null )
267             {
268                 throw new UnresolvableModelException(
269                     String.format( "No versions matched the requested dependency version range '%s'",
270                                    dependency.getVersion() ),
271                     dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion() );
272 
273             }
274 
275             if ( versionRangeResult.getVersionConstraint() != null
276                      && versionRangeResult.getVersionConstraint().getRange() != null
277                      && versionRangeResult.getVersionConstraint().getRange().getUpperBound() == null )
278             {
279                 // Message below is checked for in the MNG-4463 core IT.
280                 throw new UnresolvableModelException(
281                     String.format( "The requested dependency version range '%s' does not specify an upper bound",
282                                    dependency.getVersion() ),
283                     dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion() );
284 
285             }
286 
287             dependency.setVersion( versionRangeResult.getHighestVersion().toString() );
288 
289             return resolveModel( dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion() );
290         }
291         catch ( VersionRangeResolutionException e )
292         {
293             throw new UnresolvableModelException( e.getMessage(), dependency.getGroupId(), dependency.getArtifactId(),
294                                                   dependency.getVersion(), e );
295 
296         }
297     }
298 }