View Javadoc
1   package org.apache.maven.shared.artifact.filter.collection;
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 java.util.LinkedHashSet;
23  import java.util.Set;
24  
25  import org.apache.maven.artifact.Artifact;
26  import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
27  import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter;
28  
29  import static org.apache.maven.shared.artifact.filter.internal.Utils.isNotEmpty;
30  
31  /**
32   * <p>ScopeFilter class.</p>
33   *
34   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
35   */
36  public class ScopeFilter
37      extends AbstractArtifactsFilter
38  {
39  
40      private String includeScope;
41  
42      private String excludeScope;
43  
44      /**
45       * <p>Constructor for ScopeFilter.</p>
46       *
47       * @param includeScope the scope to be included.
48       * @param excludeScope the scope to be excluded.
49       */
50      public ScopeFilter( String includeScope, String excludeScope )
51      {
52          this.includeScope = includeScope;
53          this.excludeScope = excludeScope;
54      }
55  
56      /**
57       * {@inheritDoc}
58       *
59       * This function determines if filtering needs to be performed. Excludes are
60       * ignored if Includes are used.
61       */
62      public Set<Artifact> filter( Set<Artifact> artifacts )
63          throws ArtifactFilterException
64      {
65          Set<Artifact> results = artifacts;
66  
67          if ( isNotEmpty( includeScope ) )
68          {
69              if ( !Artifact.SCOPE_COMPILE.equals( includeScope ) && !Artifact.SCOPE_TEST.equals( includeScope )
70                  && !Artifact.SCOPE_PROVIDED.equals( includeScope ) && !Artifact.SCOPE_RUNTIME.equals( includeScope )
71                  && !Artifact.SCOPE_SYSTEM.equals( includeScope ) )
72              {
73                  throw new ArtifactFilterException( "Invalid Scope in includeScope: " + includeScope );
74              }
75  
76              results = new LinkedHashSet<>();
77  
78              if ( Artifact.SCOPE_PROVIDED.equals( includeScope ) || Artifact.SCOPE_SYSTEM.equals( includeScope ) )
79              {
80                  results = includeSingleScope( artifacts, includeScope );
81              }
82              else
83              {
84                  ArtifactFilter saf = new ScopeArtifactFilter( includeScope );
85  
86                  for ( Artifact artifact : artifacts )
87                  {
88                      if ( saf.include( artifact ) )
89                      {
90                          results.add( artifact );
91                      }
92                  }
93              }
94          }
95          else if ( isNotEmpty( excludeScope ) )
96          {
97              if ( !Artifact.SCOPE_COMPILE.equals( excludeScope ) && !Artifact.SCOPE_TEST.equals( excludeScope )
98                  && !Artifact.SCOPE_PROVIDED.equals( excludeScope ) && !Artifact.SCOPE_RUNTIME.equals( excludeScope )
99                  && !Artifact.SCOPE_SYSTEM.equals( excludeScope ) )
100             {
101                 throw new ArtifactFilterException( "Invalid Scope in excludeScope: " + excludeScope );
102             }
103             results = new LinkedHashSet<>();
104             // plexus ScopeArtifactFilter doesn't handle the provided scope so
105             // we
106             // need special handling for it.
107             if ( Artifact.SCOPE_TEST.equals( excludeScope ) )
108             {
109                 throw new ArtifactFilterException( " Can't exclude Test scope, this will exclude everything." );
110             }
111             else if ( !Artifact.SCOPE_PROVIDED.equals( excludeScope ) && !Artifact.SCOPE_SYSTEM.equals( excludeScope ) )
112             {
113                 ArtifactFilter saf = new ScopeArtifactFilter( excludeScope );
114 
115                 for ( Artifact artifact : artifacts )
116                 {
117                     if ( !saf.include( artifact ) )
118                     {
119                         results.add( artifact );
120                     }
121                 }
122             }
123             else
124             {
125                 results = excludeSingleScope( artifacts, excludeScope );
126             }
127         }
128 
129         return results;
130     }
131 
132     private Set<Artifact> includeSingleScope( Set<Artifact> artifacts, String scope )
133     {
134         Set<Artifact> results = new LinkedHashSet<>();
135         for ( Artifact artifact : artifacts )
136         {
137             if ( scope.equals( artifact.getScope() ) )
138             {
139                 results.add( artifact );
140             }
141         }
142         return results;
143     }
144 
145     private Set<Artifact> excludeSingleScope( Set<Artifact> artifacts, String scope )
146     {
147         Set<Artifact> results = new LinkedHashSet<>();
148         for ( Artifact artifact : artifacts )
149         {
150             if ( !scope.equals( artifact.getScope() ) )
151             {
152                 results.add( artifact );
153             }
154         }
155         return results;
156     }
157 
158     /**
159      * <p>Getter for the field <code>includeScope</code>.</p>
160      *
161      * @return Returns the includeScope.
162      */
163     public String getIncludeScope()
164     {
165         return this.includeScope;
166     }
167 
168     /**
169      * <p>Setter for the field <code>includeScope</code>.</p>
170      *
171      * @param scope
172      *            The includeScope to set.
173      */
174     public void setIncludeScope( String scope )
175     {
176         this.includeScope = scope;
177     }
178 
179     /**
180      * <p>Getter for the field <code>excludeScope</code>.</p>
181      *
182      * @return Returns the excludeScope.
183      */
184     public String getExcludeScope()
185     {
186         return this.excludeScope;
187     }
188 
189     /**
190      * <p>Setter for the field <code>excludeScope</code>.</p>
191      *
192      * @param scope
193      *            The excludeScope to set.
194      */
195     public void setExcludeScope( String scope )
196     {
197         this.excludeScope = scope;
198     }
199 
200 }