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 java.io.File; |
30 | |
import java.io.IOException; |
31 | |
|
32 | |
|
33 | |
|
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 | 39 | { |
44 | 39 | this.configSource = configSource; |
45 | 39 | this.logger = logger; |
46 | 39 | } |
47 | |
|
48 | |
public File formatFileSetForAssembly( File fileSetDir, org.apache.maven.plugin.assembly.model.FileSet set ) |
49 | |
throws AssemblyFormattingException, IOException |
50 | |
{ |
51 | 30 | String lineEndingHint = set.getLineEnding(); |
52 | |
|
53 | 30 | String lineEnding = AssemblyFileUtils.getLineEndingCharacters( lineEndingHint ); |
54 | |
|
55 | 30 | if ( ( lineEnding != null ) || set.isFiltered() ) |
56 | |
{ |
57 | |
|
58 | 18 | FileSet fileSet = new FileSet(); |
59 | 18 | fileSet.setLineEnding( lineEnding ); |
60 | |
|
61 | 18 | fileSet.setDirectory(fileSetDir.getAbsolutePath()); |
62 | |
|
63 | 18 | fileSet.setIncludes( set.getIncludes() ); |
64 | |
|
65 | 18 | fileSet.setExcludes( set.getExcludes() ); |
66 | 18 | fileSet.setUseDefaultExcludes( true ); |
67 | |
|
68 | 18 | FileSetManager fsm = new FileSetManager( logger ); |
69 | 18 | String[] files = fsm.getIncludedFiles( fileSet ); |
70 | |
|
71 | |
|
72 | 18 | if ( ( files == null ) || ( files.length == 0 ) ) |
73 | |
{ |
74 | 3 | logger.info( "No files selected for line-ending conversion or filtering. Skipping: " + fileSet.getDirectory() ); |
75 | |
} |
76 | |
else |
77 | |
{ |
78 | 15 | File formattedDir = |
79 | |
FileUtils.createTempFile( "fileSetFormatter.", ".tmp", configSource.getTemporaryRootDirectory() ); |
80 | |
|
81 | 15 | logger.debug( "Filtering files from: " + fileSetDir + " into temp dir: " + formattedDir ); |
82 | |
|
83 | 15 | formattedDir.delete(); |
84 | 15 | formattedDir.mkdirs(); |
85 | |
|
86 | 15 | FileFormatter fileFormatter = new FileFormatter( configSource, logger ); |
87 | 36 | for ( int i = 0; i < files.length; i++ ) |
88 | |
{ |
89 | 21 | String file = files[i]; |
90 | |
|
91 | 21 | logger.debug( "Filtering: " + file ); |
92 | |
|
93 | 21 | File targetFile = new File( formattedDir, file ); |
94 | |
|
95 | 21 | targetFile.getParentFile().mkdirs(); |
96 | |
|
97 | 21 | File sourceFile = new File( fileSetDir, file ); |
98 | |
try |
99 | |
{ |
100 | 21 | sourceFile = fileFormatter.format( sourceFile, set.isFiltered(), lineEndingHint, formattedDir, configSource.getEncoding() ); |
101 | 21 | AssemblyFileUtils.copyFile( sourceFile, targetFile ); |
102 | |
} |
103 | 0 | catch ( AssemblyFormattingException e ) |
104 | |
{ |
105 | 0 | deleteDirectory( formattedDir ); |
106 | 0 | throw e; |
107 | |
} |
108 | 0 | catch ( IOException e ) |
109 | |
{ |
110 | 0 | deleteDirectory( formattedDir ); |
111 | 0 | throw e; |
112 | 21 | } |
113 | |
} |
114 | 15 | return formattedDir; |
115 | |
} |
116 | 3 | } |
117 | |
else |
118 | |
{ |
119 | 12 | logger.debug( "NOT reformatting any files in " + fileSetDir ); |
120 | |
} |
121 | |
|
122 | 15 | return fileSetDir; |
123 | |
} |
124 | |
|
125 | |
private static void deleteDirectory( File formattedDir ) |
126 | |
{ |
127 | |
try |
128 | |
{ |
129 | 0 | FileUtils.deleteDirectory( formattedDir ); |
130 | |
} |
131 | 0 | catch ( IOException e1 ) |
132 | |
{ |
133 | |
|
134 | 0 | } |
135 | 0 | } |
136 | |
|
137 | |
} |