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 861760 2013-05-12 17:31:26Z hboutemy $
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> artifacts_ = artifacts;
68          Set<Artifact> result = new HashSet<Artifact>();
69          
70          for ( Artifact artifact : artifacts_ )
71          {
72              if ( isArtifactIncluded( new ArtifactItem( artifact ) ) )
73              {
74                  result.add( artifact );
75              }
76          }
77          return result;
78      }
79  
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
110      *            The overWriteReleases to set.
111      */
112     public void setOverWriteReleases( boolean overWriteReleases )
113     {
114         this.overWriteReleases = overWriteReleases;
115     }
116 
117     /**
118      * @return Returns the overWriteSnapshots.
119      */
120     public boolean isOverWriteSnapshots()
121     {
122         return this.overWriteSnapshots;
123     }
124 
125     /**
126      * @param overWriteSnapshots
127      *            The overWriteSnapshots to set.
128      */
129     public void setOverWriteSnapshots( boolean overWriteSnapshots )
130     {
131         this.overWriteSnapshots = overWriteSnapshots;
132     }
133 
134     /**
135      * @return Returns the overWriteIfNewer.
136      */
137     public boolean isOverWriteIfNewer()
138     {
139         return this.overWriteIfNewer;
140     }
141 
142     /**
143      * @param overWriteIfNewer
144      *            The overWriteIfNewer to set.
145      */
146     public void setOverWriteIfNewer( boolean overWriteIfNewer )
147     {
148         this.overWriteIfNewer = overWriteIfNewer;
149     }
150 }