View Javadoc
1   package org.apache.maven.plugin.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.HashSet;
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.plugin.dependency.fromConfiguration.ArtifactItem;
28  import org.apache.maven.plugin.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.html 937155 2015-01-21 21:53:50Z 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       *
61       * @see org.apache.mojo.dependency.utils.filters.ArtifactsFilter#filter(java.util.Set,
62       *      org.apache.maven.plugin.logging.Log)
63       */
64      public Set filter( Set artifacts )
65          throws ArtifactFilterException
66      {
67          Set<Artifact> result = new HashSet<Artifact>();
68          
69          for ( Artifact artifact : (Set<Artifact>) artifacts )
70          {
71              if ( isArtifactIncluded( new ArtifactItem( artifact ) ) )
72              {
73                  result.add( artifact );
74              }
75          }
76          return result;
77      }
78  
79      public boolean isArtifactIncluded( ArtifactItem item )
80        throws ArtifactFilterException
81      {
82          Artifact artifact = item.getArtifact();
83  
84          boolean overWrite = ( artifact.isSnapshot() && this.overWriteSnapshots )
85              || ( !artifact.isSnapshot() && this.overWriteReleases );
86  
87          handler.setArtifact( artifact );
88  
89          try
90          {
91              return overWrite || !handler.isMarkerSet() || ( overWriteIfNewer && handler.isMarkerOlder( artifact ) );
92          }
93          catch ( MojoExecutionException e )
94          {
95              throw new ArtifactFilterException( e.getMessage(), e );
96          }
97      }
98  
99      /**
100      * @return Returns the overWriteReleases.
101      */
102     public boolean isOverWriteReleases()
103     {
104         return this.overWriteReleases;
105     }
106 
107     /**
108      * @param overWriteReleases
109      *            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
126      *            The overWriteSnapshots to set.
127      */
128     public void setOverWriteSnapshots( boolean overWriteSnapshots )
129     {
130         this.overWriteSnapshots = overWriteSnapshots;
131     }
132 
133     /**
134      * @return Returns the overWriteIfNewer.
135      */
136     public boolean isOverWriteIfNewer()
137     {
138         return this.overWriteIfNewer;
139     }
140 
141     /**
142      * @param overWriteIfNewer
143      *            The overWriteIfNewer to set.
144      */
145     public void setOverWriteIfNewer( boolean overWriteIfNewer )
146     {
147         this.overWriteIfNewer = overWriteIfNewer;
148     }
149 }