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 import java.util.function.Function;
28
29 import javax.inject.Named;
30 import javax.inject.Singleton;
31
32 import org.apache.maven.api.model.Build;
33 import org.apache.maven.api.model.Dependency;
34 import org.apache.maven.api.model.Model;
35 import org.apache.maven.api.model.Plugin;
36 import org.apache.maven.model.building.ModelBuildingRequest;
37 import org.apache.maven.model.building.ModelProblemCollector;
38 import org.apache.maven.model.merge.MavenModelMerger;
39 import org.codehaus.plexus.util.StringUtils;
40
41
42
43
44
45
46 @Named
47 @Singleton
48 public class DefaultModelNormalizer
49 implements ModelNormalizer
50 {
51
52 private DuplicateMerger merger = new DuplicateMerger();
53
54 @Override
55 public Model mergeDuplicates( Model model, ModelBuildingRequest request, ModelProblemCollector problems )
56 {
57 Model.Builder builder = Model.newBuilder( model );
58
59 Build build = model.getBuild();
60 if ( build != null )
61 {
62 List<Plugin> plugins = build.getPlugins();
63 Map<Object, Plugin> normalized = new LinkedHashMap<>( plugins.size() * 2 );
64
65 for ( Plugin plugin : plugins )
66 {
67 Object key = plugin.getKey();
68 Plugin first = normalized.get( key );
69 if ( first != null )
70 {
71 plugin = merger.mergePlugin( plugin, first );
72 }
73 normalized.put( key, plugin );
74 }
75
76 if ( plugins.size() != normalized.size() )
77 {
78 builder.build( Build.newBuilder( build )
79 .plugins( normalized.values() )
80 .build() );
81 }
82 }
83
84
85
86
87
88
89
90
91 List<Dependency> dependencies = model.getDependencies();
92 Map<String, Dependency> normalized = new LinkedHashMap<>( dependencies.size() * 2 );
93
94 for ( Dependency dependency : dependencies )
95 {
96 normalized.put( dependency.getManagementKey(), dependency );
97 }
98
99 if ( dependencies.size() != normalized.size() )
100 {
101 builder.dependencies( normalized.values() );
102 }
103
104 return builder.build();
105 }
106
107
108
109
110 protected static class DuplicateMerger
111 extends MavenModelMerger
112 {
113
114 public Plugin mergePlugin( Plugin target, Plugin source )
115 {
116 return super.mergePlugin( target, source, false, Collections.emptyMap() );
117 }
118
119 }
120
121 @Override
122 public Model injectDefaultValues( Model model, ModelBuildingRequest request, ModelProblemCollector problems )
123 {
124 Model.Builder builder = Model.newBuilder( model );
125
126 builder.dependencies( injectList( model.getDependencies(), this::injectDependency ) );
127 Build build = model.getBuild();
128 if ( build != null )
129 {
130 Build newBuild = Build.newBuilder( build )
131 .plugins( injectList( build.getPlugins(), this::injectPlugin ) )
132 .build();
133 builder.build( newBuild != build ? newBuild : null );
134 }
135
136 return builder.build();
137 }
138
139 private Plugin injectPlugin( Plugin p )
140 {
141 return Plugin.newBuilder( p )
142 .dependencies( injectList( p.getDependencies(), this::injectDependency ) )
143 .build();
144 }
145
146 private Dependency injectDependency( Dependency d )
147 {
148
149 return StringUtils.isEmpty( d.getScope() ) ? Dependency.newBuilder( d ).scope( "compile" ).build() : d;
150 }
151
152
153
154
155 private <T> List<T> injectList( List<T> list, Function<T, T> modifer )
156 {
157 List<T> newList = null;
158 for ( int i = 0; i < list.size(); i++ )
159 {
160 T oldT = list.get( i );
161 T newT = modifer.apply( oldT );
162 if ( newT != oldT )
163 {
164 if ( newList == null )
165 {
166 newList = new ArrayList<>( list );
167 }
168 newList.set( i, newT );
169 }
170 }
171 return newList;
172 }
173
174 }