View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.model.normalization;
20  
21  import java.util.ArrayList;
22  import java.util.Collections;
23  import java.util.LinkedHashMap;
24  import java.util.List;
25  import java.util.Map;
26  import java.util.function.Function;
27  import javax.inject.Named;
28  import javax.inject.Singleton;
29  import org.apache.maven.api.model.Build;
30  import org.apache.maven.api.model.Dependency;
31  import org.apache.maven.api.model.Model;
32  import org.apache.maven.api.model.Plugin;
33  import org.apache.maven.model.building.ModelBuildingRequest;
34  import org.apache.maven.model.building.ModelProblemCollector;
35  import org.apache.maven.model.merge.MavenModelMerger;
36  import org.codehaus.plexus.util.StringUtils;
37  
38  /**
39   * Handles normalization of a model.
40   *
41   * @author Benjamin Bentmann
42   */
43  @Named
44  @Singleton
45  public class DefaultModelNormalizer implements ModelNormalizer {
46  
47      private DuplicateMerger merger = new DuplicateMerger();
48  
49      @Override
50      public Model mergeDuplicates(Model model, ModelBuildingRequest request, ModelProblemCollector problems) {
51          Model.Builder builder = Model.newBuilder(model);
52  
53          Build build = model.getBuild();
54          if (build != null) {
55              List<Plugin> plugins = build.getPlugins();
56              Map<Object, Plugin> normalized = new LinkedHashMap<>(plugins.size() * 2);
57  
58              for (Plugin plugin : plugins) {
59                  Object key = plugin.getKey();
60                  Plugin first = normalized.get(key);
61                  if (first != null) {
62                      plugin = merger.mergePlugin(plugin, first);
63                  }
64                  normalized.put(key, plugin);
65              }
66  
67              if (plugins.size() != normalized.size()) {
68                  builder.build(
69                          Build.newBuilder(build).plugins(normalized.values()).build());
70              }
71          }
72  
73          /*
74           * NOTE: This is primarily to keep backward-compat with Maven 2.x which did not validate that dependencies are
75           * unique within a single POM. Upon multiple declarations, 2.x just kept the last one but retained the order of
76           * the first occurrence. So when we're in lenient/compat mode, we have to deal with such broken POMs and mimic
77           * the way 2.x works. When we're in strict mode, the removal of duplicates just saves other merging steps from
78           * aftereffects and bogus error messages.
79           */
80          List<Dependency> dependencies = model.getDependencies();
81          Map<String, Dependency> normalized = new LinkedHashMap<>(dependencies.size() * 2);
82  
83          for (Dependency dependency : dependencies) {
84              normalized.put(dependency.getManagementKey(), dependency);
85          }
86  
87          if (dependencies.size() != normalized.size()) {
88              builder.dependencies(normalized.values());
89          }
90  
91          return builder.build();
92      }
93  
94      /**
95       * DuplicateMerger
96       */
97      protected static class DuplicateMerger extends MavenModelMerger {
98  
99          public Plugin mergePlugin(Plugin target, Plugin source) {
100             return super.mergePlugin(target, source, false, Collections.emptyMap());
101         }
102     }
103 
104     @Override
105     public Model injectDefaultValues(Model model, ModelBuildingRequest request, ModelProblemCollector problems) {
106         Model.Builder builder = Model.newBuilder(model);
107 
108         builder.dependencies(injectList(model.getDependencies(), this::injectDependency));
109         Build build = model.getBuild();
110         if (build != null) {
111             Build newBuild = Build.newBuilder(build)
112                     .plugins(injectList(build.getPlugins(), this::injectPlugin))
113                     .build();
114             builder.build(newBuild != build ? newBuild : null);
115         }
116 
117         return builder.build();
118     }
119 
120     private Plugin injectPlugin(Plugin p) {
121         return Plugin.newBuilder(p)
122                 .dependencies(injectList(p.getDependencies(), this::injectDependency))
123                 .build();
124     }
125 
126     private Dependency injectDependency(Dependency d) {
127         // we cannot set this directly in the MDO due to the interactions with dependency management
128         return StringUtils.isEmpty(d.getScope())
129                 ? Dependency.newBuilder(d).scope("compile").build()
130                 : d;
131     }
132 
133     /**
134      * Returns a list suited for the builders, i.e. null if not modified
135      */
136     private <T> List<T> injectList(List<T> list, Function<T, T> modifer) {
137         List<T> newList = null;
138         for (int i = 0; i < list.size(); i++) {
139             T oldT = list.get(i);
140             T newT = modifer.apply(oldT);
141             if (newT != oldT) {
142                 if (newList == null) {
143                     newList = new ArrayList<>(list);
144                 }
145                 newList.set(i, newT);
146             }
147         }
148         return newList;
149     }
150 }