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.HashSet;
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  import org.apache.maven.shared.utils.StringUtils;
29  
30  /**
31   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
32   * @version $Id: ScopeFilter.java 1716978 2015-11-28 14:47:04Z khmarbaise $
33   */
34  public class ScopeFilter
35      extends AbstractArtifactsFilter
36  {
37  
38      private String includeScope;
39  
40      private String excludeScope;
41  
42      /**
43       * @param includeScope the scope to be included.
44       * @param excludeScope the scope to be excluded.
45       */
46      public ScopeFilter( String includeScope, String excludeScope )
47      {
48          this.includeScope = includeScope;
49          this.excludeScope = excludeScope;
50      }
51  
52      /**
53       * This function determines if filtering needs to be performed. Excludes are
54       * ignored if Includes are used.
55       * 
56       * @param artifacts
57       *            the set of artifacts to filter.
58       * 
59       * @return a Set of filtered artifacts.
60       * @throws ArtifactFilterException when there's an invalid included scope
61       */
62      public Set<Artifact> filter( Set<Artifact> artifacts )
63          throws ArtifactFilterException
64      {
65          Set<Artifact> results = artifacts;
66  
67          if ( StringUtils.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 HashSet<Artifact>();
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 ( StringUtils.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 HashSet<Artifact>();
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 HashSet<Artifact>();
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 HashSet<Artifact>();
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      * @return Returns the includeScope.
160      */
161     public String getIncludeScope()
162     {
163         return this.includeScope;
164     }
165 
166     /**
167      * @param scope
168      *            The includeScope to set.
169      */
170     public void setIncludeScope( String scope )
171     {
172         this.includeScope = scope;
173     }
174 
175     /**
176      * @return Returns the excludeScope.
177      */
178     public String getExcludeScope()
179     {
180         return this.excludeScope;
181     }
182 
183     /**
184      * @param scope
185      *            The excludeScope to set.
186      */
187     public void setExcludeScope( String scope )
188     {
189         this.excludeScope = scope;
190     }
191 
192 }