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 public void injectManagement( Model model, ModelBuildingRequest request, ModelProblemCollector problems ) 049 { 050 merger.mergeManagedDependencies( model ); 051 } 052 053 protected static class ManagementModelMerger 054 extends MavenModelMerger 055 { 056 057 public void mergeManagedDependencies( Model model ) 058 { 059 DependencyManagement dependencyManagement = model.getDependencyManagement(); 060 if ( dependencyManagement != null ) 061 { 062 Map<Object, Dependency> dependencies = new HashMap<Object, Dependency>(); 063 Map<Object, Object> context = Collections.emptyMap(); 064 065 for ( Dependency dependency : model.getDependencies() ) 066 { 067 Object key = getDependencyKey( dependency ); 068 dependencies.put( key, dependency ); 069 } 070 071 for ( Dependency managedDependency : dependencyManagement.getDependencies() ) 072 { 073 Object key = getDependencyKey( managedDependency ); 074 Dependency dependency = dependencies.get( key ); 075 if ( dependency != null ) 076 { 077 mergeDependency( dependency, managedDependency, false, context ); 078 } 079 } 080 } 081 } 082 083 @Override 084 protected void mergeDependency_Optional( Dependency target, Dependency source, boolean sourceDominant, 085 Map<Object, Object> context ) 086 { 087 // optional flag is not managed 088 } 089 090 @Override 091 protected void mergeDependency_Exclusions( Dependency target, Dependency source, boolean sourceDominant, 092 Map<Object, Object> context ) 093 { 094 List<Exclusion> tgt = target.getExclusions(); 095 if ( tgt.isEmpty() ) 096 { 097 List<Exclusion> src = source.getExclusions(); 098 099 for ( Exclusion element : src ) 100 { 101 Exclusion clone = element.clone(); 102 target.addExclusion( clone ); 103 } 104 } 105 } 106 107 } 108 109}