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  
30  import org.apache.maven.model.Build;
31  import org.apache.maven.model.Dependency;
32  import org.apache.maven.model.Model;
33  import org.apache.maven.model.Plugin;
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.util.StringUtils;
38  
39  /**
40   * Handles normalization of a model.
41   *
42   * @author Benjamin Bentmann
43   */
44  @Named
45  @Singleton
46  public class DefaultModelNormalizer implements ModelNormalizer {
47  
48      private DuplicateMerger merger = new DuplicateMerger();
49  
50      @Override
51      public void mergeDuplicates(Model model, ModelBuildingRequest request, ModelProblemCollector problems) {
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                      merger.mergePlugin(plugin, first);
62                  }
63                  normalized.put(key, plugin);
64              }
65  
66              if (plugins.size() != normalized.size()) {
67                  build.setPlugins(new ArrayList<>(normalized.values()));
68              }
69          }
70  
71          /*
72           * NOTE: This is primarily to keep backward-compat with Maven 2.x which did not validate that dependencies are
73           * unique within a single POM. Upon multiple declarations, 2.x just kept the last one but retained the order of
74           * the first occurrence. So when we're in lenient/compat mode, we have to deal with such broken POMs and mimic
75           * the way 2.x works. When we're in strict mode, the removal of duplicates just saves other merging steps from
76           * aftereffects and bogus error messages.
77           */
78          List<Dependency> dependencies = model.getDependencies();
79          Map<String, Dependency> normalized = new LinkedHashMap<>(dependencies.size() * 2);
80  
81          for (Dependency dependency : dependencies) {
82              normalized.put(dependency.getManagementKey(), dependency);
83          }
84  
85          if (dependencies.size() != normalized.size()) {
86              model.setDependencies(new ArrayList<>(normalized.values()));
87          }
88      }
89  
90      /**
91       * DuplicateMerger
92       */
93      protected static class DuplicateMerger extends MavenModelMerger {
94  
95          public void mergePlugin(Plugin target, Plugin source) {
96              super.mergePlugin(target, source, false, Collections.emptyMap());
97          }
98      }
99  
100     @Override
101     public void injectDefaultValues(Model model, ModelBuildingRequest request, ModelProblemCollector problems) {
102         injectDependencyDefaults(model.getDependencies());
103 
104         Build build = model.getBuild();
105         if (build != null) {
106             for (Plugin plugin : build.getPlugins()) {
107                 injectDependencyDefaults(plugin.getDependencies());
108             }
109         }
110     }
111 
112     private void injectDependencyDefaults(List<Dependency> dependencies) {
113         for (Dependency dependency : dependencies) {
114             if (StringUtils.isEmpty(dependency.getScope())) {
115                 // we cannot set this directly in the MDO due to the interactions with dependency management
116                 dependency.setScope("compile");
117             }
118         }
119     }
120 }