1 package org.apache.maven.model.management;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.Collections;
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Map;
26
27 import javax.inject.Named;
28 import javax.inject.Singleton;
29
30 import org.apache.maven.model.Dependency;
31 import org.apache.maven.model.DependencyManagement;
32 import org.apache.maven.model.Exclusion;
33 import org.apache.maven.model.Model;
34 import org.apache.maven.model.building.ModelBuildingRequest;
35 import org.apache.maven.model.building.ModelProblemCollector;
36 import org.apache.maven.model.merge.MavenModelMerger;
37
38
39
40
41
42
43 @SuppressWarnings( { "checkstyle:methodname" } )
44 @Named
45 @Singleton
46 public class DefaultDependencyManagementInjector
47 implements DependencyManagementInjector
48 {
49
50 private ManagementModelMerger merger = new ManagementModelMerger();
51
52 @Override
53 public void injectManagement( Model model, ModelBuildingRequest request, ModelProblemCollector problems )
54 {
55 merger.mergeManagedDependencies( model );
56 }
57
58
59
60
61 protected static class ManagementModelMerger
62 extends MavenModelMerger
63 {
64
65 public void mergeManagedDependencies( Model model )
66 {
67 DependencyManagement dependencyManagement = model.getDependencyManagement();
68 if ( dependencyManagement != null )
69 {
70 Map<Object, Dependency> dependencies = new HashMap<>();
71 Map<Object, Object> context = Collections.emptyMap();
72
73 for ( Dependency dependency : model.getDependencies() )
74 {
75 Object key = getDependencyKey( dependency );
76 dependencies.put( key, dependency );
77 }
78
79 for ( Dependency managedDependency : dependencyManagement.getDependencies() )
80 {
81 Object key = getDependencyKey( managedDependency );
82 Dependency dependency = dependencies.get( key );
83 if ( dependency != null )
84 {
85 mergeDependency( dependency, managedDependency, false, context );
86 }
87 }
88 }
89 }
90
91 @Override
92 protected void mergeDependency_Optional( Dependency target, Dependency source, boolean sourceDominant,
93 Map<Object, Object> context )
94 {
95
96 }
97
98 @Override
99 protected void mergeDependency_Exclusions( Dependency target, Dependency source, boolean sourceDominant,
100 Map<Object, Object> context )
101 {
102 List<Exclusion> tgt = target.getExclusions();
103 if ( tgt.isEmpty() )
104 {
105 List<Exclusion> src = source.getExclusions();
106
107 for ( Exclusion element : src )
108 {
109 Exclusion clone = element.clone();
110 target.addExclusion( clone );
111 }
112 }
113 }
114
115 }
116
117 }