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