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