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