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   * @deprecated use {@code org.apache.maven.api.services.ModelBuilder} instead
43   */
44  @Named
45  @Singleton
46  @Deprecated(since = "4.0.0")
47  public class DefaultModelNormalizer implements ModelNormalizer {
48  
49      private DuplicateMerger merger = new DuplicateMerger();
50  
51      @Override
52      public void mergeDuplicates(Model model, ModelBuildingRequest request, ModelProblemCollector problems) {
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                      merger.mergePlugin(plugin, first);
63                  }
64                  normalized.put(key, plugin);
65              }
66  
67              if (plugins.size() != normalized.size()) {
68                  build.setPlugins(new ArrayList<>(normalized.values()));
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              model.setDependencies(new ArrayList<>(normalized.values()));
88          }
89      }
90  
91      /**
92       * DuplicateMerger
93       */
94      protected static class DuplicateMerger extends MavenModelMerger {
95  
96          public void mergePlugin(Plugin target, Plugin source) {
97              super.mergePlugin(target, source, false, Collections.emptyMap());
98          }
99      }
100 
101     @Override
102     public void injectDefaultValues(Model model, ModelBuildingRequest request, ModelProblemCollector problems) {
103         injectDependencyDefaults(model.getDependencies());
104 
105         Build build = model.getBuild();
106         if (build != null) {
107             for (Plugin plugin : build.getPlugins()) {
108                 injectDependencyDefaults(plugin.getDependencies());
109             }
110         }
111     }
112 
113     private void injectDependencyDefaults(List<Dependency> dependencies) {
114         for (Dependency dependency : dependencies) {
115             if (StringUtils.isEmpty(dependency.getScope())) {
116                 // we cannot set this directly in the MDO due to the interactions with dependency management
117                 dependency.setScope("compile");
118             }
119         }
120     }
121 }