1 package org.apache.maven.plugin.war.packaging;
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.IOException;
24 import java.util.Iterator;
25
26 import org.apache.maven.artifact.Artifact;
27 import org.apache.maven.plugin.MojoExecutionException;
28 import org.apache.maven.plugin.war.AbstractWarMojo;
29 import org.apache.maven.plugin.war.util.MappingUtils;
30 import org.apache.maven.plugin.war.util.PathSet;
31 import org.apache.maven.plugin.war.util.WebappStructure;
32 import org.apache.maven.shared.filtering.MavenFilteringException;
33 import org.codehaus.plexus.archiver.ArchiverException;
34 import org.codehaus.plexus.archiver.UnArchiver;
35 import org.codehaus.plexus.archiver.manager.NoSuchArchiverException;
36 import org.codehaus.plexus.interpolation.InterpolationException;
37 import org.codehaus.plexus.util.DirectoryScanner;
38 import org.codehaus.plexus.util.FileUtils;
39
40
41
42
43
44 public abstract class AbstractWarPackagingTask
45 implements WarPackagingTask
46 {
47 public static final String[] DEFAULT_INCLUDES = {"**/**"};
48
49 public static final String WEB_INF_PATH = "WEB-INF";
50
51 public static final String META_INF_PATH = "META-INF";
52
53 public static final String CLASSES_PATH = "WEB-INF/classes/";
54
55 public static final String LIB_PATH = "WEB-INF/lib/";
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76 protected void copyFiles( String sourceId, WarPackagingContext context, File sourceBaseDir, PathSet sourceFilesSet,
77 String targetPrefix, boolean filtered )
78 throws IOException, MojoExecutionException
79 {
80 for ( Iterator iter = sourceFilesSet.iterator(); iter.hasNext(); )
81 {
82 final String fileToCopyName = (String) iter.next();
83 final File sourceFile = new File( sourceBaseDir, fileToCopyName );
84
85 String destinationFileName;
86 if ( targetPrefix == null )
87 {
88 destinationFileName = fileToCopyName;
89 }
90 else
91 {
92 destinationFileName = targetPrefix + fileToCopyName;
93 }
94
95
96 if ( filtered
97 && !context.isNonFilteredExtension( sourceFile.getName() ) )
98 {
99 copyFilteredFile( sourceId, context, sourceFile, destinationFileName );
100 }
101 else
102 {
103 copyFile( sourceId, context, sourceFile, destinationFileName );
104 }
105 }
106 }
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121 protected void copyFiles( String sourceId, WarPackagingContext context, File sourceBaseDir, PathSet sourceFilesSet,
122 boolean filtered )
123 throws IOException, MojoExecutionException
124 {
125 copyFiles( sourceId, context, sourceBaseDir, sourceFilesSet, null, filtered );
126 }
127
128
129
130
131
132
133
134
135
136
137
138
139
140 protected void copyFile( String sourceId, final WarPackagingContext context, final File file,
141 String targetFilename )
142 throws IOException
143 {
144 final File targetFile = new File( context.getWebappDirectory(), targetFilename );
145 context.getWebappStructure().registerFile( sourceId, targetFilename, new WebappStructure.RegistrationCallback()
146 {
147 public void registered( String ownerId, String targetFilename )
148 throws IOException
149 {
150 copyFile( context, file, targetFile, targetFilename, false );
151 }
152
153 public void alreadyRegistered( String ownerId, String targetFilename )
154 throws IOException
155 {
156 copyFile( context, file, targetFile, targetFilename, true );
157 }
158
159 public void refused( String ownerId, String targetFilename, String actualOwnerId )
160 throws IOException
161 {
162 context.getLog().debug( " - " + targetFilename + " wasn't copied because it has "
163 + "already been packaged for overlay [" + actualOwnerId + "]." );
164 }
165
166 public void superseded( String ownerId, String targetFilename, String deprecatedOwnerId )
167 throws IOException
168 {
169 context.getLog().info( "File [" + targetFilename + "] belonged to overlay [" + deprecatedOwnerId
170 + "] so it will be overwritten." );
171 copyFile( context, file, targetFile, targetFilename, false );
172 }
173
174 public void supersededUnknownOwner( String ownerId, String targetFilename, String unknownOwnerId )
175 throws IOException
176 {
177 context.getLog()
178 .warn( "File [" + targetFilename + "] belonged to overlay [" + unknownOwnerId
179 + "] which does not exist anymore in the current project. It is recommended to invoke "
180 + "clean if the dependencies of the project changed." );
181 copyFile( context, file, targetFile, targetFilename, false );
182 }
183 } );
184 }
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201 protected boolean copyFilteredFile( String sourceId, final WarPackagingContext context, File file,
202 String targetFilename )
203 throws IOException, MojoExecutionException
204 {
205
206 if ( context.getWebappStructure().registerFile( sourceId, targetFilename ) )
207 {
208 final File targetFile = new File( context.getWebappDirectory(), targetFilename );
209 try
210 {
211
212 targetFile.getParentFile().mkdirs();
213
214 context.getMavenFileFilter().copyFile( file, targetFile, true, context.getFilterWrappers(), null );
215 }
216 catch ( MavenFilteringException e )
217 {
218 throw new MojoExecutionException( e.getMessage(), e );
219 }
220
221 context.getLog().debug( " + " + targetFilename + " has been copied (filtered)." );
222 return true;
223 }
224 else
225 {
226 context.getLog().debug(
227 " - " + targetFilename + " wasn't copied because it has already been packaged (filtered)." );
228 return false;
229 }
230 }
231
232
233
234
235
236
237
238
239
240
241 protected void doUnpack( WarPackagingContext context, File file, File unpackDirectory )
242 throws MojoExecutionException
243 {
244 String archiveExt = FileUtils.getExtension( file.getAbsolutePath() ).toLowerCase();
245
246 try
247 {
248 UnArchiver unArchiver = context.getArchiverManager().getUnArchiver( archiveExt );
249 unArchiver.setSourceFile( file );
250 unArchiver.setDestDirectory( unpackDirectory );
251 unArchiver.setOverwrite( true );
252 unArchiver.extract();
253 }
254 catch ( ArchiverException e )
255 {
256 throw new MojoExecutionException( "Error unpacking file [" + file.getAbsolutePath() + "]" + "to ["
257 + unpackDirectory.getAbsolutePath() + "]", e );
258 }
259 catch ( NoSuchArchiverException e )
260 {
261 context.getLog().warn( "Skip unpacking dependency file [" + file.getAbsolutePath()
262 + " with unknown extension [" + archiveExt + "]" );
263 }
264 }
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282 protected boolean copyFile( WarPackagingContext context, File source, File destination, String targetFilename,
283 boolean onlyIfModified )
284 throws IOException
285 {
286 if ( onlyIfModified && destination.lastModified() >= source.lastModified() )
287 {
288 context.getLog().debug( " * " + targetFilename + " is up to date." );
289 return false;
290 }
291 else
292 {
293 FileUtils.copyFile( source.getCanonicalFile(), destination );
294
295 destination.setLastModified( source.lastModified() );
296 context.getLog().debug( " + " + targetFilename + " has been copied." );
297 return true;
298 }
299 }
300
301
302
303
304
305
306
307
308
309
310 protected PathSet getFilesToIncludes( File baseDir, String[] includes, String[] excludes )
311 {
312 final DirectoryScanner scanner = new DirectoryScanner();
313 scanner.setBasedir( baseDir );
314
315 if ( excludes != null )
316 {
317 scanner.setExcludes( excludes );
318 }
319 scanner.addDefaultExcludes();
320
321 if ( includes != null && includes.length > 0 )
322 {
323 scanner.setIncludes( includes );
324 }
325 else
326 {
327 scanner.setIncludes( DEFAULT_INCLUDES );
328 }
329
330 scanner.scan();
331
332 return new PathSet( scanner.getIncludedFiles() );
333
334 }
335
336
337
338
339
340
341
342
343
344
345
346 protected String getArtifactFinalName( WarPackagingContext context, Artifact artifact )
347 throws InterpolationException
348 {
349 if ( context.getOutputFileNameMapping() != null )
350 {
351 return MappingUtils.evaluateFileNameMapping( context.getOutputFileNameMapping(), artifact );
352 }
353
354 String classifier = artifact.getClassifier();
355 if ( ( classifier != null ) && !( "".equals( classifier.trim() ) ) )
356 {
357 return MappingUtils.evaluateFileNameMapping( AbstractWarMojo.DEFAULT_FILE_NAME_MAPPING_CLASSIFIER,
358 artifact );
359 }
360 else
361 {
362 return MappingUtils.evaluateFileNameMapping( AbstractWarMojo.DEFAULT_FILE_NAME_MAPPING, artifact );
363 }
364
365 }
366 }