1 package org.apache.maven.model.normalization;
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.LinkedHashMap;
25 import java.util.List;
26 import java.util.Map;
27
28 import org.apache.maven.model.Build;
29 import org.apache.maven.model.Dependency;
30 import org.apache.maven.model.Model;
31 import org.apache.maven.model.Plugin;
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 import org.codehaus.plexus.component.annotations.Component;
36 import org.codehaus.plexus.util.StringUtils;
37
38
39
40
41
42
43 @Component( role = ModelNormalizer.class )
44 public class DefaultModelNormalizer
45 implements ModelNormalizer
46 {
47
48 private DuplicateMerger merger = new DuplicateMerger();
49
50 public void mergeDuplicates( Model model, ModelBuildingRequest request, ModelProblemCollector problems )
51 {
52 Build build = model.getBuild();
53 if ( build != null )
54 {
55 List<Plugin> plugins = build.getPlugins();
56 Map<Object, Plugin> normalized = new LinkedHashMap<Object, Plugin>( plugins.size() * 2 );
57
58 for ( Plugin plugin : plugins )
59 {
60 Object key = plugin.getKey();
61 Plugin first = normalized.get( key );
62 if ( first != null )
63 {
64 merger.mergePlugin( plugin, first );
65 }
66 normalized.put( key, plugin );
67 }
68
69 if ( plugins.size() != normalized.size() )
70 {
71 build.setPlugins( new ArrayList<Plugin>( normalized.values() ) );
72 }
73 }
74
75
76
77
78
79
80
81
82 List<Dependency> dependencies = model.getDependencies();
83 Map<String, Dependency> normalized = new LinkedHashMap<String, Dependency>( dependencies.size() * 2 );
84
85 for ( Dependency dependency : dependencies )
86 {
87 normalized.put( dependency.getManagementKey(), dependency );
88 }
89
90 if ( dependencies.size() != normalized.size() )
91 {
92 model.setDependencies( new ArrayList<Dependency>( normalized.values() ) );
93 }
94 }
95
96 private static class DuplicateMerger
97 extends MavenModelMerger
98 {
99
100 public void mergePlugin( Plugin target, Plugin source )
101 {
102 super.mergePlugin( target, source, false, Collections.emptyMap() );
103 }
104
105 }
106
107 public void injectDefaultValues( Model model, ModelBuildingRequest request, ModelProblemCollector problems )
108 {
109 injectDependencyDefaults( model.getDependencies() );
110
111 Build build = model.getBuild();
112 if ( build != null )
113 {
114 for ( Plugin plugin : build.getPlugins() )
115 {
116 injectDependencyDefaults( plugin.getDependencies() );
117 }
118 }
119 }
120
121 private void injectDependencyDefaults( List<Dependency> dependencies )
122 {
123 for ( Dependency dependency : dependencies )
124 {
125 if ( StringUtils.isEmpty( dependency.getScope() ) )
126 {
127
128 dependency.setScope( "compile" );
129 }
130 }
131 }
132
133 }