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.plugins.assembly.artifact;
20  
21  import java.util.LinkedHashSet;
22  import java.util.Set;
23  
24  import org.apache.maven.artifact.Artifact;
25  
26  /**
27   * Helper class used to accumulate scopes and modules (with binaries included) that are used in an assembly, for the
28   * purposes of creating an aggregated managed-version map with dependency version conflicts resolved.
29   *
30   * @author jdcasey
31   */
32  class ResolutionManagementInfo {
33      private final LinkedHashSet<Artifact> artifacts = new LinkedHashSet<>();
34  
35      Set<Artifact> getArtifacts() {
36          return artifacts;
37      }
38  
39      void addArtifacts(final Set<Artifact> a) {
40          for (Artifact artifact : a) {
41              addOneArtifact(artifact);
42          }
43          artifacts.addAll(a);
44      }
45  
46      private void addOneArtifact(Artifact artifact) {
47          for (Artifact existing : artifacts) {
48              if (existing.equals(artifact)) {
49                  if (isScopeUpgrade(artifact, existing)) {
50                      artifacts.remove(existing);
51                      artifacts.add(artifact);
52                      return;
53                  }
54              }
55          }
56      }
57  
58      private boolean isScopeUpgrade(Artifact a, Artifact existing) {
59          return scopeValue(a.getScope()) > scopeValue(existing.getScope());
60      }
61  
62      private int scopeValue(final String scope) {
63          if (Artifact.SCOPE_COMPILE.equals(scope)) {
64              return 5;
65          } else if (Artifact.SCOPE_PROVIDED.equals(scope)) {
66              return 4;
67          } else if (Artifact.SCOPE_RUNTIME.equals(scope)) {
68              return 3;
69          } else if (Artifact.SCOPE_SYSTEM.equals(scope)) {
70              return 2;
71          } else if (Artifact.SCOPE_TEST.equals(scope)) {
72              return 1;
73          }
74          return 0;
75      }
76  }