Coverage Report - org.apache.maven.plugin.assembly.archive.phase.FileItemAssemblyPhase
 
Classes in this File Line Coverage Branch Coverage Complexity
FileItemAssemblyPhase
89%
24/27
83%
10/12
0
 
 1  
 package org.apache.maven.plugin.assembly.archive.phase;
 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.AssemblyContext;
 24  
 import org.apache.maven.plugin.assembly.archive.ArchiveCreationException;
 25  
 import org.apache.maven.plugin.assembly.format.AssemblyFormattingException;
 26  
 import org.apache.maven.plugin.assembly.format.FileFormatter;
 27  
 import org.apache.maven.plugin.assembly.model.Assembly;
 28  
 import org.apache.maven.plugin.assembly.model.FileItem;
 29  
 import org.apache.maven.plugin.assembly.utils.AssemblyFormatUtils;
 30  
 import org.apache.maven.plugin.assembly.utils.TypeConversionUtils;
 31  
 import org.codehaus.plexus.archiver.Archiver;
 32  
 import org.codehaus.plexus.archiver.ArchiverException;
 33  
 import org.codehaus.plexus.logging.AbstractLogEnabled;
 34  
 
 35  
 import java.io.File;
 36  
 import java.util.Iterator;
 37  
 import java.util.List;
 38  
 
 39  
 /**
 40  
  * Handles the top-level <files/> section of the assembly descriptor.
 41  
  * 
 42  
  * @version $Id: FileItemAssemblyPhase.java 1002307 2010-09-28 18:13:44Z jdcasey $
 43  
  * @plexus.component role="org.apache.maven.plugin.assembly.archive.phase.AssemblyArchiverPhase" role-hint="file-items"
 44  
  */
 45  6
 public class FileItemAssemblyPhase
 46  
     extends AbstractLogEnabled
 47  
     implements AssemblyArchiverPhase
 48  
 {
 49  
 
 50  
     /**
 51  
      * {@inheritDoc}
 52  
      */
 53  
     public void execute( final Assembly assembly, final Archiver archiver,
 54  
                          final AssemblerConfigurationSource configSource, final AssemblyContext context )
 55  
         throws ArchiveCreationException, AssemblyFormattingException
 56  
     {
 57  6
         final List<FileItem> fileList = assembly.getFiles();
 58  6
         final File basedir = configSource.getBasedir();
 59  
 
 60  6
         final FileFormatter fileFormatter = new FileFormatter( configSource, getLogger() );
 61  6
         for ( final Iterator<FileItem> i = fileList.iterator(); i.hasNext(); )
 62  
         {
 63  11
             final FileItem fileItem = i.next();
 64  
 
 65  11
             final String sourcePath = fileItem.getSource();
 66  
 
 67  
             // ensure source file is in absolute path for reactor build to work
 68  11
             File source = new File( sourcePath );
 69  
 
 70  
             // save the original sourcefile's name, because filtration may
 71  
             // create a temp file with a different name.
 72  11
             final String sourceName = source.getName();
 73  
 
 74  11
             if ( !source.isAbsolute() )
 75  
             {
 76  10
                 source = new File( basedir, sourcePath );
 77  
             }
 78  
 
 79  11
             source = fileFormatter.format( source, fileItem.isFiltered(), fileItem.getLineEnding() );
 80  
 
 81  11
             String destName = fileItem.getDestName();
 82  
 
 83  11
             if ( destName == null )
 84  
             {
 85  5
                 destName = sourceName;
 86  
             }
 87  
 
 88  11
             final String outputDirectory =
 89  
                 AssemblyFormatUtils.getOutputDirectory( fileItem.getOutputDirectory(), configSource.getProject(), null,
 90  
                                                         configSource.getFinalName(), configSource );
 91  
 
 92  
             String target;
 93  
 
 94  
             // omit the last char if ends with / or \\
 95  11
             if ( outputDirectory.endsWith( "/" ) || outputDirectory.endsWith( "\\" ) )
 96  
             {
 97  3
                 target = outputDirectory + destName;
 98  
             }
 99  8
             else if ( outputDirectory.length() < 1 )
 100  
             {
 101  8
                 target = destName;
 102  
             }
 103  
             else
 104  
             {
 105  0
                 target = outputDirectory + "/" + destName;
 106  
             }
 107  
 
 108  
             try
 109  
             {
 110  11
                 archiver.addFile( source, target, TypeConversionUtils.modeToInt( fileItem.getFileMode(), getLogger() ) );
 111  
             }
 112  0
             catch ( final ArchiverException e )
 113  
             {
 114  0
                 throw new ArchiveCreationException( "Error adding file to archive: " + e.getMessage(), e );
 115  11
             }
 116  11
         }
 117  6
     }
 118  
 
 119  
 }