View Javadoc
1   package org.apache.maven.plugins.assembly.artifact;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.artifact.Artifact;
23  
24  import java.util.LinkedHashSet;
25  import java.util.Set;
26  
27  /**
28   * Helper class used to accumulate scopes and modules (with binaries included) that are used in an assembly, for the
29   * purposes of creating an aggregated managed-version map with dependency version conflicts resolved.
30   *
31   * @author jdcasey
32   */
33  class ResolutionManagementInfo
34  {
35      private final LinkedHashSet<Artifact> artifacts = new LinkedHashSet<>();
36  
37      Set<Artifact> getArtifacts()
38      {
39          return artifacts;
40      }
41  
42      void addArtifacts( final Set<Artifact> a )
43      {
44          for ( Artifact artifact : a )
45          {
46              addOneArtifact( artifact );
47          }
48          artifacts.addAll( a );
49      }
50  
51      private void addOneArtifact( Artifact artifact )
52      {
53          for ( Artifact existing : artifacts )
54          {
55              if ( existing.equals( artifact ) )
56              {
57                  if ( isScopeUpgrade( artifact, existing ) )
58                  {
59                      artifacts.remove( existing );
60                      artifacts.add( artifact );
61                      return;
62                  }
63              }
64          }
65      }
66  
67      private boolean isScopeUpgrade( Artifact a, Artifact existing )
68      {
69          return scopeValue( a.getScope() ) > scopeValue( existing.getScope() );
70      }
71  
72      private int scopeValue( final String scope )
73      {
74          if ( Artifact.SCOPE_COMPILE.equals( scope ) )
75          {
76              return 5;
77          }
78          else if ( Artifact.SCOPE_PROVIDED.equals( scope ) )
79          {
80              return 4;
81          }
82          else if ( Artifact.SCOPE_RUNTIME.equals( scope ) )
83          {
84              return 3;
85          }
86          else if ( Artifact.SCOPE_SYSTEM.equals( scope ) )
87          {
88              return 2;
89          }
90          else if ( Artifact.SCOPE_TEST.equals( scope ) )
91          {
92              return 1;
93          }
94          return 0;
95      }
96  
97  }