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