View Javadoc
1   package org.apache.maven.plugin.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  import org.apache.maven.project.MavenProject;
24  import org.apache.maven.shared.artifact.filter.ScopeArtifactFilter;
25  
26  import java.util.LinkedHashSet;
27  import java.util.Set;
28  
29  /**
30   * Helper class used to accumulate scopes and modules (with binaries included) that are used in an assembly, for the
31   * purposes of creating an aggregated managed-version map with dependency version conflicts resolved.
32   *
33   * @author jdcasey
34   */
35  class ResolutionManagementInfo
36  {
37      private boolean resolutionRequired;
38  
39      private final ScopeArtifactFilter scopeFilter = new ScopeArtifactFilter();
40  
41      private boolean resolvedTransitively;
42  
43      private final Set<MavenProject> enabledProjects = new LinkedHashSet<MavenProject>();
44  
45      private final LinkedHashSet<Artifact> artifacts = new LinkedHashSet<Artifact>();
46  
47      ResolutionManagementInfo( final MavenProject currentProject )
48      {
49          enabledProjects.add( currentProject );
50      }
51  
52      boolean isResolutionRequired()
53      {
54          return resolutionRequired;
55      }
56  
57      void setResolutionRequired( final boolean resolutionRequired )
58      {
59          this.resolutionRequired = resolutionRequired;
60      }
61  
62      boolean isResolvedTransitively()
63      {
64          return resolvedTransitively;
65      }
66  
67      void setResolvedTransitively( final boolean resolvedTransitively )
68      {
69          this.resolvedTransitively = this.resolvedTransitively || resolvedTransitively;
70      }
71  
72      ScopeArtifactFilter getScopeFilter()
73      {
74          return scopeFilter;
75      }
76  
77      void enableCompileScope()
78      {
79          scopeFilter.setIncludeCompileScope( true );
80          scopeFilter.setIncludeProvidedScope( true );
81          scopeFilter.setIncludeSystemScope( true );
82      }
83  
84      void enableProvidedScope()
85      {
86          scopeFilter.setIncludeProvidedScope( true );
87      }
88  
89      void enableRuntimeScope()
90      {
91          scopeFilter.setIncludeRuntimeScope( true );
92          scopeFilter.setIncludeCompileScope( true );
93      }
94  
95      void enableTestScope()
96      {
97          scopeFilter.setIncludeTestScope( true );
98          scopeFilter.setIncludeCompileScope( true );
99          scopeFilter.setIncludeProvidedScope( true );
100         scopeFilter.setIncludeSystemScope( true );
101         scopeFilter.setIncludeRuntimeScope( true );
102     }
103 
104     void enableSystemScope()
105     {
106         scopeFilter.setIncludeSystemScope( true );
107     }
108 
109     void enableProjectResolution( final MavenProject project )
110     {
111         if ( !enabledProjects.contains( project ) )
112         {
113             enabledProjects.add( project );
114         }
115     }
116 
117     Set<MavenProject> getEnabledProjects()
118     {
119         return enabledProjects;
120     }
121 
122     Set<Artifact> getArtifacts()
123     {
124         return artifacts;
125     }
126 
127     void addArtifacts( final Set<Artifact> a )
128     {
129         for ( Artifact artifact : a )
130         {
131             addOneArtifact( artifact );
132         }
133         artifacts.addAll( a );
134     }
135 
136     private void addOneArtifact( Artifact artifact )
137     {
138         for ( Artifact existing : artifacts )
139         {
140             if ( existing.equals( artifact ) )
141             {
142                 if ( isScopeUpgrade( artifact, existing ) )
143                 {
144                     artifacts.remove( existing );
145                     artifacts.add( artifact );
146                     return;
147                 }
148             }
149         }
150     }
151 
152     private boolean isScopeUpgrade( Artifact a, Artifact existing )
153     {
154         return scopeValue( a.getScope() ) > scopeValue( existing.getScope() );
155     }
156 
157     private int scopeValue( final String scope )
158     {
159         if ( Artifact.SCOPE_COMPILE.equals( scope ) )
160         {
161             return 5;
162         }
163         else if ( Artifact.SCOPE_PROVIDED.equals( scope ) )
164         {
165             return 4;
166         }
167         else if ( Artifact.SCOPE_RUNTIME.equals( scope ) )
168         {
169             return 3;
170         }
171         else if ( Artifact.SCOPE_SYSTEM.equals( scope ) )
172         {
173             return 2;
174         }
175         else if ( Artifact.SCOPE_TEST.equals( scope ) )
176         {
177             return 1;
178         }
179         return 0;
180     }
181 
182 }