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.ArrayList;
25 import java.util.Collections;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Map;
29
30 import org.apache.maven.api.model.Dependency;
31 import org.apache.maven.api.model.DependencyManagement;
32 import org.apache.maven.api.model.Exclusion;
33 import org.apache.maven.api.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 implements DependencyManagementInjector {
47
48 private ManagementModelMerger merger = new ManagementModelMerger();
49
50 @Override
51 public void injectManagement(
52 org.apache.maven.model.Model model, ModelBuildingRequest request, ModelProblemCollector problems) {
53 model.update(merger.mergeManagedDependencies(model.getDelegate()));
54 }
55
56
57
58
59 protected static class ManagementModelMerger extends MavenModelMerger {
60
61 public Model mergeManagedDependencies(Model model) {
62 DependencyManagement dependencyManagement = model.getDependencyManagement();
63 if (dependencyManagement != null) {
64 Map<Object, Dependency> dependencies = new HashMap<>();
65 Map<Object, Object> context = Collections.emptyMap();
66
67 for (Dependency dependency : model.getDependencies()) {
68 Object key = getDependencyKey().apply(dependency);
69 dependencies.put(key, dependency);
70 }
71
72 boolean modified = false;
73 for (Dependency managedDependency : dependencyManagement.getDependencies()) {
74 Object key = getDependencyKey().apply(managedDependency);
75 Dependency dependency = dependencies.get(key);
76 if (dependency != null) {
77 Dependency merged = mergeDependency(dependency, managedDependency, false, context);
78 if (merged != dependency) {
79 dependencies.put(key, merged);
80 modified = true;
81 }
82 }
83 }
84
85 if (modified) {
86 List<Dependency> newDeps = new ArrayList<>(dependencies.size());
87 for (Dependency dep : model.getDependencies()) {
88 Object key = getDependencyKey().apply(dep);
89 Dependency dependency = dependencies.get(key);
90 newDeps.add(dependency);
91 }
92 return Model.newBuilder(model).dependencies(newDeps).build();
93 }
94 }
95 return model;
96 }
97
98 @Override
99 protected void mergeDependency_Optional(
100 Dependency.Builder builder,
101 Dependency target,
102 Dependency source,
103 boolean sourceDominant,
104 Map<Object, Object> context) {
105
106 }
107
108 @Override
109 protected void mergeDependency_Exclusions(
110 Dependency.Builder builder,
111 Dependency target,
112 Dependency source,
113 boolean sourceDominant,
114 Map<Object, Object> context) {
115 List<Exclusion> tgt = target.getExclusions();
116 if (tgt.isEmpty()) {
117 List<Exclusion> src = source.getExclusions();
118 builder.exclusions(src);
119 }
120 }
121 }
122 }