001package 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
022import org.apache.maven.execution.MavenSession;
023import org.apache.maven.model.Plugin;
024import org.apache.maven.model.PluginManagement;
025import org.apache.maven.plugin.version.DefaultPluginVersionRequest;
026import org.apache.maven.plugin.version.PluginVersionRequest;
027import org.apache.maven.plugin.version.PluginVersionResolutionException;
028import org.apache.maven.plugin.version.PluginVersionResolver;
029import org.apache.maven.project.MavenProject;
030import org.codehaus.plexus.component.annotations.Component;
031import org.codehaus.plexus.component.annotations.Requirement;
032
033import java.util.HashMap;
034import 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 )
044public class LifecyclePluginResolver
045{
046    @Requirement
047    private PluginVersionResolver pluginVersionResolver;
048
049
050    public LifecyclePluginResolver( PluginVersionResolver pluginVersionResolver )
051    {
052        this.pluginVersionResolver = pluginVersionResolver;
053    }
054
055    public LifecyclePluginResolver()
056    {
057    }
058
059    public void resolveMissingPluginVersions( MavenProject project, MavenSession session )
060        throws PluginVersionResolutionException
061    {
062        Map<String, String> versions = new HashMap<>( 64 );
063
064        for ( Plugin plugin : project.getBuildPlugins() )
065        {
066            if ( plugin.getVersion() == null )
067            {
068                PluginVersionRequest request =
069                    new DefaultPluginVersionRequest( plugin, session.getRepositorySession(),
070                                                     project.getRemotePluginRepositories() );
071                plugin.setVersion( pluginVersionResolver.resolve( request ).getVersion() );
072            }
073            versions.put( plugin.getKey(), plugin.getVersion() );
074        }
075
076        PluginManagement pluginManagement = project.getPluginManagement();
077        if ( pluginManagement != null )
078        {
079            for ( Plugin plugin : pluginManagement.getPlugins() )
080            {
081                if ( plugin.getVersion() == null )
082                {
083                    plugin.setVersion( versions.get( plugin.getKey() ) );
084                    if ( plugin.getVersion() == null )
085                    {
086                        PluginVersionRequest request =
087                            new DefaultPluginVersionRequest( plugin, session.getRepositorySession(),
088                                                             project.getRemotePluginRepositories() );
089                        plugin.setVersion( pluginVersionResolver.resolve( request ).getVersion() );
090                    }
091                }
092            }
093        }
094    }
095}