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