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
066    @Override
067    public MavenExecutionRequest populateFromToolchains( MavenExecutionRequest request, PersistedToolchains toolchains )
068        throws MavenExecutionRequestPopulationException
069    {
070        if ( toolchains != null )
071        {
072            Map<String, List<ToolchainModel>> groupedToolchains = new HashMap<String, List<ToolchainModel>>( 2 );
073
074            for ( ToolchainModel model : toolchains.getToolchains() )
075            {
076                if ( !groupedToolchains.containsKey( model.getType() ) )
077                {
078                    groupedToolchains.put( model.getType(), new ArrayList<ToolchainModel>() );
079                }
080
081                groupedToolchains.get( model.getType() ).add( model );
082            }
083
084            request.setToolchains( groupedToolchains );
085        }
086        return request;
087    }
088    
089    @Override
090    public MavenExecutionRequest populateDefaults( MavenExecutionRequest request )
091        throws MavenExecutionRequestPopulationException
092    {
093        baseDirectory( request );
094
095        localRepository( request );
096
097        populateDefaultPluginGroups( request );
098
099        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}