001package org.apache.maven.model.management;
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.util.Collections;
023import java.util.HashMap;
024import java.util.List;
025import java.util.Map;
026
027import org.apache.maven.model.Dependency;
028import org.apache.maven.model.DependencyManagement;
029import org.apache.maven.model.Exclusion;
030import org.apache.maven.model.Model;
031import org.apache.maven.model.building.ModelBuildingRequest;
032import org.apache.maven.model.building.ModelProblemCollector;
033import org.apache.maven.model.merge.MavenModelMerger;
034import org.codehaus.plexus.component.annotations.Component;
035
036/**
037 * Handles injection of dependency management into the model.
038 *
039 * @author Benjamin Bentmann
040 */
041@Component( role = DependencyManagementInjector.class )
042public class DefaultDependencyManagementInjector
043    implements DependencyManagementInjector
044{
045
046    private ManagementModelMerger merger = new ManagementModelMerger();
047
048    @Override
049    public void injectManagement( Model model, ModelBuildingRequest request, ModelProblemCollector problems )
050    {
051        merger.mergeManagedDependencies( model );
052    }
053
054    protected static class ManagementModelMerger
055        extends MavenModelMerger
056    {
057
058        public void mergeManagedDependencies( Model model )
059        {
060            DependencyManagement dependencyManagement = model.getDependencyManagement();
061            if ( dependencyManagement != null )
062            {
063                Map<Object, Dependency> dependencies = new HashMap<>();
064                Map<Object, Object> context = Collections.emptyMap();
065
066                for ( Dependency dependency : model.getDependencies() )
067                {
068                    Object key = getDependencyKey( dependency );
069                    dependencies.put( key, dependency );
070                }
071
072                for ( Dependency managedDependency : dependencyManagement.getDependencies() )
073                {
074                    Object key = getDependencyKey( managedDependency );
075                    Dependency dependency = dependencies.get( key );
076                    if ( dependency != null )
077                    {
078                        mergeDependency( dependency, managedDependency, false, context );
079                    }
080                }
081            }
082        }
083
084        @Override
085        protected void mergeDependency_Optional( Dependency target, Dependency source, boolean sourceDominant,
086                                                 Map<Object, Object> context )
087        {
088            // optional flag is not managed
089        }
090
091        @Override
092        protected void mergeDependency_Exclusions( Dependency target, Dependency source, boolean sourceDominant,
093                                                   Map<Object, Object> context )
094        {
095            List<Exclusion> tgt = target.getExclusions();
096            if ( tgt.isEmpty() )
097            {
098                List<Exclusion> src = source.getExclusions();
099
100                for ( Exclusion element : src )
101                {
102                    Exclusion clone = element.clone();
103                    target.addExclusion( clone );
104                }
105            }
106        }
107
108    }
109
110}