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 javax.inject.Named;
22  import javax.inject.Singleton;
23  
24  import java.util.ArrayList;
25  import java.util.Collections;
26  import java.util.LinkedHashMap;
27  import java.util.List;
28  import java.util.Map;
29  import java.util.function.Function;
30  
31  import org.apache.maven.api.model.Build;
32  import org.apache.maven.api.model.Dependency;
33  import org.apache.maven.api.model.Model;
34  import org.apache.maven.api.model.Plugin;
35  import org.apache.maven.model.building.ModelBuildingRequest;
36  import org.apache.maven.model.building.ModelProblemCollector;
37  import org.apache.maven.model.merge.MavenModelMerger;
38  
39  /**
40   * Handles normalization of a model.
41   *
42   */
43  @Named
44  @Singleton
45  public class DefaultModelNormalizer implements ModelNormalizer {
46  
47      private DuplicateMerger merger = new DuplicateMerger();
48  
49      @Override
50      public void mergeDuplicates(
51              org.apache.maven.model.Model model, ModelBuildingRequest request, ModelProblemCollector problems) {
52          model.update(mergeDuplicates(model.getDelegate(), request, problems));
53      }
54  
55      @Override
56      public void injectDefaultValues(
57              org.apache.maven.model.Model model, ModelBuildingRequest request, ModelProblemCollector problems) {
58          model.update(injectDefaultValues(model.getDelegate(), request, problems));
59      }
60  
61      @Override
62      public Model mergeDuplicates(Model model, ModelBuildingRequest request, ModelProblemCollector problems) {
63          Model.Builder builder = Model.newBuilder(model);
64  
65          Build build = model.getBuild();
66          if (build != null) {
67              List<Plugin> plugins = build.getPlugins();
68              Map<Object, Plugin> normalized = new LinkedHashMap<>(plugins.size() * 2);
69  
70              for (Plugin plugin : plugins) {
71                  Object key = plugin.getKey();
72                  Plugin first = normalized.get(key);
73                  if (first != null) {
74                      plugin = merger.mergePlugin(plugin, first);
75                  }
76                  normalized.put(key, plugin);
77              }
78  
79              if (plugins.size() != normalized.size()) {
80                  builder.build(
81                          Build.newBuilder(build).plugins(normalized.values()).build());
82              }
83          }
84  
85          /*
86           * NOTE: This is primarily to keep backward-compat with Maven 2.x which did not validate that dependencies are
87           * unique within a single POM. Upon multiple declarations, 2.x just kept the last one but retained the order of
88           * the first occurrence. So when we're in lenient/compat mode, we have to deal with such broken POMs and mimic
89           * the way 2.x works. When we're in strict mode, the removal of duplicates just saves other merging steps from
90           * aftereffects and bogus error messages.
91           */
92          List<Dependency> dependencies = model.getDependencies();
93          Map<String, Dependency> normalized = new LinkedHashMap<>(dependencies.size() * 2);
94  
95          for (Dependency dependency : dependencies) {
96              normalized.put(dependency.getManagementKey(), dependency);
97          }
98  
99          if (dependencies.size() != normalized.size()) {
100             builder.dependencies(normalized.values());
101         }
102 
103         return builder.build();
104     }
105 
106     /**
107      * DuplicateMerger
108      */
109     protected static class DuplicateMerger extends MavenModelMerger {
110 
111         public Plugin mergePlugin(Plugin target, Plugin source) {
112             return super.mergePlugin(target, source, false, Collections.emptyMap());
113         }
114     }
115 
116     @Override
117     public Model injectDefaultValues(Model model, ModelBuildingRequest request, ModelProblemCollector problems) {
118         Model.Builder builder = Model.newBuilder(model);
119 
120         builder.dependencies(injectList(model.getDependencies(), this::injectDependency));
121         Build build = model.getBuild();
122         if (build != null) {
123             Build newBuild = Build.newBuilder(build)
124                     .plugins(injectList(build.getPlugins(), this::injectPlugin))
125                     .build();
126             builder.build(newBuild != build ? newBuild : null);
127         }
128 
129         return builder.build();
130     }
131 
132     private Plugin injectPlugin(Plugin p) {
133         return Plugin.newBuilder(p)
134                 .dependencies(injectList(p.getDependencies(), this::injectDependency))
135                 .build();
136     }
137 
138     private Dependency injectDependency(Dependency d) {
139         // we cannot set this directly in the MDO due to the interactions with dependency management
140         return (d.getScope() == null || d.getScope().isEmpty()) ? d.withScope("compile") : d;
141     }
142 
143     /**
144      * Returns a list suited for the builders, i.e. null if not modified
145      */
146     private <T> List<T> injectList(List<T> list, Function<T, T> modifer) {
147         List<T> newList = null;
148         for (int i = 0; i < list.size(); i++) {
149             T oldT = list.get(i);
150             T newT = modifer.apply(oldT);
151             if (newT != oldT) {
152                 if (newList == null) {
153                     newList = new ArrayList<>(list);
154                 }
155                 newList.set(i, newT);
156             }
157         }
158         return newList;
159     }
160 }