1 package org.apache.maven.plugin.assembly.format;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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
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
129 }
130 }
131
132 }