1   package org.apache.maven.model.inheritance;
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.HashMap;
24  import java.util.LinkedHashMap;
25  import java.util.List;
26  import java.util.Map;
27  import java.util.Properties;
28  
29  import javax.inject.Named;
30  import javax.inject.Singleton;
31  
32  import org.apache.maven.model.InputLocation;
33  import org.apache.maven.model.Model;
34  import org.apache.maven.model.ModelBase;
35  import org.apache.maven.model.Plugin;
36  import org.apache.maven.model.PluginContainer;
37  import org.apache.maven.model.ReportPlugin;
38  import org.apache.maven.model.Reporting;
39  import org.apache.maven.model.building.ModelBuildingRequest;
40  import org.apache.maven.model.building.ModelProblemCollector;
41  import org.apache.maven.model.merge.MavenModelMerger;
42  import org.codehaus.plexus.util.StringUtils;
43  
44  
45  
46  
47  
48  
49  @SuppressWarnings( { "checkstyle:methodname" } )
50  @Named
51  @Singleton
52  public class DefaultInheritanceAssembler
53      implements InheritanceAssembler
54  {
55  
56      private InheritanceModelMerger merger = new InheritanceModelMerger();
57  
58      private static final String CHILD_DIRECTORY = "child-directory";
59  
60      private static final String CHILD_DIRECTORY_PROPERTY = "project.directory";
61  
62      @Override
63      public void assembleModelInheritance( Model child, Model parent, ModelBuildingRequest request,
64                                            ModelProblemCollector problems )
65      {
66          Map<Object, Object> hints = new HashMap<>();
67          String childPath = child.getProperties().getProperty( CHILD_DIRECTORY_PROPERTY, child.getArtifactId() );
68          hints.put( CHILD_DIRECTORY, childPath );
69          hints.put( MavenModelMerger.CHILD_PATH_ADJUSTMENT, getChildPathAdjustment( child, parent, childPath ) );
70          merger.merge( child, parent, false, hints );
71      }
72  
73      
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92      private String getChildPathAdjustment( Model child, Model parent, String childDirectory )
93      {
94          String adjustment = "";
95  
96          if ( parent != null )
97          {
98              String childName = child.getArtifactId();
99  
100             
101 
102 
103 
104 
105 
106 
107             if ( child.getProjectDirectory() != null )
108             {
109                 childName = child.getProjectDirectory().getName();
110             }
111 
112             for ( String module : parent.getModules() )
113             {
114                 module = module.replace( '\\', '/' );
115 
116                 if ( module.regionMatches( true, module.length() - 4, ".xml", 0, 4 ) )
117                 {
118                     module = module.substring( 0, module.lastIndexOf( '/' ) + 1 );
119                 }
120 
121                 String moduleName = module;
122                 if ( moduleName.endsWith( "/" ) )
123                 {
124                     moduleName = moduleName.substring( 0, moduleName.length() - 1 );
125                 }
126 
127                 int lastSlash = moduleName.lastIndexOf( '/' );
128 
129                 moduleName = moduleName.substring( lastSlash + 1 );
130 
131                 if ( ( moduleName.equals( childName ) || ( moduleName.equals( childDirectory ) ) ) && lastSlash >= 0 )
132                 {
133                     adjustment = module.substring( 0, lastSlash );
134                     break;
135                 }
136             }
137         }
138 
139         return adjustment;
140     }
141 
142     
143 
144 
145     protected static class InheritanceModelMerger
146         extends MavenModelMerger
147     {
148 
149         @Override
150         protected String extrapolateChildUrl( String parentUrl, boolean appendPath, Map<Object, Object> context )
151         {
152             Object childDirectory = context.get( CHILD_DIRECTORY );
153             Object childPathAdjustment = context.get( CHILD_PATH_ADJUSTMENT );
154 
155             if ( StringUtils.isBlank( parentUrl ) || childDirectory == null || childPathAdjustment == null
156                 || !appendPath )
157             {
158                 return parentUrl;
159             }
160 
161             
162             return appendPath( parentUrl, childDirectory.toString(), childPathAdjustment.toString() );
163         }
164 
165         private String appendPath( String parentUrl, String childPath, String pathAdjustment )
166         {
167             StringBuilder url = new StringBuilder( parentUrl.length() + pathAdjustment.length() + childPath.length()
168                 + ( ( pathAdjustment.length() == 0 ) ? 1 : 2 ) );
169 
170             url.append( parentUrl );
171             concatPath( url, pathAdjustment );
172             concatPath( url, childPath );
173 
174             return url.toString();
175         }
176 
177         private void concatPath( StringBuilder url, String path )
178         {
179             if ( path.length() > 0 )
180             {
181                 boolean initialUrlEndsWithSlash = url.charAt( url.length() - 1 ) == '/';
182                 boolean pathStartsWithSlash = path.charAt( 0 ) ==  '/';
183 
184                 if ( pathStartsWithSlash )
185                 {
186                     if ( initialUrlEndsWithSlash )
187                     {
188                         
189                         url.setLength( url.length() - 1 );
190                     }
191                 }
192                 else if ( !initialUrlEndsWithSlash )
193                 {
194                     
195                     url.append( '/' );
196                 }
197 
198                 url.append( path );
199 
200                 
201                 if ( initialUrlEndsWithSlash && !path.endsWith( "/" ) )
202                 {
203                     url.append( '/' );
204                 }
205             }
206         }
207 
208         @Override
209         protected void mergeModelBase_Properties( ModelBase target, ModelBase source, boolean sourceDominant,
210                                                   Map<Object, Object> context )
211         {
212             Properties merged = new Properties();
213             if ( sourceDominant )
214             {
215                 merged.putAll( target.getProperties() );
216                 putAll( merged, source.getProperties(), CHILD_DIRECTORY_PROPERTY );
217             }
218             else
219             {
220                 putAll( merged, source.getProperties(), CHILD_DIRECTORY_PROPERTY );
221                 merged.putAll( target.getProperties() );
222             }
223             target.setProperties( merged );
224             target.setLocation( "properties",
225                                 InputLocation.merge( target.getLocation( "properties" ),
226                                                      source.getLocation( "properties" ), sourceDominant ) );
227         }
228 
229         private void putAll( Map<Object, Object> s, Map<Object, Object> t, Object excludeKey )
230         {
231             for ( Map.Entry<Object, Object> e : t.entrySet() )
232             {
233                 if ( !e.getKey().equals( excludeKey ) )
234                 {
235                     s.put( e.getKey(), e.getValue() );
236                 }
237             }
238         }
239 
240         @Override
241         protected void mergePluginContainer_Plugins( PluginContainer target, PluginContainer source,
242                                                      boolean sourceDominant, Map<Object, Object> context )
243         {
244             List<Plugin> src = source.getPlugins();
245             if ( !src.isEmpty() )
246             {
247                 List<Plugin> tgt = target.getPlugins();
248                 Map<Object, Plugin> master = new LinkedHashMap<>( src.size() * 2 );
249 
250                 for ( Plugin element : src )
251                 {
252                     if ( element.isInherited() || !element.getExecutions().isEmpty() )
253                     {
254                         
255                         Plugin plugin = new Plugin();
256                         plugin.setLocation( "", element.getLocation( "" ) );
257                         plugin.setGroupId( null );
258                         mergePlugin( plugin, element, sourceDominant, context );
259 
260                         Object key = getPluginKey( element );
261 
262                         master.put( key, plugin );
263                     }
264                 }
265 
266                 Map<Object, List<Plugin>> predecessors = new LinkedHashMap<>();
267                 List<Plugin> pending = new ArrayList<>();
268                 for ( Plugin element : tgt )
269                 {
270                     Object key = getPluginKey( element );
271                     Plugin existing = master.get( key );
272                     if ( existing != null )
273                     {
274                         mergePlugin( element, existing, sourceDominant, context );
275 
276                         master.put( key, element );
277 
278                         if ( !pending.isEmpty() )
279                         {
280                             predecessors.put( key, pending );
281                             pending = new ArrayList<>();
282                         }
283                     }
284                     else
285                     {
286                         pending.add( element );
287                     }
288                 }
289 
290                 List<Plugin> result = new ArrayList<>( src.size() + tgt.size() );
291                 for ( Map.Entry<Object, Plugin> entry : master.entrySet() )
292                 {
293                     List<Plugin> pre = predecessors.get( entry.getKey() );
294                     if ( pre != null )
295                     {
296                         result.addAll( pre );
297                     }
298                     result.add( entry.getValue() );
299                 }
300                 result.addAll( pending );
301 
302                 target.setPlugins( result );
303             }
304         }
305 
306         @Override
307         protected void mergePlugin( Plugin target, Plugin source, boolean sourceDominant, Map<Object, Object> context )
308         {
309             if ( source.isInherited() )
310             {
311                 mergeConfigurationContainer( target, source, sourceDominant, context );
312             }
313             mergePlugin_GroupId( target, source, sourceDominant, context );
314             mergePlugin_ArtifactId( target, source, sourceDominant, context );
315             mergePlugin_Version( target, source, sourceDominant, context );
316             mergePlugin_Extensions( target, source, sourceDominant, context );
317             mergePlugin_Dependencies( target, source, sourceDominant, context );
318             mergePlugin_Executions( target, source, sourceDominant, context );
319         }
320 
321         @Override
322         protected void mergeReporting_Plugins( Reporting target, Reporting source, boolean sourceDominant,
323                                                Map<Object, Object> context )
324         {
325             List<ReportPlugin> src = source.getPlugins();
326             if ( !src.isEmpty() )
327             {
328                 List<ReportPlugin> tgt = target.getPlugins();
329                 Map<Object, ReportPlugin> merged =
330                     new LinkedHashMap<>( ( src.size() + tgt.size() ) * 2 );
331 
332                 for ( ReportPlugin element :  src )
333                 {
334                     Object key = getReportPluginKey( element );
335                     if ( element.isInherited() )
336                     {
337                         
338                         ReportPlugin plugin = new ReportPlugin();
339                         plugin.setLocation( "", element.getLocation( "" ) );
340                         plugin.setGroupId( null );
341                         mergeReportPlugin( plugin, element, sourceDominant, context );
342 
343                         merged.put( key, plugin );
344                     }
345                 }
346 
347                 for ( ReportPlugin element : tgt )
348                 {
349                     Object key = getReportPluginKey( element );
350                     ReportPlugin existing = merged.get( key );
351                     if ( existing != null )
352                     {
353                         mergeReportPlugin( element, existing, sourceDominant, context );
354                     }
355                     merged.put( key, element );
356                 }
357 
358                 target.setPlugins( new ArrayList<>( merged.values() ) );
359             }
360         }
361     }
362 
363 }