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 java.io.File;
23  import java.util.ArrayList;
24  import java.util.Arrays;
25  import java.util.List;
26  
27  import org.apache.maven.plugin.assembly.AssemblerConfigurationSource;
28  import org.apache.maven.plugin.assembly.archive.ArchiveCreationException;
29  import org.apache.maven.plugin.assembly.format.AssemblyFormattingException;
30  import org.apache.maven.plugin.assembly.format.ReaderFormatter;
31  import org.apache.maven.plugin.assembly.model.FileSet;
32  import org.apache.maven.plugin.assembly.utils.AssemblyFormatUtils;
33  import org.apache.maven.plugin.assembly.utils.TypeConversionUtils;
34  import org.apache.maven.project.MavenProject;
35  import org.codehaus.plexus.archiver.Archiver;
36  import org.codehaus.plexus.components.io.functions.InputStreamTransformer;
37  import org.codehaus.plexus.logging.Logger;
38  import org.codehaus.plexus.logging.console.ConsoleLogger;
39  
40  /**
41   * @version $Id: AddFileSetsTask.java 1633832 2014-10-23 15:03:47Z 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( "The archive base directory '" + archiveBaseDir.getAbsolutePath()
79                      + "' 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.getProject(), moduleProject, project,
117                                                     configSource.getFinalName(), configSource );
118 
119         if ( logger.isDebugEnabled() )
120         {
121             logger.debug( "FileSet[" + destDirectory + "]" + " dir perms: "
122                 + Integer.toString( archiver.getOverrideDirectoryMode(), 8 ) + " file perms: "
123                 + Integer.toString( archiver.getOverrideFileMode(), 8 )
124                 + ( fileSet.getLineEnding() == null ? "" : " lineEndings: " + fileSet.getLineEnding() ) );
125         }
126 
127         logger.debug( "The archive base directory is '" + archiveBaseDir + "'" );
128 
129         File fileSetDir = getFileSetDirectory( fileSet, basedir, archiveBaseDir );
130 
131         if ( fileSetDir.exists() )
132         {
133             InputStreamTransformer fileSetTransformers = ReaderFormatter.getFileSetTransformers( configSource, fileSet.isFiltered(),
134                                                                                                  fileSet.getLineEnding() );
135             if (fileSetTransformers == null)
136             {
137                 logger.debug( "NOT reformatting any files in " + fileSetDir );
138             }
139 
140             if ( fileSetDir.getPath().equals( File.separator ) )
141             {
142                 throw new AssemblyFormattingException( "Your assembly descriptor specifies a directory of "
143                     + File.separator
144                     + ", which is your *entire* file system.\nThese are not the files you are looking for" );
145             }
146             final AddDirectoryTask task = new AddDirectoryTask( fileSetDir, fileSetTransformers );
147 
148             final int dirMode = TypeConversionUtils.modeToInt( fileSet.getDirectoryMode(), logger );
149             if ( dirMode != -1 )
150             {
151                 task.setDirectoryMode( dirMode );
152             }
153 
154             final int fileMode = TypeConversionUtils.modeToInt( fileSet.getFileMode(), logger );
155             if ( fileMode != -1 )
156             {
157                 task.setFileMode( fileMode );
158             }
159 
160             task.setUseDefaultExcludes( fileSet.isUseDefaultExcludes() );
161 
162             final List<String> excludes = fileSet.getExcludes();
163             excludes.add( "**/*.filtered" );
164             excludes.add( "**/*.formatted" );
165             task.setExcludes( excludes );
166 
167             task.setIncludes( fileSet.getIncludes() );
168             task.setOutputDirectory( destDirectory );
169 
170             task.execute( archiver );
171         }
172     }
173 
174     File getFileSetDirectory( final FileSet fileSet, final File basedir, final File archiveBaseDir )
175         throws ArchiveCreationException, AssemblyFormattingException
176     {
177         String sourceDirectory = fileSet.getDirectory();
178 
179         if ( sourceDirectory == null || sourceDirectory.trim().length() < 1 )
180         {
181             sourceDirectory = basedir.getAbsolutePath();
182         }
183 
184         File fileSetDir;
185 
186         if ( archiveBaseDir == null )
187         {
188             fileSetDir = new File( sourceDirectory );
189 
190             if ( !fileSetDir.isAbsolute() )
191             {
192                 fileSetDir = new File( basedir, sourceDirectory );
193             }
194         }
195         else
196         {
197             fileSetDir = new File( archiveBaseDir, sourceDirectory );
198         }
199 
200         return fileSetDir;
201     }
202 
203     private void checkLogger()
204     {
205         if ( logger == null )
206         {
207             logger = new ConsoleLogger( Logger.LEVEL_INFO, "AddFileSetsTask-internal" );
208         }
209     }
210 
211     public void setLogger( final Logger logger )
212     {
213         this.logger = logger;
214     }
215 
216     public void setProject( final MavenProject project )
217     {
218         this.project = project;
219     }
220 
221     public void setModuleProject( final MavenProject moduleProject )
222     {
223         this.moduleProject = moduleProject;
224     }
225 
226 }