001    package org.apache.maven.lifecycle.internal;
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    
022    import org.apache.maven.execution.MavenSession;
023    import org.apache.maven.model.Plugin;
024    import org.apache.maven.model.PluginManagement;
025    import org.apache.maven.plugin.version.DefaultPluginVersionRequest;
026    import org.apache.maven.plugin.version.PluginVersionRequest;
027    import org.apache.maven.plugin.version.PluginVersionResolutionException;
028    import org.apache.maven.plugin.version.PluginVersionResolver;
029    import org.apache.maven.project.MavenProject;
030    import org.codehaus.plexus.component.annotations.Component;
031    import org.codehaus.plexus.component.annotations.Requirement;
032    
033    import java.util.HashMap;
034    import java.util.Map;
035    
036    /**
037     * @since 3.0
038     * @author Benjamin Bentmann
039     * @author Kristian Rosenvold (Extract class)
040     *         <p/>
041     *         NOTE: This class is not part of any public api and can be changed or deleted without prior notice.
042     */
043    @Component( role = LifecyclePluginResolver.class )
044    public class LifecyclePluginResolver
045    {
046        @Requirement
047        private PluginVersionResolver pluginVersionResolver;
048    
049    
050        public LifecyclePluginResolver( PluginVersionResolver pluginVersionResolver )
051        {
052            this.pluginVersionResolver = pluginVersionResolver;
053        }
054    
055        @SuppressWarnings( { "UnusedDeclaration" } )
056        public LifecyclePluginResolver()
057        {
058        }
059    
060        public void resolveMissingPluginVersions( MavenProject project, MavenSession session )
061            throws PluginVersionResolutionException
062        {
063            Map<String, String> versions = new HashMap<String, String>( 64 );
064    
065            for ( Plugin plugin : project.getBuildPlugins() )
066            {
067                if ( plugin.getVersion() == null )
068                {
069                    PluginVersionRequest request =
070                        new DefaultPluginVersionRequest( plugin, session.getRepositorySession(),
071                                                         project.getRemotePluginRepositories() );
072                    plugin.setVersion( pluginVersionResolver.resolve( request ).getVersion() );
073                }
074                versions.put( plugin.getKey(), plugin.getVersion() );
075            }
076    
077            PluginManagement pluginManagement = project.getPluginManagement();
078            if ( pluginManagement != null )
079            {
080                for ( Plugin plugin : pluginManagement.getPlugins() )
081                {
082                    if ( plugin.getVersion() == null )
083                    {
084                        plugin.setVersion( versions.get( plugin.getKey() ) );
085                        if ( plugin.getVersion() == null )
086                        {
087                            PluginVersionRequest request =
088                                new DefaultPluginVersionRequest( plugin, session.getRepositorySession(),
089                                                                 project.getRemotePluginRepositories() );
090                            plugin.setVersion( pluginVersionResolver.resolve( request ).getVersion() );
091                        }
092                    }
093                }
094            }
095        }
096    }