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.Repository;
30  import org.apache.maven.model.building.FileModelSource;
31  import org.apache.maven.model.building.ModelSource;
32  import org.apache.maven.model.resolution.InvalidRepositoryException;
33  import org.apache.maven.model.resolution.ModelResolver;
34  import org.apache.maven.model.resolution.UnresolvableModelException;
35  import org.apache.maven.repository.internal.ArtifactDescriptorUtils;
36  import org.eclipse.aether.RepositorySystem;
37  import org.eclipse.aether.RepositorySystemSession;
38  import org.eclipse.aether.RequestTrace;
39  import org.eclipse.aether.artifact.Artifact;
40  import org.eclipse.aether.artifact.DefaultArtifact;
41  import org.eclipse.aether.impl.RemoteRepositoryManager;
42  import org.eclipse.aether.repository.RemoteRepository;
43  import org.eclipse.aether.resolution.ArtifactRequest;
44  import org.eclipse.aether.resolution.ArtifactResolutionException;
45  
46  /**
47   * A model resolver to assist building of projects. This resolver gives priority to those repositories that have been
48   * declared in the POM.
49   * 
50   * @author Benjamin Bentmann
51   */
52  class ProjectModelResolver
53      implements ModelResolver
54  {
55  
56      private final RepositorySystemSession session;
57  
58      private final RequestTrace trace;
59  
60      private final String context = "project";
61  
62      private List<RemoteRepository> repositories;
63  
64      private List<RemoteRepository> pomRepositories;
65  
66      private final List<RemoteRepository> externalRepositories;
67  
68      private final RepositorySystem resolver;
69  
70      private final RemoteRepositoryManager remoteRepositoryManager;
71  
72      private final Set<String> repositoryIds;
73  
74      private final ReactorModelPool modelPool;
75  
76      private final ProjectBuildingRequest.RepositoryMerging repositoryMerging;
77  
78      public ProjectModelResolver( RepositorySystemSession session, RequestTrace trace, RepositorySystem resolver,
79                                   RemoteRepositoryManager remoteRepositoryManager, List<RemoteRepository> repositories,
80                                   ProjectBuildingRequest.RepositoryMerging repositoryMerging, ReactorModelPool modelPool )
81      {
82          this.session = session;
83          this.trace = trace;
84          this.resolver = resolver;
85          this.remoteRepositoryManager = remoteRepositoryManager;
86          this.pomRepositories = new ArrayList<RemoteRepository>();
87          this.externalRepositories = repositories;
88          this.repositories = repositories;
89          this.repositoryMerging = repositoryMerging;
90          this.repositoryIds = new HashSet<String>();
91          this.modelPool = modelPool;
92      }
93  
94      private ProjectModelResolver( ProjectModelResolver original )
95      {
96          this.session = original.session;
97          this.trace = original.trace;
98          this.resolver = original.resolver;
99          this.remoteRepositoryManager = original.remoteRepositoryManager;
100         this.pomRepositories = original.pomRepositories;
101         this.externalRepositories = original.externalRepositories;
102         this.repositories = original.repositories;
103         this.repositoryMerging = original.repositoryMerging;
104         this.repositoryIds = new HashSet<String>( original.repositoryIds );
105         this.modelPool = original.modelPool;
106     }
107 
108     public void addRepository( Repository repository )
109         throws InvalidRepositoryException
110     {
111         if ( !repositoryIds.add( repository.getId() ) )
112         {
113             return;
114         }
115 
116         List<RemoteRepository> newRepositories =
117             Collections.singletonList( ArtifactDescriptorUtils.toRemoteRepository( repository ) );
118 
119         if ( ProjectBuildingRequest.RepositoryMerging.REQUEST_DOMINANT.equals( repositoryMerging ) )
120         {
121             repositories = remoteRepositoryManager.aggregateRepositories( session, repositories, newRepositories, true );
122         }
123         else
124         {
125             pomRepositories =
126                 remoteRepositoryManager.aggregateRepositories( session, pomRepositories, newRepositories, true );
127             repositories =
128                 remoteRepositoryManager.aggregateRepositories( session, pomRepositories, externalRepositories, false );
129         }
130     }
131 
132     public ModelResolver newCopy()
133     {
134         return new ProjectModelResolver( this );
135     }
136 
137     public ModelSource resolveModel( String groupId, String artifactId, String version )
138         throws UnresolvableModelException
139     {
140         File pomFile = null;
141 
142         if ( modelPool != null )
143         {
144             pomFile = modelPool.get( groupId, artifactId, version );
145         }
146 
147         if ( pomFile == null )
148         {
149             Artifact pomArtifact = new DefaultArtifact( groupId, artifactId, "", "pom", version );
150 
151             try
152             {
153                 ArtifactRequest request = new ArtifactRequest( pomArtifact, repositories, context );
154                 request.setTrace( trace );
155                 pomArtifact = resolver.resolveArtifact( session, request ).getArtifact();
156             }
157             catch ( ArtifactResolutionException e )
158             {
159                 throw new UnresolvableModelException( e.getMessage(), groupId, artifactId, version, e );
160             }
161 
162             pomFile = pomArtifact.getFile();
163         }
164 
165         return new FileModelSource( pomFile );
166     }
167 
168 }