View Javadoc
1   package org.apache.maven.model.normalization;
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.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   * Handles normalization of a model.
43   *
44   * @author Benjamin Bentmann
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           * NOTE: This is primarily to keep backward-compat with Maven 2.x which did not validate that dependencies are
86           * unique within a single POM. Upon multiple declarations, 2.x just kept the last one but retained the order of
87           * the first occurrence. So when we're in lenient/compat mode, we have to deal with such broken POMs and mimic
88           * the way 2.x works. When we're in strict mode, the removal of duplicates just saves other merging steps from
89           * aftereffects and bogus error messages.
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      * DuplicateMerger
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         // we cannot set this directly in the MDO due to the interactions with dependency management
149         return StringUtils.isEmpty( d.getScope() ) ? Dependency.newBuilder( d ).scope( "compile" ).build() : d;
150     }
151 
152     /**
153      * Returns a list suited for the builders, i.e. null if not modified
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 }