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