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   * A model resolver to assist building of projects. This resolver gives priority to those repositories that have been
54   * declared in the POM.
55   *
56   * @author Benjamin Bentmann
57   */
58  public 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,
87                                   ReactorModelPool modelPool )
88      {
89          this.session = session;
90          this.trace = trace;
91          this.resolver = resolver;
92          this.remoteRepositoryManager = remoteRepositoryManager;
93          this.pomRepositories = new ArrayList<>();
94          List<RemoteRepository> externalRepositories = new ArrayList<>();
95          externalRepositories.addAll( repositories );
96          this.externalRepositories = Collections.unmodifiableList( externalRepositories );
97          this.repositories = new ArrayList<>();
98          this.repositories.addAll( externalRepositories );
99          this.repositoryMerging = repositoryMerging;
100         this.repositoryIds = new HashSet<>();
101         this.modelPool = modelPool;
102     }
103 
104     private ProjectModelResolver( ProjectModelResolver original )
105     {
106         this.session = original.session;
107         this.trace = original.trace;
108         this.resolver = original.resolver;
109         this.remoteRepositoryManager = original.remoteRepositoryManager;
110         this.pomRepositories = new ArrayList<>( original.pomRepositories );
111         this.externalRepositories = original.externalRepositories;
112         this.repositories = new ArrayList<>( original.repositories );
113         this.repositoryMerging = original.repositoryMerging;
114         this.repositoryIds = new HashSet<>( original.repositoryIds );
115         this.modelPool = original.modelPool;
116     }
117 
118     public void addRepository( Repository repository )
119         throws InvalidRepositoryException
120     {
121          addRepository( repository, false );
122     }
123 
124     @Override
125     public void addRepository( final Repository repository, boolean replace )
126         throws InvalidRepositoryException
127     {
128         if ( !repositoryIds.add( repository.getId() ) )
129         {
130             if ( !replace )
131             {
132                 return;
133             }
134 
135             // Remove any previous repository with this Id
136             removeMatchingRepository( repositories, repository.getId() );
137             removeMatchingRepository( pomRepositories, repository.getId() );
138         }
139 
140         List<RemoteRepository> newRepositories =
141             Collections.singletonList( ArtifactDescriptorUtils.toRemoteRepository( repository ) );
142 
143         if ( ProjectBuildingRequest.RepositoryMerging.REQUEST_DOMINANT.equals( repositoryMerging ) )
144         {
145             repositories = remoteRepositoryManager.aggregateRepositories( session, repositories, newRepositories,
146                                                                           true );
147         }
148         else
149         {
150             pomRepositories =
151                 remoteRepositoryManager.aggregateRepositories( session, pomRepositories, newRepositories, true );
152             repositories =
153                 remoteRepositoryManager.aggregateRepositories( session, pomRepositories, externalRepositories, false );
154         }
155     }
156 
157     private static void removeMatchingRepository( Iterable<RemoteRepository> repositories, final String id )
158     {
159         Iterator iterator = repositories.iterator( );
160         while ( iterator.hasNext() )
161         {
162             RemoteRepository next =  ( RemoteRepository ) iterator.next();
163             if ( next.getId().equals( id ) )
164             {
165                 iterator.remove();
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     @Override
207     public ModelSource resolveModel( final Parent parent )
208         throws UnresolvableModelException
209     {
210         try
211         {
212             final Artifact artifact = new DefaultArtifact( parent.getGroupId(), parent.getArtifactId(), "", "pom",
213                                                            parent.getVersion() );
214 
215             final VersionRangeRequest versionRangeRequest = new VersionRangeRequest( artifact, repositories, context );
216             versionRangeRequest.setTrace( trace );
217 
218             final VersionRangeResult versionRangeResult = resolver.resolveVersionRange( session, versionRangeRequest );
219 
220             if ( versionRangeResult.getHighestVersion() == null )
221             {
222                 throw new UnresolvableModelException(
223                     String.format( "No versions matched the requested parent version range '%s'",
224                                    parent.getVersion() ),
225                     parent.getGroupId(), parent.getArtifactId(), parent.getVersion() );
226 
227             }
228 
229             if ( versionRangeResult.getVersionConstraint() != null
230                      && versionRangeResult.getVersionConstraint().getRange() != null
231                      && versionRangeResult.getVersionConstraint().getRange().getUpperBound() == null )
232             {
233                 // Message below is checked for in the MNG-2199 core IT.
234                 throw new UnresolvableModelException(
235                     String.format( "The requested parent version range '%s' does not specify an upper bound",
236                                    parent.getVersion() ),
237                     parent.getGroupId(), parent.getArtifactId(), parent.getVersion() );
238 
239             }
240 
241             parent.setVersion( versionRangeResult.getHighestVersion().toString() );
242 
243             return resolveModel( parent.getGroupId(), parent.getArtifactId(), parent.getVersion() );
244         }
245         catch ( final VersionRangeResolutionException e )
246         {
247             throw new UnresolvableModelException( e.getMessage(), parent.getGroupId(), parent.getArtifactId(),
248                                                   parent.getVersion(), e );
249 
250         }
251     }
252 
253     @Override
254     public ModelSource resolveModel( final Dependency dependency )
255         throws UnresolvableModelException
256     {
257         try
258         {
259             final Artifact artifact = new DefaultArtifact( dependency.getGroupId(), dependency.getArtifactId(), "",
260                                                            "pom", dependency.getVersion() );
261 
262             final VersionRangeRequest versionRangeRequest = new VersionRangeRequest( artifact, repositories, context );
263             versionRangeRequest.setTrace( trace );
264 
265             final VersionRangeResult versionRangeResult = resolver.resolveVersionRange( session, versionRangeRequest );
266 
267             if ( versionRangeResult.getHighestVersion() == null )
268             {
269                 throw new UnresolvableModelException(
270                     String.format( "No versions matched the requested dependency version range '%s'",
271                                    dependency.getVersion() ),
272                     dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion() );
273 
274             }
275 
276             if ( versionRangeResult.getVersionConstraint() != null
277                      && versionRangeResult.getVersionConstraint().getRange() != null
278                      && versionRangeResult.getVersionConstraint().getRange().getUpperBound() == null )
279             {
280                 // Message below is checked for in the MNG-4463 core IT.
281                 throw new UnresolvableModelException(
282                     String.format( "The requested dependency version range '%s' does not specify an upper bound",
283                                    dependency.getVersion() ),
284                     dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion() );
285 
286             }
287 
288             dependency.setVersion( versionRangeResult.getHighestVersion().toString() );
289 
290             return resolveModel( dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion() );
291         }
292         catch ( VersionRangeResolutionException e )
293         {
294             throw new UnresolvableModelException( e.getMessage(), dependency.getGroupId(), dependency.getArtifactId(),
295                                                   dependency.getVersion(), e );
296 
297         }
298     }
299 }