1 package org.apache.maven.project.injection;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.model.Build;
23 import org.apache.maven.model.Dependency;
24 import org.apache.maven.model.DependencyManagement;
25 import org.apache.maven.model.Model;
26 import org.apache.maven.model.Plugin;
27 import org.apache.maven.model.PluginManagement;
28 import org.apache.maven.project.ModelUtils;
29
30 import java.util.Iterator;
31 import java.util.List;
32 import java.util.Map;
33 import java.util.TreeMap;
34
35
36
37
38 public class DefaultModelDefaultsInjector
39 implements ModelDefaultsInjector
40 {
41 public void injectDefaults( Model model )
42 {
43 injectDependencyDefaults( model.getDependencies(), model.getDependencyManagement() );
44 if ( model.getBuild() != null )
45 {
46 injectPluginDefaults( model.getBuild(), model.getBuild().getPluginManagement() );
47 }
48 }
49
50 private void injectPluginDefaults( Build build, PluginManagement pluginManagement )
51 {
52 if ( pluginManagement == null )
53 {
54
55 return ;
56 }
57
58 List buildPlugins = build.getPlugins();
59
60 if ( buildPlugins != null && !buildPlugins.isEmpty() )
61 {
62 Map pmPlugins = pluginManagement.getPluginsAsMap();
63
64 if ( pmPlugins != null && !pmPlugins.isEmpty() )
65 {
66 for ( Iterator it = buildPlugins.iterator(); it.hasNext(); )
67 {
68 Plugin buildPlugin = (Plugin) it.next();
69
70 Plugin pmPlugin = (Plugin) pmPlugins.get( buildPlugin.getKey() );
71
72 if ( pmPlugin != null )
73 {
74 mergePluginWithDefaults( buildPlugin, pmPlugin );
75 }
76 }
77 }
78 }
79
80 }
81
82 public void mergePluginWithDefaults( Plugin plugin, Plugin def )
83 {
84 ModelUtils.mergePluginDefinitions( plugin, def, false );
85 }
86
87 private void injectDependencyDefaults( List dependencies, DependencyManagement dependencyManagement )
88 {
89 if ( dependencyManagement != null )
90 {
91
92
93
94
95 Map depsMap = new TreeMap();
96 for ( Iterator it = dependencies.iterator(); it.hasNext(); )
97 {
98 Dependency dep = (Dependency) it.next();
99 depsMap.put( dep.getManagementKey(), dep );
100 }
101
102 List managedDependencies = dependencyManagement.getDependencies();
103
104 for ( Iterator it = managedDependencies.iterator(); it.hasNext(); )
105 {
106 Dependency def = (Dependency) it.next();
107 String key = def.getManagementKey();
108
109 Dependency dep = (Dependency) depsMap.get( key );
110 if ( dep != null )
111 {
112 mergeDependencyWithDefaults( dep, def );
113 }
114 }
115 }
116 }
117
118 private void mergeDependencyWithDefaults( Dependency dep, Dependency def )
119 {
120 if ( dep.getScope() == null && def.getScope() != null )
121 {
122 dep.setScope( def.getScope() );
123 dep.setSystemPath( def.getSystemPath() );
124 }
125
126 if ( dep.getVersion() == null && def.getVersion() != null )
127 {
128 dep.setVersion( def.getVersion() );
129 }
130
131 if ( dep.getClassifier() == null && def.getClassifier() != null )
132 {
133 dep.setClassifier( def.getClassifier() );
134 }
135
136 if ( dep.getType() == null && def.getType() != null )
137 {
138 dep.setType( def.getType() );
139 }
140
141 List exclusions = dep.getExclusions();
142 if ( exclusions == null || exclusions.isEmpty() )
143 {
144 dep.setExclusions( def.getExclusions() );
145 }
146 }
147
148 }