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.ArrayList;
24  import java.util.HashMap;
25  import java.util.List;
26  import java.util.Map;
27  import java.util.Set;
28  
29  import javax.inject.Inject;
30  import javax.inject.Named;
31  
32  import org.apache.maven.artifact.InvalidRepositoryException;
33  import org.apache.maven.artifact.repository.ArtifactRepository;
34  import org.apache.maven.bridge.MavenRepositorySystem;
35  import org.apache.maven.repository.RepositorySystem;
36  //
37  // All of this needs to go away and be couched in terms of the execution request
38  //
39  import org.apache.maven.settings.Mirror;
40  import org.apache.maven.settings.Proxy;
41  import org.apache.maven.settings.Repository;
42  import org.apache.maven.settings.Server;
43  import org.apache.maven.settings.Settings;
44  import org.apache.maven.settings.SettingsUtils;
45  //
46  // Settings in core
47  //
48  import org.apache.maven.toolchain.model.PersistedToolchains;
49  import org.apache.maven.toolchain.model.ToolchainModel;
50  import org.codehaus.plexus.util.StringUtils;
51  
52  @Named
53  public class DefaultMavenExecutionRequestPopulator
54      implements MavenExecutionRequestPopulator
55  {
56              
57      private final MavenRepositorySystem repositorySystem;
58      
59      @Inject
60      public DefaultMavenExecutionRequestPopulator( MavenRepositorySystem repositorySystem )
61      {
62          this.repositorySystem = repositorySystem;
63      }
64  
65      @Override
66      public MavenExecutionRequest populateFromSettings( MavenExecutionRequest request, Settings settings )
67          throws MavenExecutionRequestPopulationException
68      {
69          if ( settings == null )
70          {
71              return request;
72          }
73  
74          request.setOffline( settings.isOffline() );
75  
76          request.setInteractiveMode( settings.isInteractiveMode() );
77  
78          request.setPluginGroups( settings.getPluginGroups() );
79  
80          request.setLocalRepositoryPath( settings.getLocalRepository() );
81  
82          for ( Server server : settings.getServers() )
83          {
84              server = server.clone();
85  
86              request.addServer( server );
87          }
88  
89          //  <proxies>
90          //    <proxy>
91          //      <active>true</active>
92          //      <protocol>http</protocol>
93          //      <host>proxy.somewhere.com</host>
94          //      <port>8080</port>
95          //      <username>proxyuser</username>
96          //      <password>somepassword</password>
97          //      <nonProxyHosts>www.google.com|*.somewhere.com</nonProxyHosts>
98          //    </proxy>
99          //  </proxies>
100 
101         for ( Proxy proxy : settings.getProxies() )
102         {
103             if ( !proxy.isActive() )
104             {
105                 continue;
106             }
107 
108             proxy = proxy.clone();
109 
110             request.addProxy( proxy );
111         }
112 
113         // <mirrors>
114         //   <mirror>
115         //     <id>nexus</id>
116         //     <mirrorOf>*</mirrorOf>
117         //     <url>http://repository.sonatype.org/content/groups/public</url>
118         //   </mirror>
119         // </mirrors>
120 
121         for ( Mirror mirror : settings.getMirrors() )
122         {
123             mirror = mirror.clone();
124 
125             request.addMirror( mirror );
126         }
127 
128         request.setActiveProfiles( settings.getActiveProfiles() );
129 
130         for ( org.apache.maven.settings.Profile rawProfile : settings.getProfiles() )
131         {
132             request.addProfile( SettingsUtils.convertFromSettingsProfile( rawProfile ) );
133 
134             if ( settings.getActiveProfiles().contains( rawProfile.getId() ) )
135             {
136                 List<Repository> remoteRepositories = rawProfile.getRepositories();
137                 for ( Repository remoteRepository : remoteRepositories )
138                 {
139                     try
140                     {
141                         request.addRemoteRepository( repositorySystem.buildArtifactRepository( remoteRepository ) );
142                     }
143                     catch ( InvalidRepositoryException e )
144                     {
145                         // do nothing for now
146                     }
147                 }
148 
149                 List<Repository> pluginRepositories = rawProfile.getPluginRepositories();
150                 for ( Repository pluginRepo : pluginRepositories )
151                 {
152                     try
153                     {
154                         request.addPluginArtifactRepository( repositorySystem.buildArtifactRepository( pluginRepo ) );
155                     }
156                     catch ( InvalidRepositoryException e )
157                     {
158                         // do nothing for now
159                     }
160                 }
161             }
162         }
163 
164         return request;
165     }
166 
167     @Override
168     public MavenExecutionRequest populateFromToolchains( MavenExecutionRequest request, PersistedToolchains toolchains )
169         throws MavenExecutionRequestPopulationException
170     {
171         if ( toolchains != null )
172         {
173             Map<String, List<ToolchainModel>> groupedToolchains = new HashMap<String, List<ToolchainModel>>( 2 );
174 
175             for ( ToolchainModel model : toolchains.getToolchains() )
176             {
177                 if ( !groupedToolchains.containsKey( model.getType() ) )
178                 {
179                     groupedToolchains.put( model.getType(), new ArrayList<ToolchainModel>() );
180                 }
181 
182                 groupedToolchains.get( model.getType() ).add( model );
183             }
184 
185             request.setToolchains( groupedToolchains );
186         }
187         return request;
188     }
189     
190     @Override
191     public MavenExecutionRequest populateDefaults( MavenExecutionRequest request )
192         throws MavenExecutionRequestPopulationException
193     {
194         baseDirectory( request );
195 
196         localRepository( request );
197 
198         populateDefaultPluginGroups( request );
199 
200         injectDefaultRepositories( request );
201 
202         injectDefaultPluginRepositories( request );
203 
204         processRepositoriesInSettings( request );
205 
206         return request;
207     }
208     
209     //
210     //
211     //
212     
213     private void populateDefaultPluginGroups( MavenExecutionRequest request )
214     {
215         request.addPluginGroup( "org.apache.maven.plugins" );
216         request.addPluginGroup( "org.codehaus.mojo" );
217     }
218 
219     private void injectDefaultRepositories( MavenExecutionRequest request )
220         throws MavenExecutionRequestPopulationException
221     {
222         Set<String> definedRepositories = repositorySystem.getRepoIds( request.getRemoteRepositories() );
223 
224         if ( !definedRepositories.contains( RepositorySystem.DEFAULT_REMOTE_REPO_ID ) )
225         {
226             try
227             {
228                 request.addRemoteRepository( repositorySystem.createDefaultRemoteRepository( request ) );
229             }
230             catch ( Exception e )
231             {
232                 throw new MavenExecutionRequestPopulationException( "Cannot create default remote repository.", e );
233             }
234         }
235     }
236 
237     private void injectDefaultPluginRepositories( MavenExecutionRequest request )
238         throws MavenExecutionRequestPopulationException
239     {
240         Set<String> definedRepositories = repositorySystem.getRepoIds( request.getPluginArtifactRepositories() );
241 
242         if ( !definedRepositories.contains( RepositorySystem.DEFAULT_REMOTE_REPO_ID ) )
243         {
244             try
245             {
246                 request.addPluginArtifactRepository( repositorySystem.createDefaultRemoteRepository( request ) );
247             }
248             catch ( Exception e )
249             {
250                 throw new MavenExecutionRequestPopulationException( "Cannot create default remote repository.", e );
251             }
252         }
253     }
254 
255     private void processRepositoriesInSettings( MavenExecutionRequest request )
256         throws MavenExecutionRequestPopulationException
257     {
258         //
259         //    <settings>
260         //      <mirrors>
261         //        <mirror>
262         //          <id>central</id>
263         //          <!-- NOTE: We need to try and use the proper host name/ip as Java generally ignores proxies for
264         //                     "localhost" -->
265         //          <url>http://10.0.1.34:62247/</url>
266         //          <mirrorOf>central</mirrorOf>
267         //        </mirror>
268         //      </mirrors>
269         //      <proxies>
270         //        <proxy>
271         //          <active>true</active>
272         //          <protocol>http</protocol>
273         //          <host>localhost</host>
274         //          <port>62248</port>
275         //          <nonProxyHosts>10.0.1.34</nonProxyHosts>
276         //        </proxy>
277         //      </proxies>
278         //      <profiles>
279         //        <profile>
280         //          <id>it-defaults</id>
281         //          <!-- disable central override and use built-in values -->
282         //        </profile>
283         //      </profiles>
284         //      <activeProfiles>
285         //        <activeProfile>it-defaults</activeProfile>
286         //      </activeProfiles>
287         //    </settings>
288         //
289         // Turns
290         //
291         // http://repo1.maven.org/maven2
292         //
293         // to
294         //
295         // http://10.0.1.34:62247/
296         //
297         // Not sure why the DefaultMirrorSelector doesn't do this...
298         //
299         repositorySystem.injectMirror( request.getRemoteRepositories(), request.getMirrors() );
300         repositorySystem.injectMirror( request.getPluginArtifactRepositories(), request.getMirrors() );
301     }
302 
303     private void localRepository( MavenExecutionRequest request )
304         throws MavenExecutionRequestPopulationException
305     {
306         // ------------------------------------------------------------------------
307         // Local Repository
308         //
309         // 1. Use a value has been passed in via the configuration
310         // 2. Use value in the resultant settings
311         // 3. Use default value
312         // ------------------------------------------------------------------------
313 
314         if ( request.getLocalRepository() == null )
315         {
316             request.setLocalRepository( createLocalRepository( request ) );
317         }
318 
319         if ( request.getLocalRepositoryPath() == null )
320         {
321             request.setLocalRepositoryPath( new File( request.getLocalRepository().getBasedir() ).getAbsoluteFile() );
322         }
323     }
324 
325     // ------------------------------------------------------------------------
326     // Artifact Transfer Mechanism
327     // ------------------------------------------------------------------------
328 
329     private ArtifactRepository createLocalRepository( MavenExecutionRequest request )
330         throws MavenExecutionRequestPopulationException
331     {
332         String localRepositoryPath = null;
333 
334         if ( request.getLocalRepositoryPath() != null )
335         {
336             localRepositoryPath = request.getLocalRepositoryPath().getAbsolutePath();
337         }
338 
339         if ( StringUtils.isEmpty( localRepositoryPath ) )
340         {
341             localRepositoryPath = RepositorySystem.defaultUserLocalRepository.getAbsolutePath();
342         }
343 
344         try
345         {
346             return repositorySystem.createLocalRepository( request, new File( localRepositoryPath ) );
347         }
348         catch ( Exception e )
349         {
350             throw new MavenExecutionRequestPopulationException( "Cannot create local repository.", e );
351         }
352     }
353 
354     private void baseDirectory( MavenExecutionRequest request )
355     {
356         if ( request.getBaseDirectory() == null && request.getPom() != null )
357         {
358             request.setBaseDirectory( request.getPom().getAbsoluteFile().getParentFile() );
359         }
360     }   
361 }