View Javadoc

1   package org.apache.maven.model.inheritance;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.util.ArrayList;
24  import java.util.HashMap;
25  import java.util.LinkedHashMap;
26  import java.util.List;
27  import java.util.Map;
28  
29  import org.apache.maven.model.Model;
30  import org.apache.maven.model.Plugin;
31  import org.apache.maven.model.PluginContainer;
32  import org.apache.maven.model.ReportPlugin;
33  import org.apache.maven.model.Reporting;
34  import org.apache.maven.model.building.ModelBuildingRequest;
35  import org.apache.maven.model.building.ModelProblemCollector;
36  import org.apache.maven.model.merge.MavenModelMerger;
37  import org.codehaus.plexus.component.annotations.Component;
38  
39  /**
40   * Handles inheritance of model values.
41   *
42   * @author Benjamin Bentmann
43   */
44  @Component( role = InheritanceAssembler.class )
45  public class DefaultInheritanceAssembler
46      implements InheritanceAssembler
47  {
48  
49      private InheritanceModelMerger merger = new InheritanceModelMerger();
50  
51      public void assembleModelInheritance( Model child, Model parent, ModelBuildingRequest request,
52                                            ModelProblemCollector problems )
53      {
54          Map<Object, Object> hints = new HashMap<Object, Object>();
55          hints.put( MavenModelMerger.CHILD_PATH_ADJUSTMENT, getChildPathAdjustment( child, parent ) );
56          merger.merge( child, parent, false, hints );
57      }
58  
59      /**
60       * Calculates the relative path from the base directory of the parent to the parent directory of the base directory
61       * of the child. The general idea is to adjust inherited URLs to match the project layout (in SCM). This calculation
62       * is only a heuristic based on our conventions. In detail, the algo relies on the following assumptions. The parent
63       * uses aggregation and refers to the child via the modules section. The module path to the child is considered to
64       * point at the POM rather than its base directory if the path ends with ".xml" (ignoring case). The name of the
65       * child's base directory matches the artifact id of the child. Note that for the sake of independence from the user
66       * environment, the filesystem is intentionally not used for the calculation.
67       *
68       * @param child The child model, must not be <code>null</code>.
69       * @param parent The parent model, may be <code>null</code>.
70       * @return The path adjustment, can be empty but never <code>null</code>.
71       */
72      private String getChildPathAdjustment( Model child, Model parent )
73      {
74          String adjustment = "";
75  
76          if ( parent != null )
77          {
78              String childName = child.getArtifactId();
79  
80              /*
81               * This logic exists only for the sake of backward-compat with 2.x (MNG-5000). In generally, it is wrong to
82               * base URL inheritance on the project directory names as this information is unavailable for POMs in the
83               * repository. In other words, projects where artifactId != projectDirName will see different effective URLs
84               * depending on how the POM was constructed.
85               */
86              File childDirectory = child.getProjectDirectory();
87              if ( childDirectory != null )
88              {
89                  childName = childDirectory.getName();
90              }
91  
92              for ( String module : parent.getModules() )
93              {
94                  module = module.replace( '\\', '/' );
95  
96                  if ( module.regionMatches( true, module.length() - 4, ".xml", 0, 4 ) )
97                  {
98                      module = module.substring( 0, module.lastIndexOf( '/' ) + 1 );
99                  }
100 
101                 String moduleName = module;
102                 if ( moduleName.endsWith( "/" ) )
103                 {
104                     moduleName = moduleName.substring( 0, moduleName.length() - 1 );
105                 }
106 
107                 int lastSlash = moduleName.lastIndexOf( '/' );
108 
109                 moduleName = moduleName.substring( lastSlash + 1 );
110 
111                 if ( moduleName.equals( childName ) && lastSlash >= 0 )
112                 {
113                     adjustment = module.substring( 0, lastSlash );
114                     break;
115                 }
116             }
117         }
118 
119         return adjustment;
120     }
121 
122     private static class InheritanceModelMerger
123         extends MavenModelMerger
124     {
125 
126         @Override
127         protected void mergePluginContainer_Plugins( PluginContainer target, PluginContainer source,
128                                                      boolean sourceDominant, Map<Object, Object> context )
129         {
130             List<Plugin> src = source.getPlugins();
131             if ( !src.isEmpty() )
132             {
133                 List<Plugin> tgt = target.getPlugins();
134                 Map<Object, Plugin> master = new LinkedHashMap<Object, Plugin>( src.size() * 2 );
135 
136                 for ( Plugin element : src )
137                 {
138                     if ( element.isInherited() || !element.getExecutions().isEmpty() )
139                     {
140                         // NOTE: Enforce recursive merge to trigger merging/inheritance logic for executions
141                         Plugin plugin = new Plugin();
142                         plugin.setLocation( "", element.getLocation( "" ) );
143                         plugin.setGroupId( null );
144                         mergePlugin( plugin, element, sourceDominant, context );
145 
146                         Object key = getPluginKey( element );
147 
148                         master.put( key, plugin );
149                     }
150                 }
151 
152                 Map<Object, List<Plugin>> predecessors = new LinkedHashMap<Object, List<Plugin>>();
153                 List<Plugin> pending = new ArrayList<Plugin>();
154                 for ( Plugin element : tgt )
155                 {
156                     Object key = getPluginKey( element );
157                     Plugin existing = master.get( key );
158                     if ( existing != null )
159                     {
160                         mergePlugin( element, existing, sourceDominant, context );
161 
162                         master.put( key, element );
163 
164                         if ( !pending.isEmpty() )
165                         {
166                             predecessors.put( key, pending );
167                             pending = new ArrayList<Plugin>();
168                         }
169                     }
170                     else
171                     {
172                         pending.add( element );
173                     }
174                 }
175 
176                 List<Plugin> result = new ArrayList<Plugin>( src.size() + tgt.size() );
177                 for ( Map.Entry<Object, Plugin> entry : master.entrySet() )
178                 {
179                     List<Plugin> pre = predecessors.get( entry.getKey() );
180                     if ( pre != null )
181                     {
182                         result.addAll( pre );
183                     }
184                     result.add( entry.getValue() );
185                 }
186                 result.addAll( pending );
187 
188                 target.setPlugins( result );
189             }
190         }
191 
192         @Override
193         protected void mergePlugin( Plugin target, Plugin source, boolean sourceDominant, Map<Object, Object> context )
194         {
195             if ( source.isInherited() )
196             {
197                 mergeConfigurationContainer( target, source, sourceDominant, context );
198             }
199             mergePlugin_GroupId( target, source, sourceDominant, context );
200             mergePlugin_ArtifactId( target, source, sourceDominant, context );
201             mergePlugin_Version( target, source, sourceDominant, context );
202             mergePlugin_Extensions( target, source, sourceDominant, context );
203             mergePlugin_Dependencies( target, source, sourceDominant, context );
204             mergePlugin_Executions( target, source, sourceDominant, context );
205         }
206 
207         @Override
208         protected void mergeReporting_Plugins( Reporting target, Reporting source, boolean sourceDominant,
209                                                Map<Object, Object> context )
210         {
211             List<ReportPlugin> src = source.getPlugins();
212             if ( !src.isEmpty() )
213             {
214                 List<ReportPlugin> tgt = target.getPlugins();
215                 Map<Object, ReportPlugin> merged =
216                     new LinkedHashMap<Object, ReportPlugin>( ( src.size() + tgt.size() ) * 2 );
217 
218                 for ( ReportPlugin element :  src )
219                 {
220                     Object key = getReportPluginKey( element );
221                     if ( element.isInherited() )
222                     {
223                         // NOTE: Enforce recursive merge to trigger merging/inheritance logic for executions as well
224                         ReportPlugin plugin = new ReportPlugin();
225                         plugin.setLocation( "", element.getLocation( "" ) );
226                         plugin.setGroupId( null );
227                         mergeReportPlugin( plugin, element, sourceDominant, context );
228 
229                         merged.put( key, plugin );
230                     }
231                 }
232 
233                 for ( ReportPlugin element : tgt )
234                 {
235                     Object key = getReportPluginKey( element );
236                     ReportPlugin existing = merged.get( key );
237                     if ( existing != null )
238                     {
239                         mergeReportPlugin( element, existing, sourceDominant, context );
240                     }
241                     merged.put( key, element );
242                 }
243 
244                 target.setPlugins( new ArrayList<ReportPlugin>( merged.values() ) );
245             }
246         }
247     }
248 
249 }