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  
66      @Override
67      public MavenExecutionRequest populateFromToolchains( MavenExecutionRequest request, PersistedToolchains toolchains )
68          throws MavenExecutionRequestPopulationException
69      {
70          if ( toolchains != null )
71          {
72              Map<String, List<ToolchainModel>> groupedToolchains = new HashMap<String, List<ToolchainModel>>( 2 );
73  
74              for ( ToolchainModel model : toolchains.getToolchains() )
75              {
76                  if ( !groupedToolchains.containsKey( model.getType() ) )
77                  {
78                      groupedToolchains.put( model.getType(), new ArrayList<ToolchainModel>() );
79                  }
80  
81                  groupedToolchains.get( model.getType() ).add( model );
82              }
83  
84              request.setToolchains( groupedToolchains );
85          }
86          return request;
87      }
88      
89      @Override
90      public MavenExecutionRequest populateDefaults( MavenExecutionRequest request )
91          throws MavenExecutionRequestPopulationException
92      {
93          baseDirectory( request );
94  
95          localRepository( request );
96  
97          populateDefaultPluginGroups( request );
98  
99          injectDefaultRepositories( request );
100 
101         injectDefaultPluginRepositories( request );
102 
103         return request;
104     }
105     
106     //
107     //
108     //
109     
110     private void populateDefaultPluginGroups( MavenExecutionRequest request )
111     {
112         request.addPluginGroup( "org.apache.maven.plugins" );
113         request.addPluginGroup( "org.codehaus.mojo" );
114     }
115 
116     private void injectDefaultRepositories( MavenExecutionRequest request )
117         throws MavenExecutionRequestPopulationException
118     {
119         Set<String> definedRepositories = repositorySystem.getRepoIds( request.getRemoteRepositories() );
120 
121         if ( !definedRepositories.contains( RepositorySystem.DEFAULT_REMOTE_REPO_ID ) )
122         {
123             try
124             {
125                 request.addRemoteRepository( repositorySystem.createDefaultRemoteRepository( request ) );
126             }
127             catch ( Exception e )
128             {
129                 throw new MavenExecutionRequestPopulationException( "Cannot create default remote repository.", e );
130             }
131         }
132     }
133 
134     private void injectDefaultPluginRepositories( MavenExecutionRequest request )
135         throws MavenExecutionRequestPopulationException
136     {
137         Set<String> definedRepositories = repositorySystem.getRepoIds( request.getPluginArtifactRepositories() );
138 
139         if ( !definedRepositories.contains( RepositorySystem.DEFAULT_REMOTE_REPO_ID ) )
140         {
141             try
142             {
143                 request.addPluginArtifactRepository( repositorySystem.createDefaultRemoteRepository( request ) );
144             }
145             catch ( Exception e )
146             {
147                 throw new MavenExecutionRequestPopulationException( "Cannot create default remote repository.", e );
148             }
149         }
150     }
151 
152     private void localRepository( MavenExecutionRequest request )
153         throws MavenExecutionRequestPopulationException
154     {
155         // ------------------------------------------------------------------------
156         // Local Repository
157         //
158         // 1. Use a value has been passed in via the configuration
159         // 2. Use value in the resultant settings
160         // 3. Use default value
161         // ------------------------------------------------------------------------
162 
163         if ( request.getLocalRepository() == null )
164         {
165             request.setLocalRepository( createLocalRepository( request ) );
166         }
167 
168         if ( request.getLocalRepositoryPath() == null )
169         {
170             request.setLocalRepositoryPath( new File( request.getLocalRepository().getBasedir() ).getAbsoluteFile() );
171         }
172     }
173 
174     // ------------------------------------------------------------------------
175     // Artifact Transfer Mechanism
176     // ------------------------------------------------------------------------
177 
178     private ArtifactRepository createLocalRepository( MavenExecutionRequest request )
179         throws MavenExecutionRequestPopulationException
180     {
181         String localRepositoryPath = null;
182 
183         if ( request.getLocalRepositoryPath() != null )
184         {
185             localRepositoryPath = request.getLocalRepositoryPath().getAbsolutePath();
186         }
187 
188         if ( StringUtils.isEmpty( localRepositoryPath ) )
189         {
190             localRepositoryPath = RepositorySystem.defaultUserLocalRepository.getAbsolutePath();
191         }
192 
193         try
194         {
195             return repositorySystem.createLocalRepository( request, new File( localRepositoryPath ) );
196         }
197         catch ( Exception e )
198         {
199             throw new MavenExecutionRequestPopulationException( "Cannot create local repository.", e );
200         }
201     }
202 
203     private void baseDirectory( MavenExecutionRequest request )
204     {
205         if ( request.getBaseDirectory() == null && request.getPom() != null )
206         {
207             request.setBaseDirectory( request.getPom().getAbsoluteFile().getParentFile() );
208         }
209     }   
210     
211     /*if_not[MAVEN4]*/
212     
213     @Override
214     @Deprecated
215     public MavenExecutionRequest populateFromSettings( MavenExecutionRequest request, Settings settings )
216         throws MavenExecutionRequestPopulationException
217     {
218         if ( settings == null )
219         {
220             return request;
221         }
222 
223         request.setOffline( settings.isOffline() );
224 
225         request.setInteractiveMode( settings.isInteractiveMode() );
226 
227         request.setPluginGroups( settings.getPluginGroups() );
228 
229         request.setLocalRepositoryPath( settings.getLocalRepository() );
230 
231         for ( Server server : settings.getServers() )
232         {
233             server = server.clone();
234 
235             request.addServer( server );
236         }
237 
238         //  <proxies>
239         //    <proxy>
240         //      <active>true</active>
241         //      <protocol>http</protocol>
242         //      <host>proxy.somewhere.com</host>
243         //      <port>8080</port>
244         //      <username>proxyuser</username>
245         //      <password>somepassword</password>
246         //      <nonProxyHosts>www.google.com|*.somewhere.com</nonProxyHosts>
247         //    </proxy>
248         //  </proxies>
249 
250         for ( Proxy proxy : settings.getProxies() )
251         {
252             if ( !proxy.isActive() )
253             {
254                 continue;
255             }
256 
257             proxy = proxy.clone();
258 
259             request.addProxy( proxy );
260         }
261 
262         // <mirrors>
263         //   <mirror>
264         //     <id>nexus</id>
265         //     <mirrorOf>*</mirrorOf>
266         //     <url>http://repository.sonatype.org/content/groups/public</url>
267         //   </mirror>
268         // </mirrors>
269 
270         for ( Mirror mirror : settings.getMirrors() )
271         {
272             mirror = mirror.clone();
273 
274             request.addMirror( mirror );
275         }
276 
277         request.setActiveProfiles( settings.getActiveProfiles() );
278 
279         for ( org.apache.maven.settings.Profile rawProfile : settings.getProfiles() )
280         {
281             request.addProfile( SettingsUtils.convertFromSettingsProfile( rawProfile ) );
282 
283             if ( settings.getActiveProfiles().contains( rawProfile.getId() ) )
284             {
285                 List<Repository> remoteRepositories = rawProfile.getRepositories();
286                 for ( Repository remoteRepository : remoteRepositories )
287                 {
288                     try
289                     {
290                         request.addRemoteRepository( repositorySystem.buildArtifactRepository( remoteRepository ) );
291                     }
292                     catch ( InvalidRepositoryException e )
293                     {
294                         // do nothing for now
295                     }
296                 }
297 
298                 List<Repository> pluginRepositories = rawProfile.getPluginRepositories();
299                 for ( Repository pluginRepo : pluginRepositories )
300                 {
301                     try
302                     {
303                         request.addPluginArtifactRepository( repositorySystem.buildArtifactRepository( pluginRepo ) );
304                     }
305                     catch ( InvalidRepositoryException e )
306                     {
307                         // do nothing for now
308                     }
309                 }
310             }
311         }
312 
313         return request;
314     }    
315     
316     /*end[MAVEN4]*/
317 
318 }