1 package org.apache.maven.model.profile;
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.BuildBase;
30 import org.apache.maven.model.Model;
31 import org.apache.maven.model.ModelBase;
32 import org.apache.maven.model.Plugin;
33 import org.apache.maven.model.PluginContainer;
34 import org.apache.maven.model.Profile;
35 import org.apache.maven.model.ReportPlugin;
36 import org.apache.maven.model.Reporting;
37 import org.apache.maven.model.building.ModelBuildingRequest;
38 import org.apache.maven.model.building.ModelProblemCollector;
39 import org.apache.maven.model.merge.MavenModelMerger;
40 import org.codehaus.plexus.component.annotations.Component;
41
42
43
44
45
46
47 @Component( role = ProfileInjector.class )
48 public class DefaultProfileInjector
49 implements ProfileInjector
50 {
51
52 private ProfileModelMerger merger = new ProfileModelMerger();
53
54 public void injectProfile( Model model, Profile profile, ModelBuildingRequest request,
55 ModelProblemCollector problems )
56 {
57 if ( profile != null )
58 {
59 merger.mergeModelBase( model, profile );
60
61 if ( profile.getBuild() != null )
62 {
63 if ( model.getBuild() == null )
64 {
65 model.setBuild( new Build() );
66 }
67 merger.mergeBuildBase( model.getBuild(), profile.getBuild() );
68 }
69 }
70 }
71
72 private static class ProfileModelMerger
73 extends MavenModelMerger
74 {
75
76 public void mergeModelBase( ModelBase target, ModelBase source )
77 {
78 mergeModelBase( target, source, true, Collections.emptyMap() );
79 }
80
81 public void mergeBuildBase( BuildBase target, BuildBase source )
82 {
83 mergeBuildBase( target, source, true, Collections.emptyMap() );
84 }
85
86 @Override
87 protected void mergePluginContainer_Plugins( PluginContainer target, PluginContainer source,
88 boolean sourceDominant, Map<Object, Object> context )
89 {
90 List<Plugin> src = source.getPlugins();
91 if ( !src.isEmpty() )
92 {
93 List<Plugin> tgt = target.getPlugins();
94 Map<Object, Plugin> master = new LinkedHashMap<Object, Plugin>( tgt.size() * 2 );
95
96 for ( Plugin element : tgt )
97 {
98 Object key = getPluginKey( element );
99 master.put( key, element );
100 }
101
102 Map<Object, List<Plugin>> predecessors = new LinkedHashMap<Object, List<Plugin>>();
103 List<Plugin> pending = new ArrayList<Plugin>();
104 for ( Plugin element : src )
105 {
106 Object key = getPluginKey( element );
107 Plugin existing = master.get( key );
108 if ( existing != null )
109 {
110 mergePlugin( existing, element, sourceDominant, context );
111
112 if ( !pending.isEmpty() )
113 {
114 predecessors.put( key, pending );
115 pending = new ArrayList<Plugin>();
116 }
117 }
118 else
119 {
120 pending.add( element );
121 }
122 }
123
124 List<Plugin> result = new ArrayList<Plugin>( src.size() + tgt.size() );
125 for ( Map.Entry<Object, Plugin> entry : master.entrySet() )
126 {
127 List<Plugin> pre = predecessors.get( entry.getKey() );
128 if ( pre != null )
129 {
130 result.addAll( pre );
131 }
132 result.add( entry.getValue() );
133 }
134 result.addAll( pending );
135
136 target.setPlugins( result );
137 }
138 }
139
140 @Override
141 protected void mergeReporting_Plugins( Reporting target, Reporting source, boolean sourceDominant,
142 Map<Object, Object> context )
143 {
144 List<ReportPlugin> src = source.getPlugins();
145 if ( !src.isEmpty() )
146 {
147 List<ReportPlugin> tgt = target.getPlugins();
148 Map<Object, ReportPlugin> merged =
149 new LinkedHashMap<Object, ReportPlugin>( ( src.size() + tgt.size() ) * 2 );
150
151 for ( ReportPlugin element : tgt )
152 {
153 Object key = getReportPluginKey( element );
154 merged.put( key, element );
155 }
156
157 for ( ReportPlugin element : src )
158 {
159 Object key = getReportPluginKey( element );
160 ReportPlugin existing = merged.get( key );
161 if ( existing == null )
162 {
163 merged.put( key, element );
164 }
165 else
166 {
167 mergeReportPlugin( existing, element, sourceDominant, context );
168 }
169 }
170
171 target.setPlugins( new ArrayList<ReportPlugin>( merged.values() ) );
172 }
173 }
174
175 }
176
177 }