001package org.apache.maven.execution;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.io.File;
023import java.util.ArrayList;
024import java.util.HashMap;
025import java.util.List;
026import java.util.Map;
027import java.util.Set;
028
029import javax.inject.Inject;
030import javax.inject.Named;
031
032import org.apache.maven.artifact.InvalidRepositoryException;
033import org.apache.maven.artifact.repository.ArtifactRepository;
034import org.apache.maven.bridge.MavenRepositorySystem;
035import org.apache.maven.repository.RepositorySystem;
036//
037// All of this needs to go away and be couched in terms of the execution request
038//
039import org.apache.maven.settings.Mirror;
040import org.apache.maven.settings.Proxy;
041import org.apache.maven.settings.Repository;
042import org.apache.maven.settings.Server;
043import org.apache.maven.settings.Settings;
044import org.apache.maven.settings.SettingsUtils;
045//
046// Settings in core
047//
048import org.apache.maven.toolchain.model.PersistedToolchains;
049import org.apache.maven.toolchain.model.ToolchainModel;
050import org.codehaus.plexus.util.StringUtils;
051
052@Named
053public class DefaultMavenExecutionRequestPopulator
054    implements MavenExecutionRequestPopulator
055{
056            
057    private final MavenRepositorySystem repositorySystem;
058    
059    @Inject
060    public DefaultMavenExecutionRequestPopulator( MavenRepositorySystem repositorySystem )
061    {
062        this.repositorySystem = repositorySystem;
063    }
064
065    @Override
066    public MavenExecutionRequest populateFromSettings( MavenExecutionRequest request, Settings settings )
067        throws MavenExecutionRequestPopulationException
068    {
069        if ( settings == null )
070        {
071            return request;
072        }
073
074        request.setOffline( settings.isOffline() );
075
076        request.setInteractiveMode( settings.isInteractiveMode() );
077
078        request.setPluginGroups( settings.getPluginGroups() );
079
080        request.setLocalRepositoryPath( settings.getLocalRepository() );
081
082        for ( Server server : settings.getServers() )
083        {
084            server = server.clone();
085
086            request.addServer( server );
087        }
088
089        //  <proxies>
090        //    <proxy>
091        //      <active>true</active>
092        //      <protocol>http</protocol>
093        //      <host>proxy.somewhere.com</host>
094        //      <port>8080</port>
095        //      <username>proxyuser</username>
096        //      <password>somepassword</password>
097        //      <nonProxyHosts>www.google.com|*.somewhere.com</nonProxyHosts>
098        //    </proxy>
099        //  </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}