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