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 | 13 | { |
44 | 13 | this.configSource = configSource; |
45 | 13 | this.logger = logger; |
46 | 13 | } |
47 | |
|
48 | |
public File formatFileSetForAssembly( File fileSetDir, org.apache.maven.plugin.assembly.model.FileSet set ) |
49 | |
throws AssemblyFormattingException, IOException |
50 | |
{ |
51 | 10 | String lineEndingHint = set.getLineEnding(); |
52 | |
|
53 | 10 | String lineEnding = AssemblyFileUtils.getLineEndingCharacters( lineEndingHint ); |
54 | |
|
55 | 10 | if ( ( lineEnding != null ) || set.isFiltered() ) |
56 | |
{ |
57 | |
|
58 | 6 | FileSet fileSet = new FileSet(); |
59 | 6 | fileSet.setLineEnding( lineEnding ); |
60 | |
|
61 | 6 | fileSet.setDirectory(fileSetDir.getAbsolutePath()); |
62 | |
|
63 | 6 | fileSet.setIncludes( set.getIncludes() ); |
64 | |
|
65 | 6 | fileSet.setExcludes( set.getExcludes() ); |
66 | 6 | fileSet.setUseDefaultExcludes( true ); |
67 | |
|
68 | 6 | FileSetManager fsm = new FileSetManager( logger ); |
69 | 6 | String[] files = fsm.getIncludedFiles( fileSet ); |
70 | |
|
71 | |
|
72 | 6 | if ( ( files == null ) || ( files.length == 0 ) ) |
73 | |
{ |
74 | 1 | logger.info( "No files selected for line-ending conversion or filtering. Skipping: " + fileSet.getDirectory() ); |
75 | |
} |
76 | |
else |
77 | |
{ |
78 | 5 | File formattedDir = |
79 | |
FileUtils.createTempFile( "fileSetFormatter.", ".tmp", configSource.getTemporaryRootDirectory() ); |
80 | |
|
81 | 5 | logger.debug( "Filtering files from: " + fileSetDir + " into temp dir: " + formattedDir ); |
82 | |
|
83 | 5 | formattedDir.delete(); |
84 | 5 | formattedDir.mkdirs(); |
85 | |
|
86 | 5 | FileFormatter fileFormatter = new FileFormatter( configSource, logger ); |
87 | 12 | for ( int i = 0; i < files.length; i++ ) |
88 | |
{ |
89 | 7 | String file = files[i]; |
90 | |
|
91 | 7 | logger.debug( "Filtering: " + file ); |
92 | |
|
93 | 7 | File targetFile = new File( formattedDir, file ); |
94 | |
|
95 | 7 | targetFile.getParentFile().mkdirs(); |
96 | |
|
97 | 7 | File sourceFile = new File( fileSetDir, file ); |
98 | |
try |
99 | |
{ |
100 | 7 | sourceFile = fileFormatter.format( sourceFile, set.isFiltered(), lineEndingHint, formattedDir ); |
101 | 7 | 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 | 7 | } |
113 | |
} |
114 | 5 | return formattedDir; |
115 | |
} |
116 | 1 | } |
117 | |
else |
118 | |
{ |
119 | 4 | logger.debug( "NOT reformatting any files in " + fileSetDir ); |
120 | |
} |
121 | |
|
122 | 5 | 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 | |
} |