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