This repository has been archived by the owner on Jan 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 42
/
DefaultMavenResourcesFiltering.java
441 lines (365 loc) · 16.5 KB
/
DefaultMavenResourcesFiltering.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
package org.apache.maven.shared.filtering;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Resource;
import org.apache.maven.project.MavenProject;
import org.apache.maven.shared.utils.PathTool;
import org.apache.maven.shared.utils.ReaderFactory;
import org.apache.maven.shared.utils.StringUtils;
import org.apache.maven.shared.utils.io.FileUtils;
import org.apache.maven.shared.utils.io.FileUtils.FilterWrapper;
import org.apache.maven.shared.utils.io.IOUtil;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement;
import org.codehaus.plexus.logging.AbstractLogEnabled;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
import org.codehaus.plexus.util.Scanner;
import org.sonatype.plexus.build.incremental.BuildContext;
/**
* @author Olivier Lamy
*/
@Component( role = MavenResourcesFiltering.class, hint = "default" )
public class DefaultMavenResourcesFiltering
extends AbstractLogEnabled
implements MavenResourcesFiltering, Initializable
{
private static final String[] EMPTY_STRING_ARRAY = {};
private static final String[] DEFAULT_INCLUDES = { "**/**" };
private List<String> defaultNonFilteredFileExtensions;
@Requirement
private BuildContext buildContext;
@Requirement
private MavenFileFilter mavenFileFilter;
// ------------------------------------------------
// Plexus lifecycle
// ------------------------------------------------
/** {@inheritDoc} */
public void initialize()
throws InitializationException
{
// jpg,jpeg,gif,bmp,png
this.defaultNonFilteredFileExtensions = new ArrayList<String>( 5 );
this.defaultNonFilteredFileExtensions.add( "jpg" );
this.defaultNonFilteredFileExtensions.add( "jpeg" );
this.defaultNonFilteredFileExtensions.add( "gif" );
this.defaultNonFilteredFileExtensions.add( "bmp" );
this.defaultNonFilteredFileExtensions.add( "png" );
}
/** {@inheritDoc} */
public void filterResources( List<Resource> resources, File outputDirectory, MavenProject mavenProject,
String encoding, List<String> fileFilters, List<String> nonFilteredFileExtensions,
MavenSession mavenSession )
throws MavenFilteringException
{
MavenResourcesExecution mavenResourcesExecution =
new MavenResourcesExecution( resources, outputDirectory, mavenProject, encoding, fileFilters,
nonFilteredFileExtensions, mavenSession );
mavenResourcesExecution.setUseDefaultFilterWrappers( true );
// mavenResourcesExecution.setEscapeWindowsPaths( false );
filterResources( mavenResourcesExecution );
}
/** {@inheritDoc} */
public void filterResources( List<Resource> resources, File outputDirectory, String encoding,
List<FileUtils.FilterWrapper> filterWrappers, File resourcesBaseDirectory,
List<String> nonFilteredFileExtensions )
throws MavenFilteringException
{
MavenResourcesExecution mavenResourcesExecution =
new MavenResourcesExecution( resources, outputDirectory, encoding, filterWrappers, resourcesBaseDirectory,
nonFilteredFileExtensions );
filterResources( mavenResourcesExecution );
}
/** {@inheritDoc} */
public boolean filteredFileExtension( String fileName, List<String> userNonFilteredFileExtensions )
{
List<String> nonFilteredFileExtensions = new ArrayList<String>( getDefaultNonFilteredFileExtensions() );
if ( userNonFilteredFileExtensions != null )
{
nonFilteredFileExtensions.addAll( userNonFilteredFileExtensions );
}
boolean filteredFileExtension =
!nonFilteredFileExtensions.contains( StringUtils.lowerCase( FileUtils.extension( fileName ) ) );
if ( getLogger().isDebugEnabled() )
{
getLogger().debug( "file " + fileName + " has a" + ( filteredFileExtension ? " " : " non " )
+ "filtered file extension" );
}
return filteredFileExtension;
}
/** {@inheritDoc} */
public List<String> getDefaultNonFilteredFileExtensions()
{
if ( this.defaultNonFilteredFileExtensions == null )
{
this.defaultNonFilteredFileExtensions = new ArrayList<String>();
}
return this.defaultNonFilteredFileExtensions;
}
/** {@inheritDoc} */
public void filterResources( MavenResourcesExecution mavenResourcesExecution )
throws MavenFilteringException
{
if ( mavenResourcesExecution == null )
{
throw new MavenFilteringException( "mavenResourcesExecution cannot be null" );
}
if ( mavenResourcesExecution.getResources() == null )
{
getLogger().info( "No resources configured skip copying/filtering" );
return;
}
if ( mavenResourcesExecution.getOutputDirectory() == null )
{
throw new MavenFilteringException( "outputDirectory cannot be null" );
}
if ( mavenResourcesExecution.isUseDefaultFilterWrappers() )
{
List<FileUtils.FilterWrapper> filterWrappers = new ArrayList<FileUtils.FilterWrapper>();
if ( mavenResourcesExecution.getFilterWrappers() != null )
{
filterWrappers.addAll( mavenResourcesExecution.getFilterWrappers() );
}
filterWrappers.addAll( mavenFileFilter.getDefaultFilterWrappers( mavenResourcesExecution ) );
mavenResourcesExecution.setFilterWrappers( filterWrappers );
}
if ( mavenResourcesExecution.getEncoding() == null || mavenResourcesExecution.getEncoding().length() < 1 )
{
getLogger().warn( "Using platform encoding (" + ReaderFactory.FILE_ENCODING
+ " actually) to copy filtered resources, i.e. build is platform dependent!" );
}
else
{
getLogger().info( "Using '" + mavenResourcesExecution.getEncoding()
+ "' encoding to copy filtered resources." );
}
for ( Resource resource : mavenResourcesExecution.getResources() )
{
if ( getLogger().isDebugEnabled() )
{
String ls = System.getProperty( "line.separator" );
StringBuilder debugMessage =
new StringBuilder( "resource with targetPath " ).append( resource.getTargetPath() ).append( ls );
debugMessage.append( "directory " ).append( resource.getDirectory() ).append( ls );
// @formatter:off
debugMessage.append( "excludes " ).append( resource.getExcludes() == null ? " empty "
: resource.getExcludes().toString() ).append( ls );
debugMessage.append( "includes " ).append( resource.getIncludes() == null ? " empty "
: resource.getIncludes().toString() );
// @formatter:on
getLogger().debug( debugMessage.toString() );
}
String targetPath = resource.getTargetPath();
File resourceDirectory = new File( resource.getDirectory() );
if ( !resourceDirectory.isAbsolute() )
{
resourceDirectory =
new File( mavenResourcesExecution.getResourcesBaseDirectory(), resourceDirectory.getPath() );
}
if ( !resourceDirectory.exists() )
{
getLogger().info( "skip non existing resourceDirectory " + resourceDirectory.getPath() );
continue;
}
// this part is required in case the user specified "../something" as destination
// see MNG-1345
File outputDirectory = mavenResourcesExecution.getOutputDirectory();
boolean outputExists = outputDirectory.exists();
if ( !outputExists && !outputDirectory.mkdirs() )
{
throw new MavenFilteringException( "Cannot create resource output directory: " + outputDirectory );
}
boolean ignoreDelta = !outputExists || buildContext.hasDelta( mavenResourcesExecution.getFileFilters() )
|| buildContext.hasDelta( getRelativeOutputDirectory( mavenResourcesExecution ) );
getLogger().debug( "ignoreDelta " + ignoreDelta );
Scanner scanner = buildContext.newScanner( resourceDirectory, ignoreDelta );
setupScanner( resource, scanner );
scanner.scan();
if ( mavenResourcesExecution.isIncludeEmptyDirs() )
{
try
{
File targetDirectory =
targetPath == null ? outputDirectory : new File( outputDirectory, targetPath );
copyDirectoryLayout( resourceDirectory, targetDirectory, scanner );
}
catch ( IOException e )
{
throw new MavenFilteringException( "Cannot copy directory structure from "
+ resourceDirectory.getPath() + " to " + outputDirectory.getPath() );
}
}
List<String> includedFiles = Arrays.asList( scanner.getIncludedFiles() );
getLogger().info( "Copying " + includedFiles.size() + " resource" + ( includedFiles.size() > 1 ? "s" : "" )
+ ( targetPath == null ? "" : " to " + targetPath ) );
for ( String name : includedFiles )
{
File source = new File( resourceDirectory, name );
File destinationFile = getDestinationFile( outputDirectory, targetPath, name, mavenResourcesExecution );
boolean filteredExt =
filteredFileExtension( source.getName(), mavenResourcesExecution.getNonFilteredFileExtensions() );
mavenFileFilter.copyFile( source, destinationFile, resource.isFiltering() && filteredExt,
mavenResourcesExecution.getFilterWrappers(),
mavenResourcesExecution.getEncoding(),
mavenResourcesExecution.isOverwrite() );
}
// deal with deleted source files
scanner = buildContext.newDeleteScanner( resourceDirectory );
setupScanner( resource, scanner );
scanner.scan();
List<String> deletedFiles = Arrays.asList( scanner.getIncludedFiles() );
for ( String name : deletedFiles )
{
File destinationFile = getDestinationFile( outputDirectory, targetPath, name, mavenResourcesExecution );
destinationFile.delete();
buildContext.refresh( destinationFile );
}
}
}
private File getDestinationFile( File outputDirectory, String targetPath, String name,
MavenResourcesExecution mavenResourcesExecution )
throws MavenFilteringException
{
String destination = name;
if ( mavenResourcesExecution.isFilterFilenames() && mavenResourcesExecution.getFilterWrappers().size() > 0 )
{
destination = filterFileName( destination, mavenResourcesExecution.getFilterWrappers() );
}
if ( targetPath != null )
{
destination = targetPath + "/" + destination;
}
File destinationFile = new File( destination );
if ( !destinationFile.isAbsolute() )
{
destinationFile = new File( outputDirectory, destination );
}
if ( !destinationFile.getParentFile().exists() )
{
destinationFile.getParentFile().mkdirs();
}
return destinationFile;
}
private String[] setupScanner( Resource resource, Scanner scanner )
{
String[] includes = null;
if ( resource.getIncludes() != null && !resource.getIncludes().isEmpty() )
{
includes = (String[]) resource.getIncludes().toArray( EMPTY_STRING_ARRAY );
}
else
{
includes = DEFAULT_INCLUDES;
}
scanner.setIncludes( includes );
String[] excludes = null;
if ( resource.getExcludes() != null && !resource.getExcludes().isEmpty() )
{
excludes = (String[]) resource.getExcludes().toArray( EMPTY_STRING_ARRAY );
scanner.setExcludes( excludes );
}
scanner.addDefaultExcludes();
return includes;
}
private void copyDirectoryLayout( File sourceDirectory, File destinationDirectory, Scanner scanner )
throws IOException
{
if ( sourceDirectory == null )
{
throw new IOException( "source directory can't be null." );
}
if ( destinationDirectory == null )
{
throw new IOException( "destination directory can't be null." );
}
if ( sourceDirectory.equals( destinationDirectory ) )
{
throw new IOException( "source and destination are the same directory." );
}
if ( !sourceDirectory.exists() )
{
throw new IOException( "Source directory doesn't exists (" + sourceDirectory.getAbsolutePath() + ")." );
}
List<String> includedDirectories = Arrays.asList( scanner.getIncludedDirectories() );
for ( String name : includedDirectories )
{
File source = new File( sourceDirectory, name );
if ( source.equals( sourceDirectory ) )
{
continue;
}
File destination = new File( destinationDirectory, name );
destination.mkdirs();
}
}
private String getRelativeOutputDirectory( MavenResourcesExecution execution )
{
String relOutDir = execution.getOutputDirectory().getAbsolutePath();
if ( execution.getMavenProject() != null && execution.getMavenProject().getBasedir() != null )
{
String basedir = execution.getMavenProject().getBasedir().getAbsolutePath();
relOutDir = PathTool.getRelativeFilePath( basedir, relOutDir );
if ( relOutDir == null )
{
relOutDir = execution.getOutputDirectory().getPath();
}
else
{
relOutDir = relOutDir.replace( '\\', '/' );
}
}
return relOutDir;
}
/*
* Filter the name of a file using the same mechanism for filtering the content of the file.
*/
private String filterFileName( String name, List<FilterWrapper> wrappers )
throws MavenFilteringException
{
Reader reader = new StringReader( name );
for ( FilterWrapper wrapper : wrappers )
{
reader = wrapper.getReader( reader );
}
StringWriter writer = new StringWriter();
try
{
IOUtil.copy( reader, writer );
}
catch ( IOException e )
{
throw new MavenFilteringException( "Failed filtering filename" + name, e );
}
String filteredFilename = writer.toString();
if ( getLogger().isDebugEnabled() )
{
getLogger().debug( "renaming filename " + name + " to " + filteredFilename );
}
return filteredFilename;
}
}