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.Collections;
24  import java.util.HashSet;
25  import java.util.List;
26  import java.util.Set;
27  
28  import org.apache.maven.model.Repository;
29  import org.apache.maven.model.building.FileModelSource;
30  import org.apache.maven.model.building.ModelSource;
31  import org.apache.maven.model.resolution.InvalidRepositoryException;
32  import org.apache.maven.model.resolution.ModelResolver;
33  import org.apache.maven.model.resolution.UnresolvableModelException;
34  import org.sonatype.aether.RepositorySystemSession;
35  import org.sonatype.aether.artifact.Artifact;
36  import org.sonatype.aether.impl.ArtifactResolver;
37  import org.sonatype.aether.impl.RemoteRepositoryManager;
38  import org.sonatype.aether.repository.RemoteRepository;
39  import org.sonatype.aether.resolution.ArtifactRequest;
40  import org.sonatype.aether.resolution.ArtifactResolutionException;
41  import org.sonatype.aether.util.artifact.DefaultArtifact;
42  
43  /**
44   * A model resolver to assist building of dependency POMs. This resolver gives priority to those repositories that have
45   * been initially specified and repositories discovered in dependency POMs are recessively merged into the search chain.
46   * 
47   * @author Benjamin Bentmann
48   * @see DefaultArtifactDescriptorReader
49   */
50  class DefaultModelResolver
51      implements ModelResolver
52  {
53  
54      private final RepositorySystemSession session;
55  
56      private final String context;
57  
58      private List<RemoteRepository> repositories;
59  
60      private final ArtifactResolver resolver;
61  
62      private final RemoteRepositoryManager remoteRepositoryManager;
63  
64      private final Set<String> repositoryIds;
65  
66      public DefaultModelResolver( RepositorySystemSession session, String context, ArtifactResolver resolver,
67                                   RemoteRepositoryManager remoteRepositoryManager, List<RemoteRepository> repositories )
68      {
69          this.session = session;
70          this.context = context;
71          this.resolver = resolver;
72          this.remoteRepositoryManager = remoteRepositoryManager;
73          this.repositories = repositories;
74          this.repositoryIds = new HashSet<String>();
75      }
76  
77      private DefaultModelResolver( DefaultModelResolver original )
78      {
79          this.session = original.session;
80          this.context = original.context;
81          this.resolver = original.resolver;
82          this.remoteRepositoryManager = original.remoteRepositoryManager;
83          this.repositories = original.repositories;
84          this.repositoryIds = new HashSet<String>( original.repositoryIds );
85      }
86  
87      public void addRepository( Repository repository )
88          throws InvalidRepositoryException
89      {
90          if ( !repositoryIds.add( repository.getId() ) )
91          {
92              return;
93          }
94  
95          List<RemoteRepository> newRepositories =
96              Collections.singletonList( DefaultArtifactDescriptorReader.convert( repository ) );
97  
98          this.repositories =
99              remoteRepositoryManager.aggregateRepositories( session, repositories, newRepositories, true );
100     }
101 
102     public ModelResolver newCopy()
103     {
104         return new DefaultModelResolver( this );
105     }
106 
107     public ModelSource resolveModel( String groupId, String artifactId, String version )
108         throws UnresolvableModelException
109     {
110         Artifact pomArtifact = new DefaultArtifact( groupId, artifactId, "", "pom", version );
111 
112         try
113         {
114             ArtifactRequest request = new ArtifactRequest( pomArtifact, repositories, context );
115             pomArtifact = resolver.resolveArtifact( session, request ).getArtifact();
116         }
117         catch ( ArtifactResolutionException e )
118         {
119             throw new UnresolvableModelException( e.getMessage(), groupId, artifactId, version, e );
120         }
121 
122         File pomFile = pomArtifact.getFile();
123 
124         return new FileModelSource( pomFile );
125     }
126 
127 }