Coverage Report - org.apache.maven.plugin.assembly.archive.task.AddDirectoryTask
 
Classes in this File Line Coverage Branch Coverage Complexity
AddDirectoryTask
92%
65/71
79%
27/34
0
 
 1  
 package org.apache.maven.plugin.assembly.archive.task;
 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 org.apache.maven.plugin.assembly.AssemblerConfigurationSource;
 23  
 import org.apache.maven.plugin.assembly.archive.ArchiveCreationException;
 24  
 import org.apache.maven.plugin.assembly.utils.AssemblyFormatUtils;
 25  
 import org.codehaus.plexus.archiver.Archiver;
 26  
 import org.codehaus.plexus.archiver.ArchiverException;
 27  
 import org.codehaus.plexus.archiver.util.DefaultFileSet;
 28  
 
 29  
 import java.io.File;
 30  
 import java.util.ArrayList;
 31  
 import java.util.Iterator;
 32  
 import java.util.List;
 33  
 
 34  
 /**
 35  
  * @version $Id: AddDirectoryTask.java 1074206 2011-02-24 16:42:54Z dennisl $
 36  
  */
 37  
 public class AddDirectoryTask
 38  
     implements ArchiverTask
 39  
 {
 40  
 
 41  
     private final File directory;
 42  
 
 43  
     private List<String> includes;
 44  
 
 45  
     private List<String> excludes;
 46  
 
 47  
     private String outputDirectory;
 48  
 
 49  7
     private boolean useDefaultExcludes = true;
 50  
 
 51  7
     private int directoryMode = -1;
 52  
 
 53  7
     private int fileMode = -1;
 54  
 
 55  
     public AddDirectoryTask( final File directory )
 56  7
     {
 57  7
         this.directory = directory;
 58  7
     }
 59  
 
 60  
     public void execute( final Archiver archiver, final AssemblerConfigurationSource configSource )
 61  
         throws ArchiveCreationException
 62  
     {
 63  7
         if ( ".".equals( outputDirectory ) )
 64  
         {
 65  0
             outputDirectory = "";
 66  
         }
 67  7
         else if ( "..".equals( outputDirectory ) )
 68  
         {
 69  0
             throw new ArchiveCreationException( "Cannot add source directory: " + directory + " to archive-path: "
 70  
                             + outputDirectory + ". All paths must be within the archive root directory." );
 71  
         }
 72  
 
 73  7
         final int oldDirMode = archiver.getOverrideDirectoryMode();
 74  7
         final int oldFileMode = archiver.getOverrideFileMode();
 75  
 
 76  7
         boolean fileModeSet = false;
 77  7
         boolean dirModeSet = false;
 78  
 
 79  
         try
 80  
         {
 81  7
             if ( directoryMode != -1 )
 82  
             {
 83  2
                 archiver.setDirectoryMode( directoryMode );
 84  2
                 dirModeSet = true;
 85  
             }
 86  
 
 87  7
             if ( fileMode != -1 )
 88  
             {
 89  2
                 archiver.setFileMode( fileMode );
 90  2
                 fileModeSet = true;
 91  
             }
 92  
 
 93  7
             if ( directory.exists() )
 94  
             {
 95  
                 List<String> directoryExcludes;
 96  6
                 if ( excludes != null && !excludes.isEmpty() )
 97  
                 {
 98  3
                     directoryExcludes = new ArrayList<String>( excludes );
 99  
                 }
 100  
                 else
 101  
                 {
 102  3
                     directoryExcludes = new ArrayList<String>();
 103  
                 }
 104  
 
 105  
                 try
 106  
                 {
 107  6
                     String[] includesArray = null;
 108  6
                     if ( includes != null && !includes.isEmpty() )
 109  
                     {
 110  1
                         includesArray = new String[includes.size()];
 111  
 
 112  1
                         int i = 0;
 113  1
                         for ( final Iterator<String> it = includes.iterator(); it.hasNext(); )
 114  
                         {
 115  1
                             String value = AssemblyFormatUtils.fixRelativeRefs( it.next() );
 116  
                             
 117  1
                             if ( value.startsWith( "/" ) || value.startsWith( "\\" ) )
 118  
                             {
 119  0
                                 value = value.substring( 1 );
 120  
                             }
 121  
 
 122  1
                             includesArray[i] = value;
 123  
 
 124  1
                             i++;
 125  1
                         }
 126  
                     }
 127  
 
 128  
                     // this one is guaranteed to be non-null by code above.
 129  6
                     final String[] excludesArray = new String[directoryExcludes.size()];
 130  
 
 131  6
                     int i = 0;
 132  6
                     for ( final Iterator<String> it = directoryExcludes.iterator(); it.hasNext(); )
 133  
                     {
 134  5
                         String value = AssemblyFormatUtils.fixRelativeRefs( it.next() );
 135  
                         
 136  5
                         if ( value.startsWith( "/" ) || value.startsWith( "\\" ) )
 137  
                         {
 138  0
                             value = value.substring( 1 );
 139  
                         }
 140  
 
 141  5
                         excludesArray[i] = value;
 142  
 
 143  5
                         i++;
 144  5
                     }
 145  
 
 146  6
                     final DefaultFileSet fs = new DefaultFileSet();
 147  6
                     fs.setUsingDefaultExcludes( useDefaultExcludes );
 148  6
                     fs.setPrefix( outputDirectory );
 149  6
                     fs.setDirectory( directory );
 150  6
                     fs.setIncludes( includesArray );
 151  6
                     fs.setExcludes( excludesArray );
 152  
 
 153  6
                     archiver.addFileSet( fs );
 154  
                 }
 155  0
                 catch ( final ArchiverException e )
 156  
                 {
 157  0
                     throw new ArchiveCreationException( "Error adding directory to archive: " + e.getMessage(), e );
 158  6
                 }
 159  
             }
 160  
         }
 161  
         finally
 162  
         {
 163  7
             if ( dirModeSet )
 164  
             {
 165  2
                 archiver.setDirectoryMode( oldDirMode );
 166  
             }
 167  
 
 168  7
             if ( fileModeSet )
 169  
             {
 170  2
                 archiver.setFileMode( oldFileMode );
 171  
             }
 172  
         }
 173  7
     }
 174  
 
 175  
     public void setExcludes( final List<String> excludes )
 176  
     {
 177  3
         this.excludes = excludes;
 178  3
     }
 179  
 
 180  
     public void setIncludes( final List<String> includes )
 181  
     {
 182  3
         this.includes = includes;
 183  3
     }
 184  
 
 185  
     public void setOutputDirectory( final String outputDirectory )
 186  
     {
 187  6
         this.outputDirectory = outputDirectory;
 188  6
     }
 189  
 
 190  
     public void setDirectoryMode( final int directoryMode )
 191  
     {
 192  2
         this.directoryMode = directoryMode;
 193  2
     }
 194  
 
 195  
     public void setFileMode( final int fileMode )
 196  
     {
 197  2
         this.fileMode = fileMode;
 198  2
     }
 199  
 
 200  
     public void setUseDefaultExcludes( final boolean useDefaultExcludes )
 201  
     {
 202  2
         this.useDefaultExcludes = useDefaultExcludes;
 203  2
     }
 204  
 
 205  
 }