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