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