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