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.codehaus.plexus.components.io.resources.ResourceFactory.createResource;
42  
43  /**
44   * Handles the top-level <files/> section of the assembly descriptor.
45   *
46   * @version $Id: FileItemAssemblyPhase.java 1642461 2014-11-29 18:26:36Z krosenvold $
47   */
48  @Component( role = AssemblyArchiverPhase.class, hint = "file-items" )
49  public class FileItemAssemblyPhase
50      extends AbstractLogEnabled
51      implements AssemblyArchiverPhase, PhaseOrder
52  {
53  
54      /**
55       * {@inheritDoc}
56       */
57      public void execute( final Assembly assembly, final Archiver archiver,
58                           final AssemblerConfigurationSource configSource )
59          throws ArchiveCreationException, AssemblyFormattingException
60      {
61          final List<FileItem> fileList = assembly.getFiles();
62          final File basedir = configSource.getBasedir();
63  
64          for ( final FileItem fileItem : fileList )
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              String destName = fileItem.getDestName();
81  
82              if ( destName == null )
83              {
84                  destName = sourceName;
85              }
86  
87              final String outputDirectory1 = fileItem.getOutputDirectory();
88  
89              final String outputDirectory =
90                  AssemblyFormatUtils.getOutputDirectory( outputDirectory1, configSource.getFinalName(), configSource,
91                                                          AssemblyFormatUtils.moduleProjectInterpolator(
92                                                              configSource.getProject() ),
93                                                          AssemblyFormatUtils.artifactProjectInterpolator( null ) );
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                 final InputStreamTransformer fileSetTransformers =
114                     ReaderFormatter.getFileSetTransformers( configSource, fileItem.isFiltered(),
115                                                             fileItem.getLineEnding() );
116 
117                 final PlexusIoResource restoUse = createResource( source, fileSetTransformers );
118 
119                 int mode = TypeConversionUtils.modeToInt( fileItem.getFileMode(), getLogger() );
120                 archiver.addResource( restoUse, target, mode );
121             }
122             catch ( final ArchiverException e )
123             {
124                 throw new ArchiveCreationException( "Error adding file to archive: " + e.getMessage(), e );
125             }
126             catch ( IOException e )
127             {
128                 throw new ArchiveCreationException( "Error adding file to archive: " + e.getMessage(), e );
129             }
130         }
131     }
132 
133     public int order()
134     {
135         return 10;
136     }
137 }