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.management;
20  
21  import javax.inject.Named;
22  import javax.inject.Singleton;
23  
24  import java.util.Collections;
25  import java.util.HashMap;
26  import java.util.List;
27  import java.util.Map;
28  
29  import org.apache.maven.model.Dependency;
30  import org.apache.maven.model.DependencyManagement;
31  import org.apache.maven.model.Exclusion;
32  import org.apache.maven.model.Model;
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  
37  /**
38   * Handles injection of dependency management into the model.
39   *
40   * @author Benjamin Bentmann
41   */
42  @SuppressWarnings({"checkstyle:methodname"})
43  @Named
44  @Singleton
45  public class DefaultDependencyManagementInjector implements DependencyManagementInjector {
46  
47      private ManagementModelMerger merger = new ManagementModelMerger();
48  
49      @Override
50      public void injectManagement(Model model, ModelBuildingRequest request, ModelProblemCollector problems) {
51          merger.mergeManagedDependencies(model);
52      }
53  
54      /**
55       * ManagementModelMerger
56       */
57      protected static class ManagementModelMerger extends MavenModelMerger {
58  
59          public void mergeManagedDependencies(Model model) {
60              DependencyManagement dependencyManagement = model.getDependencyManagement();
61              if (dependencyManagement != null) {
62                  Map<Object, Dependency> dependencies = new HashMap<>();
63                  Map<Object, Object> context = Collections.emptyMap();
64  
65                  for (Dependency dependency : model.getDependencies()) {
66                      Object key = getDependencyKey(dependency);
67                      dependencies.put(key, dependency);
68                  }
69  
70                  for (Dependency managedDependency : dependencyManagement.getDependencies()) {
71                      Object key = getDependencyKey(managedDependency);
72                      Dependency dependency = dependencies.get(key);
73                      if (dependency != null) {
74                          mergeDependency(dependency, managedDependency, false, context);
75                      }
76                  }
77              }
78          }
79  
80          @Override
81          protected void mergeDependency_Optional(
82                  Dependency target, Dependency source, boolean sourceDominant, Map<Object, Object> context) {
83              // optional flag is not managed
84          }
85  
86          @Override
87          protected void mergeDependency_Exclusions(
88                  Dependency target, Dependency source, boolean sourceDominant, Map<Object, Object> context) {
89              List<Exclusion> tgt = target.getExclusions();
90              if (tgt.isEmpty()) {
91                  List<Exclusion> src = source.getExclusions();
92  
93                  for (Exclusion element : src) {
94                      Exclusion clone = element.clone();
95                      target.addExclusion(clone);
96                  }
97              }
98          }
99      }
100 }