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 1675144 2015-04-21 16:05:16Z 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         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 
166             final List<String> excludes = fileSet.getExcludes();
167             excludes.add( "**/*.filtered" );
168             excludes.add( "**/*.formatted" );
169             task.setExcludes( excludes );
170 
171             task.setIncludes( fileSet.getIncludes() );
172             task.setOutputDirectory( destDirectory );
173 
174             task.execute( archiver );
175         }
176     }
177 
178     File getFileSetDirectory( final FileSet fileSet, final File basedir, final File archiveBaseDir )
179         throws ArchiveCreationException, AssemblyFormattingException
180     {
181         String sourceDirectory = fileSet.getDirectory();
182 
183         if ( sourceDirectory == null || sourceDirectory.trim().length() < 1 )
184         {
185             sourceDirectory = basedir.getAbsolutePath();
186         }
187 
188         File fileSetDir;
189 
190         if ( archiveBaseDir == null )
191         {
192             fileSetDir = new File( sourceDirectory );
193 
194             if ( !fileSetDir.isAbsolute() )
195             {
196                 fileSetDir = new File( basedir, sourceDirectory );
197             }
198         }
199         else
200         {
201             fileSetDir = new File( archiveBaseDir, sourceDirectory );
202         }
203 
204         return fileSetDir;
205     }
206 
207     private void checkLogger()
208     {
209         if ( logger == null )
210         {
211             logger = new ConsoleLogger( Logger.LEVEL_INFO, "AddFileSetsTask-internal" );
212         }
213     }
214 
215     public void setLogger( final Logger logger )
216     {
217         this.logger = logger;
218     }
219 
220     public void setProject( final MavenProject project )
221     {
222         this.project = project;
223     }
224 
225     public void setModuleProject( final MavenProject moduleProject )
226     {
227         this.moduleProject = moduleProject;
228     }
229 
230 }