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   * @deprecated use {@code org.apache.maven.api.services.ModelBuilder} instead
39   */
40  @Named
41  @Singleton
42  @Deprecated(since = "4.0.0")
43  public class DefaultDependencyManagementImporter implements DependencyManagementImporter {
44  
45      @Override
46      public void importManagement(
47              Model target,
48              List<? extends DependencyManagement> sources,
49              ModelBuildingRequest request,
50              ModelProblemCollector problems) {
51          if (sources != null && !sources.isEmpty()) {
52              Map<String, Dependency> dependencies = new LinkedHashMap<>();
53  
54              DependencyManagement depMgmt = target.getDependencyManagement();
55  
56              if (depMgmt != null) {
57                  for (Dependency dependency : depMgmt.getDependencies()) {
58                      dependencies.put(dependency.getManagementKey(), dependency);
59                  }
60              } else {
61                  depMgmt = new DependencyManagement();
62                  target.setDependencyManagement(depMgmt);
63              }
64  
65              for (DependencyManagement source : sources) {
66                  for (Dependency dependency : source.getDependencies()) {
67                      String key = dependency.getManagementKey();
68                      if (!dependencies.containsKey(key)) {
69                          dependencies.put(key, dependency);
70                      }
71                  }
72              }
73  
74              depMgmt.setDependencies(new ArrayList<>(dependencies.values()));
75          }
76      }
77  }