Coverage Report - org.apache.maven.plugin.assembly.archive.task.AddArtifactTask
 
Classes in this File Line Coverage Branch Coverage Complexity
AddArtifactTask
80%
74/92
76%
26/34
2,438
 
 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.artifact.Artifact;
 23  
 import org.apache.maven.plugin.assembly.AssemblerConfigurationSource;
 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.utils.AssemblyFormatUtils;
 27  
 import org.apache.maven.plugin.assembly.utils.TypeConversionUtils;
 28  
 import org.apache.maven.project.MavenProject;
 29  
 import org.codehaus.plexus.archiver.Archiver;
 30  
 import org.codehaus.plexus.archiver.ArchiverException;
 31  
 import org.codehaus.plexus.logging.Logger;
 32  
 import org.codehaus.plexus.util.FileUtils;
 33  
 import org.codehaus.plexus.util.StringUtils;
 34  
 
 35  
 import java.io.File;
 36  
 import java.io.IOException;
 37  
 import java.util.List;
 38  
 
 39  
 /**
 40  
  * @version $Id: AddArtifactTask.java 999612 2010-09-21 20:34:50Z jdcasey $
 41  
  */
 42  
 public class AddArtifactTask
 43  
     implements ArchiverTask
 44  
 {
 45  
 
 46  3
     public static final String[] DEFAULT_INCLUDES_ARRAY = { "**/*" };
 47  
 
 48  54
     private int directoryMode = -1;
 49  
 
 50  54
     private int fileMode = -1;
 51  
 
 52  54
     private boolean unpack = false;
 53  
 
 54  
     private List<String> includes;
 55  
 
 56  
     private List<String> excludes;
 57  
 
 58  
     private final Artifact artifact;
 59  
 
 60  
     private MavenProject project;
 61  
 
 62  
     private MavenProject moduleProject;
 63  
 
 64  
     private Artifact moduleArtifact;
 65  
 
 66  
     private String outputDirectory;
 67  
 
 68  
     private String outputFileNameMapping;
 69  
 
 70  
     private final Logger logger;
 71  
 
 72  
     public AddArtifactTask( final Artifact artifact, final Logger logger )
 73  54
     {
 74  54
         this.artifact = artifact;
 75  54
         this.logger = logger;
 76  54
     }
 77  
 
 78  
     public void execute( final Archiver archiver, final AssemblerConfigurationSource configSource )
 79  
         throws ArchiveCreationException, AssemblyFormattingException
 80  
     {
 81  
         // MASSEMBLY-282: We should support adding a project's standard output file as part of an assembly that replaces
 82  
         // it.
 83  54
         if ( ( ( artifact.getFile() != null ) && ( archiver.getDestFile() != null ) )
 84  
                         && artifact.getFile()
 85  
                                    .equals( archiver.getDestFile() ) )
 86  
         {
 87  0
             final File tempRoot = configSource.getTemporaryRootDirectory();
 88  0
             final File tempArtifactFile = new File( tempRoot, artifact.getFile()
 89  
                                                                       .getName() );
 90  
 
 91  0
             logger.warn( "Artifact: "
 92  
                             + artifact.getId()
 93  
                             + " references the same file as the assembly destination file. Moving it to a temporary location for inclusion." );
 94  
             try
 95  
             {
 96  0
                 FileUtils.copyFile( artifact.getFile(), tempArtifactFile );
 97  
             }
 98  0
             catch ( final IOException e )
 99  
             {
 100  0
                 throw new ArchiveCreationException( "Error moving artifact file: '" + artifact.getFile()
 101  
                                 + "' to temporary location: " + tempArtifactFile + ". Reason: " + e.getMessage(), e );
 102  0
             }
 103  
 
 104  0
             artifact.setFile( tempArtifactFile );
 105  
         }
 106  
 
 107  54
         String destDirectory = outputDirectory;
 108  
 
 109  54
         destDirectory =
 110  
             AssemblyFormatUtils.getOutputDirectory( destDirectory, configSource.getProject(), moduleProject, project,
 111  
                                                     configSource.getFinalName(), configSource );
 112  
 
 113  54
         if ( unpack )
 114  
         {
 115  12
             String outputLocation = destDirectory;
 116  
 
 117  12
             if ( ( outputLocation.length() > 0 ) && !outputLocation.endsWith( "/" ) )
 118  
             {
 119  0
                 outputLocation += "/";
 120  
             }
 121  
 
 122  12
             String[] includesArray = TypeConversionUtils.toStringArray( includes );
 123  12
             if ( includesArray == null )
 124  
             {
 125  9
                 includesArray = DEFAULT_INCLUDES_ARRAY;
 126  
             }
 127  12
             final String[] excludesArray = TypeConversionUtils.toStringArray( excludes );
 128  
 
 129  12
             final int oldDirMode = archiver.getOverrideDirectoryMode();
 130  12
             final int oldFileMode = archiver.getOverrideFileMode();
 131  
 
 132  12
             logger.debug( "Unpacking artifact: " + artifact.getId() + " to assembly location: " + outputLocation + "." );
 133  
 
 134  12
             boolean fileModeSet = false;
 135  12
             boolean dirModeSet = false;
 136  
 
 137  
             try
 138  
             {
 139  12
                 if ( fileMode != -1 )
 140  
                 {
 141  6
                     archiver.setFileMode( fileMode );
 142  6
                     fileModeSet = true;
 143  
                 }
 144  
 
 145  12
                 if ( directoryMode != -1 )
 146  
                 {
 147  6
                     archiver.setDirectoryMode( directoryMode );
 148  6
                     dirModeSet = true;
 149  
                 }
 150  
 
 151  12
                 final File artifactFile = artifact.getFile();
 152  12
                 if ( artifactFile == null )
 153  
                 {
 154  0
                     logger.warn( "Skipping artifact: " + artifact.getId()
 155  
                                     + "; it does not have an associated file or directory." );
 156  
                 }
 157  12
                 else if ( artifactFile.isDirectory() )
 158  
                 {
 159  0
                     logger.debug( "Adding artifact directory contents for: " + artifact + " to: " + outputLocation );
 160  0
                     archiver.addDirectory( artifactFile, outputLocation, includesArray, excludesArray );
 161  
                 }
 162  
                 else
 163  
                 {
 164  12
                     logger.debug( "Unpacking artifact contents for: " + artifact + " to: " + outputLocation );
 165  12
                     logger.debug( "includes:\n" + StringUtils.join( includesArray, "\n" ) + "\n" );
 166  12
                     logger.debug( "excludes:\n"
 167  
                                     + ( excludesArray == null ? "none" : StringUtils.join( excludesArray, "\n" ) )
 168  
                                     + "\n" );
 169  12
                     archiver.addArchivedFileSet( artifactFile, outputLocation, includesArray, excludesArray );
 170  
                 }
 171  
             }
 172  0
             catch ( final ArchiverException e )
 173  
             {
 174  0
                 throw new ArchiveCreationException( "Error adding file-set for '" + artifact.getId() + "' to archive: "
 175  
                                 + e.getMessage(), e );
 176  
             }
 177  
             finally
 178  
             {
 179  12
                 if ( dirModeSet )
 180  
                 {
 181  6
                     archiver.setDirectoryMode( oldDirMode );
 182  
                 }
 183  
 
 184  12
                 if ( fileModeSet )
 185  
                 {
 186  6
                     archiver.setFileMode( oldFileMode );
 187  
                 }
 188  
             }
 189  12
         }
 190  
         else
 191  
         {
 192  42
             final String fileNameMapping =
 193  
                 AssemblyFormatUtils.evaluateFileNameMapping( outputFileNameMapping, artifact,
 194  
                                                              configSource.getProject(), moduleProject, moduleArtifact,
 195  
                                                              project, configSource );
 196  
 
 197  42
             final String outputLocation = destDirectory + fileNameMapping;
 198  
 
 199  
             try
 200  
             {
 201  42
                 final File artifactFile = artifact.getFile();
 202  
 
 203  42
                 logger.debug( "Adding artifact: " + artifact.getId() + " with file: " + artifactFile
 204  
                                 + " to assembly location: " + outputLocation + "." );
 205  
 
 206  42
                 if ( fileMode != -1 )
 207  
                 {
 208  33
                     archiver.addFile( artifactFile, outputLocation, fileMode );
 209  
                 }
 210  
                 else
 211  
                 {
 212  9
                     archiver.addFile( artifactFile, outputLocation );
 213  
                 }
 214  
             }
 215  0
             catch ( final ArchiverException e )
 216  
             {
 217  0
                 throw new ArchiveCreationException( "Error adding file '" + artifact.getId() + "' to archive: "
 218  
                                 + e.getMessage(), e );
 219  42
             }
 220  
         }
 221  54
     }
 222  
 
 223  
     public void setDirectoryMode( final int directoryMode )
 224  
     {
 225  12
         this.directoryMode = directoryMode;
 226  12
     }
 227  
 
 228  
     public void setFileMode( final int fileMode )
 229  
     {
 230  39
         this.fileMode = fileMode;
 231  39
     }
 232  
 
 233  
     public void setExcludes( final List<String> excludes )
 234  
     {
 235  3
         this.excludes = excludes;
 236  3
     }
 237  
 
 238  
     public void setIncludes( final List<String> includes )
 239  
     {
 240  3
         this.includes = includes;
 241  3
     }
 242  
 
 243  
     public void setUnpack( final boolean unpack )
 244  
     {
 245  48
         this.unpack = unpack;
 246  48
     }
 247  
 
 248  
     public void setProject( final MavenProject project )
 249  
     {
 250  42
         this.project = project;
 251  42
     }
 252  
 
 253  
     public void setOutputDirectory( final String outputDirectory )
 254  
     {
 255  42
         this.outputDirectory = outputDirectory;
 256  42
     }
 257  
 
 258  
     public void setFileNameMapping( final String outputFileNameMapping )
 259  
     {
 260  54
         this.outputFileNameMapping = outputFileNameMapping;
 261  54
     }
 262  
 
 263  
     public void setOutputDirectory( final String outputDirectory, final String defaultOutputDirectory )
 264  
     {
 265  21
         setOutputDirectory( outputDirectory == null ? defaultOutputDirectory : outputDirectory );
 266  21
     }
 267  
 
 268  
     public void setFileNameMapping( final String outputFileNameMapping, final String defaultOutputFileNameMapping )
 269  
     {
 270  21
         setFileNameMapping( outputFileNameMapping == null ? defaultOutputFileNameMapping : outputFileNameMapping );
 271  21
     }
 272  
 
 273  
     public MavenProject getModuleProject()
 274  
     {
 275  0
         return moduleProject;
 276  
     }
 277  
 
 278  
     public void setModuleProject( final MavenProject moduleProject )
 279  
     {
 280  39
         this.moduleProject = moduleProject;
 281  39
     }
 282  
 
 283  
     public Artifact getModuleArtifact()
 284  
     {
 285  0
         return moduleArtifact;
 286  
     }
 287  
 
 288  
     public void setModuleArtifact( final Artifact moduleArtifact )
 289  
     {
 290  39
         this.moduleArtifact = moduleArtifact;
 291  39
     }
 292  
 
 293  
 }