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.composition;
20  
21  import javax.inject.Named;
22  import javax.inject.Singleton;
23  
24  import java.util.ArrayList;
25  import java.util.LinkedHashMap;
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.Model;
32  import org.apache.maven.model.building.ModelBuildingRequest;
33  import org.apache.maven.model.building.ModelProblemCollector;
34  
35  /**
36   * Handles the import of dependency management from other models into the target model.
37   *
38   * @author Benjamin Bentmann
39   */
40  @Named
41  @Singleton
42  public class DefaultDependencyManagementImporter implements DependencyManagementImporter {
43  
44      @Override
45      public void importManagement(
46              Model target,
47              List<? extends DependencyManagement> sources,
48              ModelBuildingRequest request,
49              ModelProblemCollector problems) {
50          if (sources != null && !sources.isEmpty()) {
51              Map<String, Dependency> dependencies = new LinkedHashMap<>();
52  
53              DependencyManagement depMgmt = target.getDependencyManagement();
54  
55              if (depMgmt != null) {
56                  for (Dependency dependency : depMgmt.getDependencies()) {
57                      dependencies.put(dependency.getManagementKey(), dependency);
58                  }
59              } else {
60                  depMgmt = new DependencyManagement();
61                  target.setDependencyManagement(depMgmt);
62              }
63  
64              for (DependencyManagement source : sources) {
65                  for (Dependency dependency : source.getDependencies()) {
66                      String key = dependency.getManagementKey();
67                      if (!dependencies.containsKey(key)) {
68                          dependencies.put(key, dependency);
69                      }
70                  }
71              }
72  
73              depMgmt.setDependencies(new ArrayList<>(dependencies.values()));
74          }
75      }
76  }