View Javadoc
1   package org.apache.maven.execution;
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.HashSet;
24  import java.util.List;
25  import java.util.Set;
26  
27  import org.apache.maven.artifact.InvalidRepositoryException;
28  import org.apache.maven.artifact.repository.ArtifactRepository;
29  import org.apache.maven.repository.RepositorySystem;
30  import org.apache.maven.settings.Mirror;
31  import org.apache.maven.settings.Proxy;
32  import org.apache.maven.settings.Server;
33  import org.apache.maven.settings.Settings;
34  import org.apache.maven.settings.SettingsUtils;
35  import org.codehaus.plexus.component.annotations.Component;
36  import org.codehaus.plexus.component.annotations.Requirement;
37  import org.codehaus.plexus.util.StringUtils;
38  
39  @Component( role = MavenExecutionRequestPopulator.class )
40  public class DefaultMavenExecutionRequestPopulator
41      implements MavenExecutionRequestPopulator
42  {
43  
44      @Requirement
45      private RepositorySystem repositorySystem;
46  
47      public MavenExecutionRequest populateFromSettings( MavenExecutionRequest request, Settings settings )
48          throws MavenExecutionRequestPopulationException
49      {
50          if ( settings == null )
51          {
52              return request;
53          }
54  
55          request.setOffline( settings.isOffline() );
56  
57          request.setInteractiveMode( settings.isInteractiveMode() );
58  
59          request.setPluginGroups( settings.getPluginGroups() );
60  
61          request.setLocalRepositoryPath( settings.getLocalRepository() );
62  
63          for ( Server server : settings.getServers() )
64          {
65              server = server.clone();
66  
67              request.addServer( server );
68          }
69  
70          //  <proxies>
71          //    <proxy>
72          //      <active>true</active>
73          //      <protocol>http</protocol>
74          //      <host>proxy.somewhere.com</host>
75          //      <port>8080</port>
76          //      <username>proxyuser</username>
77          //      <password>somepassword</password>
78          //      <nonProxyHosts>www.google.com|*.somewhere.com</nonProxyHosts>
79          //    </proxy>
80          //  </proxies>
81  
82          for ( Proxy proxy : settings.getProxies() )
83          {
84              if ( !proxy.isActive() )
85              {
86                  continue;
87              }
88  
89              proxy = proxy.clone();
90  
91              request.addProxy( proxy );
92          }
93  
94          // <mirrors>
95          //   <mirror>
96          //     <id>nexus</id>
97          //     <mirrorOf>*</mirrorOf>
98          //     <url>http://repository.sonatype.org/content/groups/public</url>
99          //   </mirror>
100         // </mirrors>
101 
102         for ( Mirror mirror : settings.getMirrors() )
103         {
104             mirror = mirror.clone();
105 
106             request.addMirror( mirror );
107         }
108 
109         request.setActiveProfiles( settings.getActiveProfiles() );
110 
111         for ( org.apache.maven.settings.Profile rawProfile : settings.getProfiles() )
112         {
113             request.addProfile( SettingsUtils.convertFromSettingsProfile( rawProfile ) );
114         }
115 
116         return request;
117     }
118 
119     private void populateDefaultPluginGroups( MavenExecutionRequest request )
120     {
121         request.addPluginGroup( "org.apache.maven.plugins" );
122         request.addPluginGroup( "org.codehaus.mojo" );
123     }
124 
125     private void injectDefaultRepositories( MavenExecutionRequest request )
126         throws MavenExecutionRequestPopulationException
127     {
128         Set<String> definedRepositories = getRepoIds( request.getRemoteRepositories() );
129 
130         if ( !definedRepositories.contains( RepositorySystem.DEFAULT_REMOTE_REPO_ID ) )
131         {
132             try
133             {
134                 request.addRemoteRepository( repositorySystem.createDefaultRemoteRepository() );
135             }
136             catch ( InvalidRepositoryException e )
137             {
138                 throw new MavenExecutionRequestPopulationException( "Cannot create default remote repository.", e );
139             }
140         }
141     }
142 
143     private void injectDefaultPluginRepositories( MavenExecutionRequest request )
144         throws MavenExecutionRequestPopulationException
145     {
146         Set<String> definedRepositories = getRepoIds( request.getPluginArtifactRepositories() );
147 
148         if ( !definedRepositories.contains( RepositorySystem.DEFAULT_REMOTE_REPO_ID ) )
149         {
150             try
151             {
152                 request.addPluginArtifactRepository( repositorySystem.createDefaultRemoteRepository() );
153             }
154             catch ( InvalidRepositoryException e )
155             {
156                 throw new MavenExecutionRequestPopulationException( "Cannot create default remote repository.", e );
157             }
158         }
159     }
160 
161     private Set<String> getRepoIds( List<ArtifactRepository> repositories )
162     {
163         Set<String> repoIds = new HashSet<String>();
164 
165         if ( repositories != null )
166         {
167             for ( ArtifactRepository repository : repositories )
168             {
169                 repoIds.add( repository.getId() );
170             }
171         }
172 
173         return repoIds;
174     }
175 
176     private void processRepositoriesInSettings( MavenExecutionRequest request )
177         throws MavenExecutionRequestPopulationException
178     {
179         repositorySystem.injectMirror( request.getRemoteRepositories(), request.getMirrors() );
180         repositorySystem.injectProxy( request.getRemoteRepositories(), request.getProxies() );
181         repositorySystem.injectAuthentication( request.getRemoteRepositories(), request.getServers() );
182 
183         request.setRemoteRepositories( repositorySystem.getEffectiveRepositories( request.getRemoteRepositories() ) );
184 
185         repositorySystem.injectMirror( request.getPluginArtifactRepositories(), request.getMirrors() );
186         repositorySystem.injectProxy( request.getPluginArtifactRepositories(), request.getProxies() );
187         repositorySystem.injectAuthentication( request.getPluginArtifactRepositories(), request.getServers() );
188 
189         request.setPluginArtifactRepositories( repositorySystem.getEffectiveRepositories( request.getPluginArtifactRepositories() ) );
190     }
191 
192     private void localRepository( MavenExecutionRequest request )
193         throws MavenExecutionRequestPopulationException
194     {
195         // ------------------------------------------------------------------------
196         // Local Repository
197         //
198         // 1. Use a value has been passed in via the configuration
199         // 2. Use value in the resultant settings
200         // 3. Use default value
201         // ------------------------------------------------------------------------
202 
203         if ( request.getLocalRepository() == null )
204         {
205             request.setLocalRepository( createLocalRepository( request ) );
206         }
207 
208         if ( request.getLocalRepositoryPath() == null )
209         {
210             request.setLocalRepositoryPath( new File( request.getLocalRepository().getBasedir() ).getAbsoluteFile() );
211         }
212     }
213 
214     // ------------------------------------------------------------------------
215     // Artifact Transfer Mechanism
216     // ------------------------------------------------------------------------
217 
218     public ArtifactRepository createLocalRepository( MavenExecutionRequest request )
219         throws MavenExecutionRequestPopulationException
220     {
221         String localRepositoryPath = null;
222 
223         if ( request.getLocalRepositoryPath() != null )
224         {
225             localRepositoryPath = request.getLocalRepositoryPath().getAbsolutePath();
226         }
227 
228         if ( StringUtils.isEmpty( localRepositoryPath ) )
229         {
230             localRepositoryPath = RepositorySystem.defaultUserLocalRepository.getAbsolutePath();
231         }
232 
233         try
234         {
235             return repositorySystem.createLocalRepository( new File( localRepositoryPath ) );
236         }
237         catch ( InvalidRepositoryException e )
238         {
239             throw new MavenExecutionRequestPopulationException( "Cannot create local repository.", e );
240         }
241     }
242 
243     private void baseDirectory( MavenExecutionRequest request )
244     {
245         if ( request.getBaseDirectory() == null && request.getPom() != null )
246         {
247             request.setBaseDirectory( request.getPom().getAbsoluteFile().getParentFile() );
248         }
249     }
250 
251     public MavenExecutionRequest populateDefaults( MavenExecutionRequest request )
252         throws MavenExecutionRequestPopulationException
253     {
254         baseDirectory( request );
255 
256         localRepository( request );
257 
258         populateDefaultPluginGroups( request );
259 
260         injectDefaultRepositories( request );
261 
262         injectDefaultPluginRepositories( request );
263 
264         processRepositoriesInSettings( request );
265 
266         return request;
267     }
268 
269 }