View Javadoc
1   package org.apache.maven.plugins.assembly.archive.task;
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.plugins.assembly.AssemblerConfigurationSource;
23  import org.apache.maven.plugins.assembly.archive.ArchiveCreationException;
24  import org.apache.maven.plugins.assembly.format.AssemblyFormattingException;
25  import org.apache.maven.plugins.assembly.format.ReaderFormatter;
26  import org.apache.maven.plugins.assembly.model.FileSet;
27  import org.apache.maven.plugins.assembly.utils.AssemblyFormatUtils;
28  import org.apache.maven.plugins.assembly.utils.TypeConversionUtils;
29  import org.apache.maven.project.MavenProject;
30  import org.codehaus.plexus.archiver.Archiver;
31  import org.codehaus.plexus.components.io.functions.InputStreamTransformer;
32  import org.codehaus.plexus.logging.Logger;
33  import org.codehaus.plexus.logging.console.ConsoleLogger;
34  
35  import java.io.File;
36  import java.util.ArrayList;
37  import java.util.Arrays;
38  import java.util.List;
39  
40  /**
41   * @version $Id: AddFileSetsTask.html 1016737 2017-08-13 12:01:54Z khmarbaise $
42   */
43  public class AddFileSetsTask
44  {
45  
46      private final List<FileSet> fileSets;
47  
48      private Logger logger;
49  
50      private MavenProject project;
51  
52      private MavenProject moduleProject;
53  
54      public AddFileSetsTask( final List<FileSet> fileSets )
55      {
56          this.fileSets = fileSets;
57      }
58  
59      public AddFileSetsTask( final FileSet... fileSets )
60      {
61          this.fileSets = new ArrayList<FileSet>( Arrays.asList( fileSets ) );
62      }
63  
64      public void execute( final Archiver archiver, final AssemblerConfigurationSource configSource )
65          throws ArchiveCreationException, AssemblyFormattingException
66      {
67          // don't need this check here. it's more efficient here, but the logger is not actually
68          // used until addFileSet(..)...and the check should be there in case someone extends the
69          // class.
70          // checkLogger();
71  
72          final File archiveBaseDir = configSource.getArchiveBaseDirectory();
73  
74          if ( archiveBaseDir != null )
75          {
76              if ( !archiveBaseDir.exists() )
77              {
78                  throw new ArchiveCreationException(
79                      "The archive base directory '" + archiveBaseDir.getAbsolutePath() + "' does not exist" );
80              }
81              else if ( !archiveBaseDir.isDirectory() )
82              {
83                  throw new ArchiveCreationException( "The archive base directory '" + archiveBaseDir.getAbsolutePath()
84                                                          + "' exists, but it is not a directory" );
85              }
86          }
87  
88          for ( final FileSet fileSet : fileSets )
89          {
90              addFileSet( fileSet, archiver, configSource, archiveBaseDir );
91          }
92      }
93  
94      void addFileSet( final FileSet fileSet, final Archiver archiver, final AssemblerConfigurationSource configSource,
95                       final File archiveBaseDir )
96          throws AssemblyFormattingException, ArchiveCreationException
97      {
98          // throw this check in just in case someone extends this class...
99          checkLogger();
100 
101         if ( project == null )
102         {
103             project = configSource.getProject();
104         }
105 
106         final File basedir = project.getBasedir();
107 
108         String destDirectory = fileSet.getOutputDirectory();
109 
110         if ( destDirectory == null )
111         {
112             destDirectory = fileSet.getDirectory();
113         }
114 
115         AssemblyFormatUtils.warnForPlatformSpecifics( logger, destDirectory );
116 
117         destDirectory =
118             AssemblyFormatUtils.getOutputDirectory( destDirectory, configSource.getFinalName(), configSource,
119                                                     AssemblyFormatUtils.moduleProjectInterpolator( moduleProject ),
120                                                     AssemblyFormatUtils.artifactProjectInterpolator( project ) );
121 
122         if ( logger.isDebugEnabled() )
123         {
124             logger.debug( "FileSet[" + destDirectory + "]" + " dir perms: " + Integer.toString(
125                 archiver.getOverrideDirectoryMode(), 8 ) + " file perms: " + Integer.toString(
126                 archiver.getOverrideFileMode(), 8 ) + ( fileSet.getLineEnding() == null
127                 ? ""
128                 : " lineEndings: " + fileSet.getLineEnding() ) );
129         }
130 
131         logger.debug( "The archive base directory is '" + archiveBaseDir + "'" );
132 
133         File fileSetDir = getFileSetDirectory( fileSet, basedir, archiveBaseDir );
134 
135         if ( fileSetDir.exists() )
136         {
137             InputStreamTransformer fileSetTransformers =
138                 ReaderFormatter.getFileSetTransformers( configSource, fileSet.isFiltered(), fileSet.getLineEnding() );
139             if ( fileSetTransformers == null )
140             {
141                 logger.debug( "NOT reformatting any files in " + fileSetDir );
142             }
143 
144             if ( fileSetDir.getPath().equals( File.separator ) )
145             {
146                 throw new AssemblyFormattingException(
147                     "Your assembly descriptor specifies a directory of " + File.separator
148                         + ", which is your *entire* file system.\nThese are not the files you are looking for" );
149             }
150             final AddDirectoryTask task = new AddDirectoryTask( fileSetDir, fileSetTransformers );
151 
152             final int dirMode = TypeConversionUtils.modeToInt( fileSet.getDirectoryMode(), logger );
153             if ( dirMode != -1 )
154             {
155                 task.setDirectoryMode( dirMode );
156             }
157 
158             final int fileMode = TypeConversionUtils.modeToInt( fileSet.getFileMode(), logger );
159             if ( fileMode != -1 )
160             {
161                 task.setFileMode( fileMode );
162             }
163 
164             task.setUseDefaultExcludes( fileSet.isUseDefaultExcludes() );
165             task.setExcludes( fileSet.getExcludes() );
166             task.setIncludes( fileSet.getIncludes() );
167             task.setOutputDirectory( destDirectory );
168 
169             task.execute( archiver );
170         }
171     }
172 
173     File getFileSetDirectory( final FileSet fileSet, final File basedir, final File archiveBaseDir )
174         throws ArchiveCreationException, AssemblyFormattingException
175     {
176         String sourceDirectory = fileSet.getDirectory();
177 
178         if ( sourceDirectory == null || sourceDirectory.trim().length() < 1 )
179         {
180             sourceDirectory = basedir.getAbsolutePath();
181         }
182 
183         File fileSetDir;
184 
185         if ( archiveBaseDir == null )
186         {
187             fileSetDir = new File( sourceDirectory );
188 
189             if ( !fileSetDir.isAbsolute() )
190             {
191                 fileSetDir = new File( basedir, sourceDirectory );
192             }
193         }
194         else
195         {
196             fileSetDir = new File( archiveBaseDir, sourceDirectory );
197         }
198 
199         return fileSetDir;
200     }
201 
202     private void checkLogger()
203     {
204         if ( logger == null )
205         {
206             logger = new ConsoleLogger( Logger.LEVEL_INFO, "AddFileSetsTask-internal" );
207         }
208     }
209 
210     public void setLogger( final Logger logger )
211     {
212         this.logger = logger;
213     }
214 
215     public void setProject( final MavenProject project )
216     {
217         this.project = project;
218     }
219 
220     public void setModuleProject( final MavenProject moduleProject )
221     {
222         this.moduleProject = moduleProject;
223     }
224 
225 }