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