1 | |
package org.apache.maven.plugin.assembly.filter; |
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.utils.AssemblyFileUtils; |
23 | |
import org.codehaus.plexus.archiver.Archiver; |
24 | |
import org.codehaus.plexus.archiver.ArchiverException; |
25 | |
import org.codehaus.plexus.archiver.UnArchiver; |
26 | |
import org.codehaus.plexus.components.io.fileselectors.FileInfo; |
27 | |
import org.codehaus.plexus.logging.LogEnabled; |
28 | |
import org.codehaus.plexus.logging.Logger; |
29 | |
import org.codehaus.plexus.logging.console.ConsoleLogger; |
30 | |
import org.codehaus.plexus.util.IOUtil; |
31 | |
|
32 | |
import java.io.File; |
33 | |
import java.io.FileWriter; |
34 | |
import java.io.IOException; |
35 | |
import java.io.InputStreamReader; |
36 | |
import java.io.Reader; |
37 | |
import java.io.StringWriter; |
38 | |
import java.io.Writer; |
39 | |
import java.util.ArrayList; |
40 | |
import java.util.Collections; |
41 | |
import java.util.Date; |
42 | |
import java.util.Iterator; |
43 | |
import java.util.List; |
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | 0 | public class SimpleAggregatingDescriptorHandler |
49 | |
implements ContainerDescriptorHandler, LogEnabled |
50 | |
{ |
51 | |
|
52 | |
|
53 | |
|
54 | |
private String filePattern; |
55 | |
|
56 | |
private String outputPath; |
57 | |
|
58 | 0 | private final String commentChars = "#"; |
59 | |
|
60 | |
|
61 | |
|
62 | |
private boolean overrideFilterAction; |
63 | |
|
64 | 0 | private final StringWriter aggregateWriter = new StringWriter(); |
65 | |
|
66 | 0 | private final List<String> filenames = new ArrayList<String>(); |
67 | |
|
68 | |
|
69 | |
|
70 | |
private Logger logger; |
71 | |
|
72 | |
public void finalizeArchiveCreation( final Archiver archiver ) throws ArchiverException |
73 | |
{ |
74 | 0 | checkConfig(); |
75 | |
|
76 | 0 | if ( outputPath.endsWith( "/" ) ) |
77 | |
{ |
78 | 0 | throw new ArchiverException( |
79 | |
"Cannot write aggregated properties to a directory. You must specify a file name in the outputPath configuration for this handler. (handler: " |
80 | |
+ getClass().getName() ); |
81 | |
} |
82 | |
|
83 | 0 | if ( outputPath.startsWith( "/" ) ) |
84 | |
{ |
85 | 0 | outputPath = outputPath.substring( 1 ); |
86 | |
} |
87 | |
|
88 | 0 | final File temp = writePropertiesFile(); |
89 | |
|
90 | 0 | overrideFilterAction = true; |
91 | |
|
92 | 0 | archiver.addFile( temp, outputPath ); |
93 | |
|
94 | 0 | overrideFilterAction = false; |
95 | 0 | } |
96 | |
|
97 | |
private File writePropertiesFile() throws ArchiverException |
98 | |
{ |
99 | |
File f; |
100 | |
|
101 | 0 | Writer writer = null; |
102 | |
try |
103 | |
{ |
104 | 0 | f = File.createTempFile( "maven-assembly-plugin", "tmp" ); |
105 | 0 | f.deleteOnExit(); |
106 | |
|
107 | |
|
108 | 0 | writer = new FileWriter( f ); |
109 | |
|
110 | 0 | writer.write( commentChars + " Aggregated on " + new Date() + " from: " ); |
111 | |
|
112 | 0 | for ( final Iterator<String> it = filenames.iterator(); it.hasNext(); ) |
113 | |
{ |
114 | 0 | final String filename = it.next(); |
115 | |
|
116 | 0 | writer.write( "\n" + commentChars + " " + filename ); |
117 | 0 | } |
118 | |
|
119 | 0 | writer.write( "\n\n" ); |
120 | |
|
121 | 0 | writer.write( aggregateWriter.toString() ); |
122 | |
} |
123 | 0 | catch ( final IOException e ) |
124 | |
{ |
125 | 0 | throw new ArchiverException( "Error adding aggregated properties to finalize archive creation. Reason: " |
126 | |
+ e.getMessage(), e ); |
127 | |
} |
128 | |
finally |
129 | |
{ |
130 | 0 | IOUtil.close( writer ); |
131 | 0 | } |
132 | |
|
133 | 0 | return f; |
134 | |
} |
135 | |
|
136 | |
public void finalizeArchiveExtraction( final UnArchiver unarchiver ) throws ArchiverException |
137 | |
{ |
138 | 0 | } |
139 | |
|
140 | |
public List<String> getVirtualFiles() |
141 | |
{ |
142 | 0 | checkConfig(); |
143 | |
|
144 | 0 | return Collections.singletonList( outputPath ); |
145 | |
} |
146 | |
|
147 | |
public boolean isSelected( final FileInfo fileInfo ) throws IOException |
148 | |
{ |
149 | 0 | checkConfig(); |
150 | |
|
151 | 0 | if ( overrideFilterAction ) |
152 | |
{ |
153 | 0 | System.out.println( "Filtering overridden. Returning true." ); |
154 | 0 | return true; |
155 | |
} |
156 | |
|
157 | 0 | String name = fileInfo.getName(); |
158 | 0 | name = AssemblyFileUtils.normalizePath( name ); |
159 | |
|
160 | 0 | name = name.replace( File.separatorChar, '/' ); |
161 | |
|
162 | 0 | if ( fileInfo.isFile() && name.matches( filePattern ) ) |
163 | |
{ |
164 | 0 | readProperties( fileInfo ); |
165 | 0 | filenames.add( name ); |
166 | |
|
167 | 0 | return false; |
168 | |
} |
169 | |
|
170 | 0 | return true; |
171 | |
} |
172 | |
|
173 | |
private void checkConfig() |
174 | |
{ |
175 | 0 | if ( filePattern == null || outputPath == null ) |
176 | |
{ |
177 | 0 | throw new IllegalStateException( |
178 | |
"You must configure filePattern and outputPath in your containerDescriptorHandler declaration." ); |
179 | |
} |
180 | 0 | } |
181 | |
|
182 | |
private void readProperties( final FileInfo fileInfo ) throws IOException |
183 | |
{ |
184 | 0 | final StringWriter writer = new StringWriter(); |
185 | 0 | Reader reader = null; |
186 | |
try |
187 | |
{ |
188 | |
|
189 | 0 | reader = new InputStreamReader( fileInfo.getContents() ); |
190 | |
|
191 | 0 | IOUtil.copy( reader, writer ); |
192 | |
} |
193 | |
finally |
194 | |
{ |
195 | 0 | IOUtil.close( reader ); |
196 | 0 | } |
197 | |
|
198 | 0 | final String content = writer.toString(); |
199 | |
|
200 | 0 | aggregateWriter.write( "\n" ); |
201 | 0 | aggregateWriter.write( content ); |
202 | 0 | } |
203 | |
|
204 | |
protected final Logger getLogger() |
205 | |
{ |
206 | 0 | if ( logger == null ) |
207 | |
{ |
208 | 0 | logger = new ConsoleLogger( Logger.LEVEL_INFO, "" ); |
209 | |
} |
210 | |
|
211 | 0 | return logger; |
212 | |
} |
213 | |
|
214 | |
public void enableLogging( final Logger logger ) |
215 | |
{ |
216 | 0 | this.logger = logger; |
217 | 0 | } |
218 | |
|
219 | |
public String getFilePattern() |
220 | |
{ |
221 | 0 | return filePattern; |
222 | |
} |
223 | |
|
224 | |
public void setFilePattern( final String filePattern ) |
225 | |
{ |
226 | 0 | this.filePattern = filePattern; |
227 | 0 | } |
228 | |
|
229 | |
public String getOutputPath() |
230 | |
{ |
231 | 0 | return outputPath; |
232 | |
} |
233 | |
|
234 | |
public void setOutputPath( final String outputPath ) |
235 | |
{ |
236 | 0 | this.outputPath = outputPath; |
237 | 0 | } |
238 | |
|
239 | |
} |