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 1517906 2013-08-27 18:25:03Z krosenvold $
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 FileItem fileItem : fileList) {
63              final String sourcePath = fileItem.getSource();
64  
65              // ensure source file is in absolute path for reactor build to work
66              File source = new File(sourcePath);
67  
68              // save the original sourcefile's name, because filtration may
69              // create a temp file with a different name.
70              final String sourceName = source.getName();
71  
72              if (!source.isAbsolute()) {
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                  destName = sourceName;
84              }
85  
86              final String outputDirectory =
87                      AssemblyFormatUtils.getOutputDirectory(fileItem.getOutputDirectory(), configSource.getProject(), null,
88                              configSource.getFinalName(), configSource);
89  
90              String target;
91  
92              // omit the last char if ends with / or \\
93              if (outputDirectory.endsWith("/") || outputDirectory.endsWith("\\")) {
94                  target = outputDirectory + destName;
95              } else if (outputDirectory.length() < 1) {
96                  target = destName;
97              } else {
98                  target = outputDirectory + "/" + destName;
99              }
100 
101             try {
102                 archiver.addFile(source, target, TypeConversionUtils.modeToInt(fileItem.getFileMode(), getLogger()));
103             } catch (final ArchiverException e) {
104                 throw new ArchiveCreationException("Error adding file to archive: " + e.getMessage(), e);
105             }
106         }
107     }
108 
109 }