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