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.plugins.dependency.utils.filters;
20  
21  import java.util.LinkedHashSet;
22  import java.util.Set;
23  import org.apache.maven.artifact.Artifact;
24  import org.apache.maven.plugin.MojoExecutionException;
25  import org.apache.maven.plugins.dependency.fromConfiguration.ArtifactItem;
26  import org.apache.maven.plugins.dependency.utils.markers.MarkerHandler;
27  import org.apache.maven.shared.artifact.filter.collection.AbstractArtifactsFilter;
28  import org.apache.maven.shared.artifact.filter.collection.ArtifactFilterException;
29  
30  /**
31   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
32   */
33  public class MarkerFileFilter extends AbstractArtifactsFilter implements ArtifactItemFilter {
34  
35      private boolean overWriteReleases;
36  
37      private boolean overWriteSnapshots;
38  
39      private boolean overWriteIfNewer;
40  
41      /**
42       * The handler.
43       */
44      protected final MarkerHandler handler;
45  
46      /**
47       * @param overWriteReleases true/false.
48       * @param overWriteSnapshots true/false.
49       * @param overWriteIfNewer true/false.
50       * @param handler {@link MarkerHandler}
51       */
52      public MarkerFileFilter(
53              boolean overWriteReleases, boolean overWriteSnapshots, boolean overWriteIfNewer, MarkerHandler handler) {
54          this.overWriteReleases = overWriteReleases;
55          this.overWriteSnapshots = overWriteSnapshots;
56          this.overWriteIfNewer = overWriteIfNewer;
57          this.handler = handler;
58      }
59  
60      /*
61       * (non-Javadoc)
62       * @see org.apache.mojo.dependency.utils.filters.ArtifactsFilter#filter(java.util.Set,
63       * org.apache.maven.plugin.logging.Log)
64       */
65      @Override
66      public Set<Artifact> filter(Set<Artifact> artifacts) throws ArtifactFilterException {
67          Set<Artifact> result = new LinkedHashSet<>();
68  
69          for (Artifact artifact : artifacts) {
70              if (isArtifactIncluded(new ArtifactItem(artifact))) {
71                  result.add(artifact);
72              }
73          }
74  
75          return result;
76      }
77  
78      @Override
79      public boolean isArtifactIncluded(ArtifactItem item) throws ArtifactFilterException {
80          Artifact artifact = item.getArtifact();
81  
82          boolean overWrite = (artifact.isSnapshot() && this.overWriteSnapshots)
83                  || (!artifact.isSnapshot() && this.overWriteReleases);
84  
85          handler.setArtifact(artifact);
86  
87          try {
88              return overWrite || !handler.isMarkerSet() || (overWriteIfNewer && handler.isMarkerOlder(artifact));
89          } catch (MojoExecutionException e) {
90              throw new ArtifactFilterException(e.getMessage(), e);
91          }
92      }
93  
94      /**
95       * @return Returns the overWriteReleases.
96       */
97      public boolean isOverWriteReleases() {
98          return this.overWriteReleases;
99      }
100 
101     /**
102      * @param overWriteReleases The overWriteReleases to set.
103      */
104     public void setOverWriteReleases(boolean overWriteReleases) {
105         this.overWriteReleases = overWriteReleases;
106     }
107 
108     /**
109      * @return Returns the overWriteSnapshots.
110      */
111     public boolean isOverWriteSnapshots() {
112         return this.overWriteSnapshots;
113     }
114 
115     /**
116      * @param overWriteSnapshots The overWriteSnapshots to set.
117      */
118     public void setOverWriteSnapshots(boolean overWriteSnapshots) {
119         this.overWriteSnapshots = overWriteSnapshots;
120     }
121 
122     /**
123      * @return Returns the overWriteIfNewer.
124      */
125     public boolean isOverWriteIfNewer() {
126         return this.overWriteIfNewer;
127     }
128 
129     /**
130      * @param overWriteIfNewer The overWriteIfNewer to set.
131      */
132     public void setOverWriteIfNewer(boolean overWriteIfNewer) {
133         this.overWriteIfNewer = overWriteIfNewer;
134     }
135 }