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