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