Coverage Report - org.apache.maven.plugin.assembly.archive.task.AddFileSetsTask
 
Classes in this File Line Coverage Branch Coverage Complexity
AddFileSetsTask
87%
63/72
78%
25/32
3
 
 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.format.AssemblyFormattingException;
 25  
 import org.apache.maven.plugin.assembly.format.FileSetFormatter;
 26  
 import org.apache.maven.plugin.assembly.model.FileSet;
 27  
 import org.apache.maven.plugin.assembly.utils.AssemblyFormatUtils;
 28  
 import org.apache.maven.plugin.assembly.utils.TypeConversionUtils;
 29  
 import org.apache.maven.project.MavenProject;
 30  
 import org.codehaus.plexus.archiver.Archiver;
 31  
 import org.codehaus.plexus.logging.Logger;
 32  
 import org.codehaus.plexus.logging.console.ConsoleLogger;
 33  
 
 34  
 import java.io.File;
 35  
 import java.io.IOException;
 36  
 import java.util.ArrayList;
 37  
 import java.util.Arrays;
 38  
 import java.util.Iterator;
 39  
 import java.util.List;
 40  
 
 41  
 /**
 42  
  * @version $Id: AddFileSetsTask.java 999624 2010-09-21 20:40:03Z jdcasey $
 43  
  */
 44  
 public class AddFileSetsTask
 45  
     implements ArchiverTask
 46  
 {
 47  
 
 48  
     private final List<FileSet> fileSets;
 49  
 
 50  
     private Logger logger;
 51  
 
 52  
     private MavenProject project;
 53  
 
 54  
     private MavenProject moduleProject;
 55  
 
 56  
     public AddFileSetsTask( final List<FileSet> fileSets )
 57  33
     {
 58  33
         this.fileSets = fileSets;
 59  33
     }
 60  
 
 61  
     public AddFileSetsTask( final FileSet... fileSets )
 62  0
     {
 63  0
         this.fileSets = new ArrayList<FileSet>( Arrays.asList( fileSets ) );
 64  0
     }
 65  
 
 66  
     public void execute( final Archiver archiver, final AssemblerConfigurationSource configSource )
 67  
         throws ArchiveCreationException, AssemblyFormattingException
 68  
     {
 69  
         // don't need this check here. it's more efficient here, but the logger is not actually
 70  
         // used until addFileSet(..)...and the check should be there in case someone extends the
 71  
         // class.
 72  
         // checkLogger();
 73  
 
 74  12
         final File archiveBaseDir = configSource.getArchiveBaseDirectory();
 75  
 
 76  12
         if ( archiveBaseDir != null )
 77  
         {
 78  12
             if ( !archiveBaseDir.exists() )
 79  
             {
 80  3
                 throw new ArchiveCreationException( "The archive base directory '" + archiveBaseDir.getAbsolutePath()
 81  
                                 + "' does not exist" );
 82  
             }
 83  9
             else if ( !archiveBaseDir.isDirectory() )
 84  
             {
 85  3
                 throw new ArchiveCreationException( "The archive base directory '" + archiveBaseDir.getAbsolutePath()
 86  
                                 + "' exists, but it is not a directory" );
 87  
             }
 88  
         }
 89  
 
 90  6
         for ( final Iterator<FileSet> i = fileSets.iterator(); i.hasNext(); )
 91  
         {
 92  6
             final FileSet fileSet = i.next();
 93  
 
 94  6
             addFileSet( fileSet, archiver, configSource, archiveBaseDir );
 95  6
         }
 96  6
     }
 97  
 
 98  
     protected void addFileSet( final FileSet fileSet, final Archiver archiver,
 99  
                                final AssemblerConfigurationSource configSource, final File archiveBaseDir )
 100  
         throws AssemblyFormattingException, ArchiveCreationException
 101  
     {
 102  
         // throw this check in just in case someone extends this class...
 103  15
         checkLogger();
 104  
 
 105  15
         final FileSetFormatter fileSetFormatter = new FileSetFormatter( configSource, logger );
 106  
 
 107  15
         if ( project == null )
 108  
         {
 109  3
             project = configSource.getProject();
 110  
         }
 111  
 
 112  15
         final File basedir = project.getBasedir();
 113  
 
 114  15
         String destDirectory = fileSet.getOutputDirectory();
 115  
 
 116  15
         if ( destDirectory == null )
 117  
         {
 118  6
             destDirectory = fileSet.getDirectory();
 119  
         }
 120  
 
 121  15
         destDirectory =
 122  
             AssemblyFormatUtils.getOutputDirectory( destDirectory, configSource.getProject(), moduleProject, project,
 123  
                                                     configSource.getFinalName(), configSource );
 124  
 
 125  15
         if ( logger.isDebugEnabled() )
 126  
         {
 127  15
             logger.debug( "FileSet[" + destDirectory + "]" + " dir perms: "
 128  
                             + Integer.toString( archiver.getOverrideDirectoryMode(), 8 ) + " file perms: "
 129  
                             + Integer.toString( archiver.getOverrideFileMode(), 8 )
 130  
                             + ( fileSet.getLineEnding() == null ? "" : " lineEndings: " + fileSet.getLineEnding() ) );
 131  
         }
 132  
 
 133  15
         logger.debug( "The archive base directory is '" + archiveBaseDir + "'" );
 134  
 
 135  15
         File fileSetDir = getFileSetDirectory( fileSet, basedir, archiveBaseDir );
 136  
 
 137  15
         if ( fileSetDir.exists() )
 138  
         {
 139  
             try
 140  
             {
 141  6
                 fileSetDir = fileSetFormatter.formatFileSetForAssembly( fileSetDir, fileSet );
 142  
             }
 143  0
             catch ( final IOException e )
 144  
             {
 145  0
                 throw new ArchiveCreationException( "Error fixing file-set line endings for assembly: "
 146  
                                 + e.getMessage(), e );
 147  6
             }
 148  
 
 149  6
             logger.debug( "Adding file-set from directory: '" + fileSetDir.getAbsolutePath()
 150  
                             + "'\nassembly output directory is: \'" + destDirectory + "\'" );
 151  
 
 152  6
             final AddDirectoryTask task = new AddDirectoryTask( fileSetDir );
 153  
 
 154  6
             final int dirMode = TypeConversionUtils.modeToInt( fileSet.getDirectoryMode(), logger );
 155  6
             if ( dirMode != -1 )
 156  
             {
 157  0
                 task.setDirectoryMode( dirMode );
 158  
             }
 159  
 
 160  6
             final int fileMode = TypeConversionUtils.modeToInt( fileSet.getFileMode(), logger );
 161  6
             if ( fileMode != -1 )
 162  
             {
 163  0
                 task.setFileMode( fileMode );
 164  
             }
 165  
 
 166  6
             task.setUseDefaultExcludes( fileSet.isUseDefaultExcludes() );
 167  
 
 168  6
             final List<String> excludes = fileSet.getExcludes();
 169  6
             excludes.add( "**/*.filtered" );
 170  6
             excludes.add( "**/*.formatted" );
 171  6
             task.setExcludes( excludes );
 172  
 
 173  6
             task.setIncludes( fileSet.getIncludes() );
 174  6
             task.setOutputDirectory( destDirectory );
 175  
 
 176  6
             task.execute( archiver, configSource );
 177  
         }
 178  15
     }
 179  
 
 180  
     protected File getFileSetDirectory( final FileSet fileSet, final File basedir, final File archiveBaseDir )
 181  
         throws ArchiveCreationException, AssemblyFormattingException
 182  
     {
 183  27
         String sourceDirectory = fileSet.getDirectory();
 184  
 
 185  27
         if ( sourceDirectory == null || sourceDirectory.trim()
 186  
                                                        .length() < 1 )
 187  
         {
 188  3
             sourceDirectory = basedir.getAbsolutePath();
 189  
         }
 190  
 
 191  27
         File fileSetDir = null;
 192  
 
 193  27
         if ( archiveBaseDir == null )
 194  
         {
 195  9
             fileSetDir = new File( sourceDirectory );
 196  
 
 197  9
             if ( !fileSetDir.isAbsolute() )
 198  
             {
 199  3
                 fileSetDir = new File( basedir, sourceDirectory );
 200  
             }
 201  
         }
 202  
         else
 203  
         {
 204  18
             fileSetDir = new File( archiveBaseDir, sourceDirectory );
 205  
         }
 206  
 
 207  27
         return fileSetDir;
 208  
     }
 209  
 
 210  
     private void checkLogger()
 211  
     {
 212  15
         if ( logger == null )
 213  
         {
 214  0
             logger = new ConsoleLogger( Logger.LEVEL_INFO, "AddFileSetsTask-internal" );
 215  
         }
 216  15
     }
 217  
 
 218  
     public void setLogger( final Logger logger )
 219  
     {
 220  15
         this.logger = logger;
 221  15
     }
 222  
 
 223  
     public void setProject( final MavenProject project )
 224  
     {
 225  12
         this.project = project;
 226  12
     }
 227  
 
 228  
     public MavenProject getModuleProject()
 229  
     {
 230  0
         return moduleProject;
 231  
     }
 232  
 
 233  
     public void setModuleProject( final MavenProject moduleProject )
 234  
     {
 235  3
         this.moduleProject = moduleProject;
 236  3
     }
 237  
 
 238  
 }