Coverage Report - org.apache.maven.plugin.dependency.utils.filters.DestFileFilter
 
Classes in this File Line Coverage Branch Coverage Complexity
DestFileFilter
95%
63/66
100%
24/24
1.3
 
 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  7
     {
 65  7
         this.outputFileDirectory = outputFileDirectory;
 66  7
         overWriteReleases = false;
 67  7
         overWriteIfNewer = false;
 68  7
         overWriteSnapshots = false;
 69  7
         useSubDirectoryPerArtifact = false;
 70  7
         useSubDirectoryPerType = false;
 71  7
         removeVersion = false;
 72  7
     }
 73  
 
 74  
     public DestFileFilter( boolean overWriteReleases, boolean overWriteSnapshots, boolean overWriteIfNewer,
 75  
                           boolean useSubDirectoryPerArtifact, boolean useSubDirectoryPerType,
 76  
                           boolean useRepositoryLayout, boolean removeVersion, File outputFileDirectory )
 77  85
     {
 78  85
         this.overWriteReleases = overWriteReleases;
 79  85
         this.overWriteSnapshots = overWriteSnapshots;
 80  85
         this.overWriteIfNewer = overWriteIfNewer;
 81  85
         this.useSubDirectoryPerArtifact = useSubDirectoryPerArtifact;
 82  85
         this.useSubDirectoryPerType = useSubDirectoryPerType;
 83  85
         this.useRepositoryLayout = useRepositoryLayout;
 84  85
         this.removeVersion = removeVersion;
 85  85
         this.outputFileDirectory = outputFileDirectory;
 86  85
     }
 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  65
         Set result = new HashSet();
 98  
 
 99  65
         Iterator iter = artifacts.iterator();
 100  
         
 101  239
         while ( iter.hasNext() )
 102  
         {
 103  174
             Artifact artifact = (Artifact) iter.next();
 104  174
             if ( isArtifactIncluded( new ArtifactItem( artifact ) ) )
 105  
             {
 106  166
                 result.add( artifact );
 107  
             }
 108  174
         }
 109  65
         return result;
 110  
     }
 111  
 
 112  
     /**
 113  
      * @return Returns the overWriteReleases.
 114  
      */
 115  
     public boolean isOverWriteReleases()
 116  
     {
 117  2
         return this.overWriteReleases;
 118  
     }
 119  
 
 120  
     /**
 121  
      * @param overWriteReleases
 122  
      *            The overWriteReleases to set.
 123  
      */
 124  
     public void setOverWriteReleases( boolean overWriteReleases )
 125  
     {
 126  2
         this.overWriteReleases = overWriteReleases;
 127  2
     }
 128  
 
 129  
     /**
 130  
      * @return Returns the overWriteSnapshots.
 131  
      */
 132  
     public boolean isOverWriteSnapshots()
 133  
     {
 134  2
         return this.overWriteSnapshots;
 135  
     }
 136  
 
 137  
     /**
 138  
      * @param overWriteSnapshots
 139  
      *            The overWriteSnapshots to set.
 140  
      */
 141  
     public void setOverWriteSnapshots( boolean overWriteSnapshots )
 142  
     {
 143  2
         this.overWriteSnapshots = overWriteSnapshots;
 144  2
     }
 145  
 
 146  
     /**
 147  
      * @return Returns the overWriteIfNewer.
 148  
      */
 149  
     public boolean isOverWriteIfNewer()
 150  
     {
 151  2
         return this.overWriteIfNewer;
 152  
     }
 153  
 
 154  
     /**
 155  
      * @param overWriteIfNewer
 156  
      *            The overWriteIfNewer to set.
 157  
      */
 158  
     public void setOverWriteIfNewer( boolean overWriteIfNewer )
 159  
     {
 160  2
         this.overWriteIfNewer = overWriteIfNewer;
 161  2
     }
 162  
 
 163  
     /**
 164  
      * @return Returns the outputFileDirectory.
 165  
      */
 166  
     public File getOutputFileDirectory()
 167  
     {
 168  2
         return this.outputFileDirectory;
 169  
     }
 170  
 
 171  
     /**
 172  
      * @param outputFileDirectory
 173  
      *            The outputFileDirectory to set.
 174  
      */
 175  
     public void setOutputFileDirectory( File outputFileDirectory )
 176  
     {
 177  1
         this.outputFileDirectory = outputFileDirectory;
 178  1
     }
 179  
 
 180  
     /**
 181  
      * @return Returns the removeVersion.
 182  
      */
 183  
     public boolean isRemoveVersion()
 184  
     {
 185  2
         return this.removeVersion;
 186  
     }
 187  
 
 188  
     /**
 189  
      * @param removeVersion
 190  
      *            The removeVersion to set.
 191  
      */
 192  
     public void setRemoveVersion( boolean removeVersion )
 193  
     {
 194  2
         this.removeVersion = removeVersion;
 195  2
     }
 196  
 
 197  
     /**
 198  
      * @return Returns the useSubDirectoryPerArtifact.
 199  
      */
 200  
     public boolean isUseSubDirectoryPerArtifact()
 201  
     {
 202  2
         return this.useSubDirectoryPerArtifact;
 203  
     }
 204  
 
 205  
     /**
 206  
      * @param useSubDirectoryPerArtifact
 207  
      *            The useSubDirectoryPerArtifact to set.
 208  
      */
 209  
     public void setUseSubDirectoryPerArtifact( boolean useSubDirectoryPerArtifact )
 210  
     {
 211  2
         this.useSubDirectoryPerArtifact = useSubDirectoryPerArtifact;
 212  2
     }
 213  
 
 214  
     /**
 215  
      * @return Returns the useSubDirectoryPerType.
 216  
      */
 217  
     public boolean isUseSubDirectoryPerType()
 218  
     {
 219  2
         return this.useSubDirectoryPerType;
 220  
     }
 221  
 
 222  
     /**
 223  
      * @param useSubDirectoryPerType
 224  
      *            The useSubDirectoryPerType to set.
 225  
      */
 226  
     public void setUseSubDirectoryPerType( boolean useSubDirectoryPerType )
 227  
     {
 228  2
         this.useSubDirectoryPerType = useSubDirectoryPerType;
 229  2
     }
 230  
 
 231  
     /**
 232  
      * 
 233  
      * @return Returns the useRepositoryLayout
 234  
      */
 235  
     public boolean isUseRepositoryLayout()
 236  
     {
 237  0
         return useRepositoryLayout;
 238  
     }
 239  
 
 240  
     /**
 241  
      * 
 242  
      * @param useRepositoryLayout
 243  
      *            the useRepositoryLayout to set
 244  
      */
 245  
     public void setUseRepositoryLayout( boolean useRepositoryLayout )
 246  
     {
 247  0
         this.useRepositoryLayout = useRepositoryLayout;
 248  0
     }
 249  
 
 250  
     public boolean isArtifactIncluded( ArtifactItem item )
 251  
     {
 252  212
         boolean overWrite = false;
 253  212
         boolean result = false;
 254  212
         Artifact artifact = item.getArtifact();
 255  
 
 256  212
         if ( ( artifact.isSnapshot() && this.overWriteSnapshots )
 257  
             || ( !artifact.isSnapshot() && this.overWriteReleases ) )
 258  
         {
 259  13
             overWrite = true;
 260  
         }
 261  
 
 262  212
         File destFolder = item.getOutputDirectory();
 263  212
         if ( destFolder == null )
 264  
         {
 265  174
             destFolder = DependencyUtil.getFormattedOutputDirectory( useSubDirectoryPerType,
 266  
                                                                      useSubDirectoryPerArtifact, useRepositoryLayout,
 267  
                                                                      removeVersion, this.outputFileDirectory, artifact );
 268  
         }
 269  
 
 270  212
         File destFile = null;
 271  212
         if ( StringUtils.isEmpty( item.getDestFileName() ) )
 272  
         {
 273  174
             destFile = new File( destFolder, DependencyUtil.getFormattedFileName( artifact, this.removeVersion ) );
 274  
         }
 275  
         else
 276  
         {
 277  38
             destFile = new File( destFolder, item.getDestFileName() );
 278  
         }
 279  
 
 280  212
         if ( overWrite
 281  
             || ( !destFile.exists() || ( overWriteIfNewer && artifact.getFile().lastModified() > destFile
 282  
                 .lastModified() ) ) )
 283  
         {
 284  202
             result = true;
 285  
         }
 286  212
         return result;
 287  
     }
 288  
 }