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