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 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  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          final List<FileItem> fileList = assembly.getFiles();
58          final File basedir = configSource.getBasedir();
59  
60          final FileFormatter fileFormatter = new FileFormatter( configSource, getLogger() );
61          for ( final Iterator<FileItem> i = fileList.iterator(); i.hasNext(); )
62          {
63              final FileItem fileItem = i.next();
64  
65              final String sourcePath = fileItem.getSource();
66  
67              // ensure source file is in absolute path for reactor build to work
68              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              final String sourceName = source.getName();
73  
74              if ( !source.isAbsolute() )
75              {
76                  source = new File( basedir, sourcePath );
77              }
78  
79              source = fileFormatter.format( source, fileItem.isFiltered(), fileItem.getLineEnding() );
80  
81              String destName = fileItem.getDestName();
82  
83              if ( destName == null )
84              {
85                  destName = sourceName;
86              }
87  
88              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              if ( outputDirectory.endsWith( "/" ) || outputDirectory.endsWith( "\\" ) )
96              {
97                  target = outputDirectory + destName;
98              }
99              else if ( outputDirectory.length() < 1 )
100             {
101                 target = destName;
102             }
103             else
104             {
105                 target = outputDirectory + "/" + destName;
106             }
107 
108             try
109             {
110                 archiver.addFile( source, target, TypeConversionUtils.modeToInt( fileItem.getFileMode(), getLogger() ) );
111             }
112             catch ( final ArchiverException e )
113             {
114                 throw new ArchiveCreationException( "Error adding file to archive: " + e.getMessage(), e );
115             }
116         }
117     }
118 
119 }