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 java.util.ArrayList;
22  import java.util.Collections;
23  import java.util.HashMap;
24  import java.util.List;
25  import java.util.Map;
26  import javax.inject.Named;
27  import javax.inject.Singleton;
28  import org.apache.maven.api.model.Dependency;
29  import org.apache.maven.api.model.DependencyManagement;
30  import org.apache.maven.api.model.Exclusion;
31  import org.apache.maven.api.model.Model;
32  import org.apache.maven.model.building.ModelBuildingRequest;
33  import org.apache.maven.model.building.ModelProblemCollector;
34  import org.apache.maven.model.merge.MavenModelMerger;
35  
36  /**
37   * Handles injection of dependency management into the model.
38   *
39   * @author Benjamin Bentmann
40   */
41  @SuppressWarnings({"checkstyle:methodname"})
42  @Named
43  @Singleton
44  public class DefaultDependencyManagementInjector implements DependencyManagementInjector {
45  
46      private ManagementModelMerger merger = new ManagementModelMerger();
47  
48      @Override
49      public void injectManagement(
50              org.apache.maven.model.Model model, ModelBuildingRequest request, ModelProblemCollector problems) {
51          model.update(merger.mergeManagedDependencies(model.getDelegate()));
52      }
53  
54      /**
55       * ManagementModelMerger
56       */
57      protected static class ManagementModelMerger extends MavenModelMerger {
58  
59          public Model 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().apply(dependency);
67                      dependencies.put(key, dependency);
68                  }
69  
70                  boolean modified = false;
71                  for (Dependency managedDependency : dependencyManagement.getDependencies()) {
72                      Object key = getDependencyKey().apply(managedDependency);
73                      Dependency dependency = dependencies.get(key);
74                      if (dependency != null) {
75                          Dependency merged = mergeDependency(dependency, managedDependency, false, context);
76                          if (merged != dependency) {
77                              dependencies.put(key, merged);
78                              modified = true;
79                          }
80                      }
81                  }
82  
83                  if (modified) {
84                      List<Dependency> newDeps = new ArrayList<>(dependencies.size());
85                      for (Dependency dep : model.getDependencies()) {
86                          Object key = getDependencyKey().apply(dep);
87                          Dependency dependency = dependencies.get(key);
88                          newDeps.add(dependency);
89                      }
90                      return Model.newBuilder(model).dependencies(newDeps).build();
91                  }
92              }
93              return model;
94          }
95  
96          @Override
97          protected void mergeDependency_Optional(
98                  Dependency.Builder builder,
99                  Dependency target,
100                 Dependency source,
101                 boolean sourceDominant,
102                 Map<Object, Object> context) {
103             // optional flag is not managed
104         }
105 
106         @Override
107         protected void mergeDependency_Exclusions(
108                 Dependency.Builder builder,
109                 Dependency target,
110                 Dependency source,
111                 boolean sourceDominant,
112                 Map<Object, Object> context) {
113             List<Exclusion> tgt = target.getExclusions();
114             if (tgt.isEmpty()) {
115                 List<Exclusion> src = source.getExclusions();
116                 builder.exclusions(src);
117             }
118         }
119     }
120 }