1 package org.apache.maven.model.management;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.ArrayList;
23 import java.util.Collections;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27
28 import javax.inject.Named;
29 import javax.inject.Singleton;
30
31 import org.apache.maven.api.model.Dependency;
32 import org.apache.maven.api.model.DependencyManagement;
33 import org.apache.maven.api.model.Exclusion;
34 import org.apache.maven.api.model.Model;
35 import org.apache.maven.model.building.ModelBuildingRequest;
36 import org.apache.maven.model.building.ModelProblemCollector;
37 import org.apache.maven.model.merge.MavenModelMerger;
38
39
40
41
42
43
44 @SuppressWarnings( { "checkstyle:methodname" } )
45 @Named
46 @Singleton
47 public class DefaultDependencyManagementInjector
48 implements DependencyManagementInjector
49 {
50
51 private ManagementModelMerger merger = new ManagementModelMerger();
52
53 @Override
54 public void injectManagement( org.apache.maven.model.Model model, ModelBuildingRequest request,
55 ModelProblemCollector problems )
56 {
57 model.update( merger.mergeManagedDependencies( model.getDelegate() ) );
58 }
59
60
61
62
63 protected static class ManagementModelMerger
64 extends MavenModelMerger
65 {
66
67 public Model mergeManagedDependencies( Model model )
68 {
69 DependencyManagement dependencyManagement = model.getDependencyManagement();
70 if ( dependencyManagement != null )
71 {
72 Map<Object, Dependency> dependencies = new HashMap<>();
73 Map<Object, Object> context = Collections.emptyMap();
74
75 for ( Dependency dependency : model.getDependencies() )
76 {
77 Object key = getDependencyKey().apply( dependency );
78 dependencies.put( key, dependency );
79 }
80
81 boolean modified = false;
82 for ( Dependency managedDependency : dependencyManagement.getDependencies() )
83 {
84 Object key = getDependencyKey().apply( managedDependency );
85 Dependency dependency = dependencies.get( key );
86 if ( dependency != null )
87 {
88 Dependency merged = mergeDependency( dependency, managedDependency, false, context );
89 if ( merged != dependency )
90 {
91 dependencies.put( key, merged );
92 modified = true;
93 }
94 }
95 }
96
97 if ( modified )
98 {
99 List<Dependency> newDeps = new ArrayList<>( dependencies.size() );
100 for ( Dependency dep : model.getDependencies() )
101 {
102 Object key = getDependencyKey().apply( dep );
103 Dependency dependency = dependencies.get( key );
104 newDeps.add( dependency );
105 }
106 return Model.newBuilder( model ).dependencies( newDeps ).build();
107 }
108 }
109 return model;
110 }
111
112 @Override
113 protected void mergeDependency_Optional( Dependency.Builder builder,
114 Dependency target, Dependency source, boolean sourceDominant,
115 Map<Object, Object> context )
116 {
117
118 }
119
120 @Override
121 protected void mergeDependency_Exclusions( Dependency.Builder builder,
122 Dependency target, Dependency source, boolean sourceDominant,
123 Map<Object, Object> context )
124 {
125 List<Exclusion> tgt = target.getExclusions();
126 if ( tgt.isEmpty() )
127 {
128 List<Exclusion> src = source.getExclusions();
129 builder.exclusions( src );
130 }
131 }
132
133 }
134
135 }