View Javadoc

1   package org.apache.maven.plugin.assembly.format;
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.utils.AssemblyFileUtils;
24  import org.apache.maven.shared.model.fileset.FileSet;
25  import org.apache.maven.shared.model.fileset.util.FileSetManager;
26  import org.codehaus.plexus.logging.Logger;
27  import org.codehaus.plexus.util.FileUtils;
28  
29  import java.io.File;
30  import java.io.IOException;
31  
32  /**
33   * @version $Id: FileSetFormatter.java 1163853 2011-08-31 22:42:32Z jdcasey $
34   */
35  public class FileSetFormatter
36  {
37  
38      private final AssemblerConfigurationSource configSource;
39  
40      private final Logger logger;
41  
42      public FileSetFormatter( AssemblerConfigurationSource configSource, Logger logger )
43      {
44          this.configSource = configSource;
45          this.logger = logger;
46      }
47  
48      public File formatFileSetForAssembly( File fileSetDir, org.apache.maven.plugin.assembly.model.FileSet set )
49          throws AssemblyFormattingException, IOException
50      {
51          String lineEndingHint = set.getLineEnding();
52  
53          String lineEnding = AssemblyFileUtils.getLineEndingCharacters( lineEndingHint );
54  
55          if ( ( lineEnding != null ) || set.isFiltered() )
56          {
57  
58              FileSet fileSet = new FileSet();
59              fileSet.setLineEnding( lineEnding );
60              
61              fileSet.setDirectory(fileSetDir.getAbsolutePath());
62                          
63              fileSet.setIncludes( set.getIncludes() );
64  
65              fileSet.setExcludes( set.getExcludes() );
66              fileSet.setUseDefaultExcludes( true );
67  
68              FileSetManager fsm = new FileSetManager( logger );
69              String[] files = fsm.getIncludedFiles( fileSet );
70  
71              // if we don't have anything to process, let's just skip all of this mess.
72              if ( ( files == null ) || ( files.length == 0 ) )
73              {
74                  logger.info( "No files selected for line-ending conversion or filtering. Skipping: " + fileSet.getDirectory() );
75              }
76              else
77              {
78                  File formattedDir =
79                      FileUtils.createTempFile( "fileSetFormatter.", ".tmp", configSource.getTemporaryRootDirectory() );
80                  
81                  logger.debug( "Filtering files from: " + fileSetDir + " into temp dir: " + formattedDir );
82  
83                  formattedDir.delete();
84                  formattedDir.mkdirs();
85  
86                  FileFormatter fileFormatter = new FileFormatter( configSource, logger );
87                  for ( int i = 0; i < files.length; i++ )
88                  {
89                      String file = files[i];
90                      
91                      logger.debug( "Filtering: " + file );
92  
93                      File targetFile = new File( formattedDir, file );
94  
95                      targetFile.getParentFile().mkdirs();
96  
97                      File sourceFile = new File( fileSetDir, file );
98                      try
99                      {
100                         sourceFile = fileFormatter.format( sourceFile, set.isFiltered(), lineEndingHint, formattedDir, configSource.getEncoding() );
101                         AssemblyFileUtils.copyFile( sourceFile, targetFile );
102                     }
103                     catch ( AssemblyFormattingException e )
104                     {
105                         deleteDirectory( formattedDir );
106                         throw e;
107                     }
108                     catch ( IOException e )
109                     {
110                         deleteDirectory( formattedDir );
111                         throw e;
112                     }
113                 }
114                 return formattedDir;
115             }
116         }
117         else
118         {
119             logger.debug( "NOT reformatting any files in " + fileSetDir );
120         }
121 
122         return fileSetDir;
123     }
124 
125     private static void deleteDirectory( File formattedDir )
126     {
127         try
128         {
129             FileUtils.deleteDirectory( formattedDir );
130         }
131         catch ( IOException e1 )
132         {
133             // ignore
134         }
135     }
136 
137 }