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  /**
23   * 
24   */
25  
26  import java.io.File;
27  import java.util.HashSet;
28  import java.util.Iterator;
29  import java.util.Set;
30  
31  import org.apache.maven.artifact.Artifact;
32  import org.apache.maven.plugin.dependency.fromConfiguration.ArtifactItem;
33  import org.apache.maven.plugin.dependency.utils.DependencyUtil;
34  import org.apache.maven.shared.artifact.filter.collection.AbstractArtifactsFilter;
35  import org.apache.maven.shared.artifact.filter.collection.ArtifactFilterException;
36  import org.codehaus.plexus.util.StringUtils;
37  
38  /**
39   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
40   * @version $Id: DestFileFilter.java 728546 2008-12-21 22:56:51Z bentmann $
41   */
42  public class DestFileFilter
43      extends AbstractArtifactsFilter
44      implements ArtifactItemFilter
45  {
46  
47      boolean overWriteReleases;
48  
49      boolean overWriteSnapshots;
50  
51      boolean overWriteIfNewer;
52  
53      boolean useSubDirectoryPerArtifact;
54  
55      boolean useSubDirectoryPerType;
56  
57      boolean useRepositoryLayout;
58  
59      boolean removeVersion;
60  
61      File outputFileDirectory;
62  
63      public DestFileFilter( File outputFileDirectory )
64      {
65          this.outputFileDirectory = outputFileDirectory;
66          overWriteReleases = false;
67          overWriteIfNewer = false;
68          overWriteSnapshots = false;
69          useSubDirectoryPerArtifact = false;
70          useSubDirectoryPerType = false;
71          removeVersion = false;
72      }
73  
74      public DestFileFilter( boolean overWriteReleases, boolean overWriteSnapshots, boolean overWriteIfNewer,
75                            boolean useSubDirectoryPerArtifact, boolean useSubDirectoryPerType,
76                            boolean useRepositoryLayout, boolean removeVersion, File outputFileDirectory )
77      {
78          this.overWriteReleases = overWriteReleases;
79          this.overWriteSnapshots = overWriteSnapshots;
80          this.overWriteIfNewer = overWriteIfNewer;
81          this.useSubDirectoryPerArtifact = useSubDirectoryPerArtifact;
82          this.useSubDirectoryPerType = useSubDirectoryPerType;
83          this.useRepositoryLayout = useRepositoryLayout;
84          this.removeVersion = removeVersion;
85          this.outputFileDirectory = outputFileDirectory;
86      }
87  
88      /*
89       * (non-Javadoc)
90       * 
91       * @see org.apache.mojo.dependency.utils.filters.ArtifactsFilter#filter(java.util.Set,
92       *      org.apache.maven.plugin.logging.Log)
93       */
94      public Set filter( Set artifacts )
95          throws ArtifactFilterException
96      {
97          Set result = new HashSet();
98  
99          Iterator iter = artifacts.iterator();
100         
101         while ( iter.hasNext() )
102         {
103             Artifact artifact = (Artifact) iter.next();
104             if ( isArtifactIncluded( new ArtifactItem( artifact ) ) )
105             {
106                 result.add( artifact );
107             }
108         }
109         return result;
110     }
111 
112     /**
113      * @return Returns the overWriteReleases.
114      */
115     public boolean isOverWriteReleases()
116     {
117         return this.overWriteReleases;
118     }
119 
120     /**
121      * @param overWriteReleases
122      *            The overWriteReleases to set.
123      */
124     public void setOverWriteReleases( boolean overWriteReleases )
125     {
126         this.overWriteReleases = overWriteReleases;
127     }
128 
129     /**
130      * @return Returns the overWriteSnapshots.
131      */
132     public boolean isOverWriteSnapshots()
133     {
134         return this.overWriteSnapshots;
135     }
136 
137     /**
138      * @param overWriteSnapshots
139      *            The overWriteSnapshots to set.
140      */
141     public void setOverWriteSnapshots( boolean overWriteSnapshots )
142     {
143         this.overWriteSnapshots = overWriteSnapshots;
144     }
145 
146     /**
147      * @return Returns the overWriteIfNewer.
148      */
149     public boolean isOverWriteIfNewer()
150     {
151         return this.overWriteIfNewer;
152     }
153 
154     /**
155      * @param overWriteIfNewer
156      *            The overWriteIfNewer to set.
157      */
158     public void setOverWriteIfNewer( boolean overWriteIfNewer )
159     {
160         this.overWriteIfNewer = overWriteIfNewer;
161     }
162 
163     /**
164      * @return Returns the outputFileDirectory.
165      */
166     public File getOutputFileDirectory()
167     {
168         return this.outputFileDirectory;
169     }
170 
171     /**
172      * @param outputFileDirectory
173      *            The outputFileDirectory to set.
174      */
175     public void setOutputFileDirectory( File outputFileDirectory )
176     {
177         this.outputFileDirectory = outputFileDirectory;
178     }
179 
180     /**
181      * @return Returns the removeVersion.
182      */
183     public boolean isRemoveVersion()
184     {
185         return this.removeVersion;
186     }
187 
188     /**
189      * @param removeVersion
190      *            The removeVersion to set.
191      */
192     public void setRemoveVersion( boolean removeVersion )
193     {
194         this.removeVersion = removeVersion;
195     }
196 
197     /**
198      * @return Returns the useSubDirectoryPerArtifact.
199      */
200     public boolean isUseSubDirectoryPerArtifact()
201     {
202         return this.useSubDirectoryPerArtifact;
203     }
204 
205     /**
206      * @param useSubDirectoryPerArtifact
207      *            The useSubDirectoryPerArtifact to set.
208      */
209     public void setUseSubDirectoryPerArtifact( boolean useSubDirectoryPerArtifact )
210     {
211         this.useSubDirectoryPerArtifact = useSubDirectoryPerArtifact;
212     }
213 
214     /**
215      * @return Returns the useSubDirectoryPerType.
216      */
217     public boolean isUseSubDirectoryPerType()
218     {
219         return this.useSubDirectoryPerType;
220     }
221 
222     /**
223      * @param useSubDirectoryPerType
224      *            The useSubDirectoryPerType to set.
225      */
226     public void setUseSubDirectoryPerType( boolean useSubDirectoryPerType )
227     {
228         this.useSubDirectoryPerType = useSubDirectoryPerType;
229     }
230 
231     /**
232      * 
233      * @return Returns the useRepositoryLayout
234      */
235     public boolean isUseRepositoryLayout()
236     {
237         return useRepositoryLayout;
238     }
239 
240     /**
241      * 
242      * @param useRepositoryLayout
243      *            the useRepositoryLayout to set
244      */
245     public void setUseRepositoryLayout( boolean useRepositoryLayout )
246     {
247         this.useRepositoryLayout = useRepositoryLayout;
248     }
249 
250     public boolean isArtifactIncluded( ArtifactItem item )
251     {
252         boolean overWrite = false;
253         boolean result = false;
254         Artifact artifact = item.getArtifact();
255 
256         if ( ( artifact.isSnapshot() && this.overWriteSnapshots )
257             || ( !artifact.isSnapshot() && this.overWriteReleases ) )
258         {
259             overWrite = true;
260         }
261 
262         File destFolder = item.getOutputDirectory();
263         if ( destFolder == null )
264         {
265             destFolder = DependencyUtil.getFormattedOutputDirectory( useSubDirectoryPerType,
266                                                                      useSubDirectoryPerArtifact, useRepositoryLayout,
267                                                                      removeVersion, this.outputFileDirectory, artifact );
268         }
269 
270         File destFile = null;
271         if ( StringUtils.isEmpty( item.getDestFileName() ) )
272         {
273             destFile = new File( destFolder, DependencyUtil.getFormattedFileName( artifact, this.removeVersion ) );
274         }
275         else
276         {
277             destFile = new File( destFolder, item.getDestFileName() );
278         }
279 
280         if ( overWrite
281             || ( !destFile.exists() || ( overWriteIfNewer && artifact.getFile().lastModified() > destFile
282                 .lastModified() ) ) )
283         {
284             result = true;
285         }
286         return result;
287     }
288 }