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