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