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