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