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.archive.ArchiveCreationException;
24  import org.apache.maven.plugin.assembly.format.AssemblyFormattingException;
25  import org.apache.maven.plugin.assembly.format.ReaderFormatter;
26  import org.apache.maven.plugin.assembly.model.Assembly;
27  import org.apache.maven.plugin.assembly.model.FileItem;
28  import org.apache.maven.plugin.assembly.utils.AssemblyFormatUtils;
29  import org.apache.maven.plugin.assembly.utils.TypeConversionUtils;
30  import org.codehaus.plexus.archiver.Archiver;
31  import org.codehaus.plexus.archiver.ArchiverException;
32  import org.codehaus.plexus.component.annotations.Component;
33  import org.codehaus.plexus.components.io.functions.InputStreamTransformer;
34  import org.codehaus.plexus.components.io.resources.PlexusIoResource;
35  import org.codehaus.plexus.logging.AbstractLogEnabled;
36  
37  import java.io.File;
38  import java.io.IOException;
39  import java.util.List;
40  
41  import static org.apache.maven.plugin.assembly.utils.AssemblyFormatUtils.warnForPlatformSpecifics;
42  import static org.codehaus.plexus.components.io.resources.ResourceFactory.createResource;
43  
44  /**
45   * Handles the top-level <files/> section of the assembly descriptor.
46   *
47   * @version $Id: FileItemAssemblyPhase.java 1675144 2015-04-21 16:05:16Z krosenvold $
48   */
49  @Component( role = AssemblyArchiverPhase.class, hint = "file-items" )
50  public class FileItemAssemblyPhase
51      extends AbstractLogEnabled
52      implements AssemblyArchiverPhase, PhaseOrder
53  {
54  
55      /**
56       * {@inheritDoc}
57       */
58      public void execute( final Assembly assembly, final Archiver archiver,
59                           final AssemblerConfigurationSource configSource )
60          throws ArchiveCreationException, AssemblyFormattingException
61      {
62          final List<FileItem> fileList = assembly.getFiles();
63          final File basedir = configSource.getBasedir();
64  
65          for ( final FileItem fileItem : fileList )
66          {
67              final String sourcePath = fileItem.getSource();
68  
69              // ensure source file is in absolute path for reactor build to work
70              File source = new File( sourcePath );
71  
72              // save the original sourcefile's name, because filtration may
73              // create a temp file with a different name.
74              final String sourceName = source.getName();
75  
76              if ( !source.isAbsolute() )
77              {
78                  source = new File( basedir, sourcePath );
79              }
80  
81              String destName = fileItem.getDestName();
82  
83              if ( destName == null )
84              {
85                  destName = sourceName;
86              }
87  
88              final String outputDirectory1 = fileItem.getOutputDirectory();
89  
90              warnForPlatformSpecifics( getLogger(), outputDirectory1 );
91  
92              final String outputDirectory =
93                  AssemblyFormatUtils.getOutputDirectory( outputDirectory1, configSource.getFinalName(), configSource,
94                                                          AssemblyFormatUtils.moduleProjectInterpolator(
95                                                              configSource.getProject() ),
96                                                          AssemblyFormatUtils.artifactProjectInterpolator( null ) );
97  
98              String target;
99  
100             // omit the last char if ends with / or \\
101             if ( outputDirectory.endsWith( "/" ) || outputDirectory.endsWith( "\\" ) )
102             {
103                 target = outputDirectory + destName;
104             }
105             else if ( outputDirectory.length() < 1 )
106             {
107                 target = destName;
108             }
109             else
110             {
111                 target = outputDirectory + "/" + destName;
112             }
113 
114             try
115             {
116                 final InputStreamTransformer fileSetTransformers =
117                     ReaderFormatter.getFileSetTransformers( configSource, fileItem.isFiltered(),
118                                                             fileItem.getLineEnding() );
119 
120                 final PlexusIoResource restoUse = createResource( source, fileSetTransformers );
121 
122                 int mode = TypeConversionUtils.modeToInt( fileItem.getFileMode(), getLogger() );
123                 archiver.addResource( restoUse, target, mode );
124             }
125             catch ( final ArchiverException e )
126             {
127                 throw new ArchiveCreationException( "Error adding file to archive: " + e.getMessage(), e );
128             }
129             catch ( IOException e )
130             {
131                 throw new ArchiveCreationException( "Error adding file to archive: " + e.getMessage(), e );
132             }
133         }
134     }
135 
136     public int order()
137     {
138         return 10;
139     }
140 }