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.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.Dependency;
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   * 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          List<RemoteRepository> externalRepositories = new ArrayList<>();
96          externalRepositories.addAll( repositories );
97          this.externalRepositories = Collections.unmodifiableList( externalRepositories );
98          this.repositories = new ArrayList<>();
99          this.repositories.addAll( externalRepositories );
100         this.repositoryMerging = repositoryMerging;
101         this.repositoryIds = new HashSet<>();
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<>( original.pomRepositories );
112         this.externalRepositories = original.externalRepositories;
113         this.repositories = new ArrayList<>( original.repositories );
114         this.repositoryMerging = original.repositoryMerging;
115         this.repositoryIds = new HashSet<>( 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             // Remove any previous repository with this Id
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     @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 }