View Javadoc
1   package org.apache.maven.plugin.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.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.FileSet;
27  import org.apache.maven.plugin.assembly.utils.AssemblyFormatUtils;
28  import org.apache.maven.plugin.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.java 1639422 2014-11-13 18:08:07Z krosenvold $
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         destDirectory =
116             AssemblyFormatUtils.getOutputDirectory( destDirectory, configSource.getFinalName(), configSource,
117                                                     AssemblyFormatUtils.moduleProjectInterpolator( moduleProject ),
118                                                     AssemblyFormatUtils.artifactProjectInterpolator( project ) );
119 
120 
121 /*
122         destDirectory =
123             AssemblyFormatUtils.getOutputDirectory( destDirectory, moduleProjectInterpolator(
124                 moduleProject ), artifactProjectInterpolator( project ),
125                                                     finalNameInterpolator( configSource.getFinalName() ),
126                                                     createProjectInterpolator( configSource.getProject() ),
127                                                     configSource.getSessionExecutionUserInterpolator() );
128 
129  */
130         if ( logger.isDebugEnabled() )
131         {
132             logger.debug( "FileSet[" + destDirectory + "]" + " dir perms: " + Integer.toString(
133                 archiver.getOverrideDirectoryMode(), 8 ) + " file perms: " + Integer.toString(
134                 archiver.getOverrideFileMode(), 8 ) + ( fileSet.getLineEnding() == null
135                 ? ""
136                 : " lineEndings: " + fileSet.getLineEnding() ) );
137         }
138 
139         logger.debug( "The archive base directory is '" + archiveBaseDir + "'" );
140 
141         File fileSetDir = getFileSetDirectory( fileSet, basedir, archiveBaseDir );
142 
143         if ( fileSetDir.exists() )
144         {
145             InputStreamTransformer fileSetTransformers =
146                 ReaderFormatter.getFileSetTransformers( configSource, fileSet.isFiltered(), fileSet.getLineEnding() );
147             if ( fileSetTransformers == null )
148             {
149                 logger.debug( "NOT reformatting any files in " + fileSetDir );
150             }
151 
152             if ( fileSetDir.getPath().equals( File.separator ) )
153             {
154                 throw new AssemblyFormattingException(
155                     "Your assembly descriptor specifies a directory of " + File.separator
156                         + ", which is your *entire* file system.\nThese are not the files you are looking for" );
157             }
158             final AddDirectoryTask task = new AddDirectoryTask( fileSetDir, fileSetTransformers );
159 
160             final int dirMode = TypeConversionUtils.modeToInt( fileSet.getDirectoryMode(), logger );
161             if ( dirMode != -1 )
162             {
163                 task.setDirectoryMode( dirMode );
164             }
165 
166             final int fileMode = TypeConversionUtils.modeToInt( fileSet.getFileMode(), logger );
167             if ( fileMode != -1 )
168             {
169                 task.setFileMode( fileMode );
170             }
171 
172             task.setUseDefaultExcludes( fileSet.isUseDefaultExcludes() );
173 
174             final List<String> excludes = fileSet.getExcludes();
175             excludes.add( "**/*.filtered" );
176             excludes.add( "**/*.formatted" );
177             task.setExcludes( excludes );
178 
179             task.setIncludes( fileSet.getIncludes() );
180             task.setOutputDirectory( destDirectory );
181 
182             task.execute( archiver );
183         }
184     }
185 
186     File getFileSetDirectory( final FileSet fileSet, final File basedir, final File archiveBaseDir )
187         throws ArchiveCreationException, AssemblyFormattingException
188     {
189         String sourceDirectory = fileSet.getDirectory();
190 
191         if ( sourceDirectory == null || sourceDirectory.trim().length() < 1 )
192         {
193             sourceDirectory = basedir.getAbsolutePath();
194         }
195 
196         File fileSetDir;
197 
198         if ( archiveBaseDir == null )
199         {
200             fileSetDir = new File( sourceDirectory );
201 
202             if ( !fileSetDir.isAbsolute() )
203             {
204                 fileSetDir = new File( basedir, sourceDirectory );
205             }
206         }
207         else
208         {
209             fileSetDir = new File( archiveBaseDir, sourceDirectory );
210         }
211 
212         return fileSetDir;
213     }
214 
215     private void checkLogger()
216     {
217         if ( logger == null )
218         {
219             logger = new ConsoleLogger( Logger.LEVEL_INFO, "AddFileSetsTask-internal" );
220         }
221     }
222 
223     public void setLogger( final Logger logger )
224     {
225         this.logger = logger;
226     }
227 
228     public void setProject( final MavenProject project )
229     {
230         this.project = project;
231     }
232 
233     public void setModuleProject( final MavenProject moduleProject )
234     {
235         this.moduleProject = moduleProject;
236     }
237 
238 }