-
Notifications
You must be signed in to change notification settings - Fork 173
/
AbstractDependencyMojo.java
502 lines (440 loc) · 15.9 KB
/
AbstractDependencyMojo.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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
package org.apache.maven.plugins.dependency;
/*
* 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.lang.reflect.Field;
import java.util.List;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.dependency.utils.DependencySilentLog;
import org.apache.maven.project.DefaultProjectBuildingRequest;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.ProjectBuildingRequest;
import org.codehaus.plexus.archiver.ArchiverException;
import org.codehaus.plexus.archiver.UnArchiver;
import org.codehaus.plexus.archiver.manager.ArchiverManager;
import org.codehaus.plexus.archiver.manager.NoSuchArchiverException;
import org.codehaus.plexus.archiver.zip.ZipUnArchiver;
import org.codehaus.plexus.components.io.filemappers.FileMapper;
import org.codehaus.plexus.components.io.fileselectors.IncludeExcludeFileSelector;
import org.codehaus.plexus.util.FileUtils;
import org.codehaus.plexus.util.ReflectionUtils;
import org.codehaus.plexus.util.StringUtils;
import org.sonatype.plexus.build.incremental.BuildContext;
/**
* @author <a href="mailto:brianf@apache.org">Brian Fox</a>
*/
public abstract class AbstractDependencyMojo
extends AbstractMojo
{
/**
* To look up Archiver/UnArchiver implementations
*/
@Component
private ArchiverManager archiverManager;
/**
* For m2e incremental build support
*/
@Component
private BuildContext buildContext;
/**
* Skip plugin execution during incremental builds (e.g. triggered from M2E).
*
* @since 3.3.1
*/
@Parameter( defaultValue = "false" )
private boolean skipDuringIncrementalBuild;
/**
* <p>
* will use the jvm chmod, this is available for user and all level group level will be ignored
* </p>
* <b>since 2.6 is on by default</b>
*
* @since 2.5.1
*/
@Parameter( property = "dependency.useJvmChmod", defaultValue = "true" )
private boolean useJvmChmod = true;
/**
* ignore to set file permissions when unpacking a dependency
*
* @since 2.7
*/
@Parameter( property = "dependency.ignorePermissions", defaultValue = "false" )
private boolean ignorePermissions;
/**
* POM
*/
@Parameter( defaultValue = "${project}", readonly = true, required = true )
private MavenProject project;
/**
* Remote repositories which will be searched for artifacts.
*/
@Parameter( defaultValue = "${project.remoteArtifactRepositories}", readonly = true, required = true )
private List<ArtifactRepository> remoteRepositories;
/**
* Remote repositories which will be searched for plugins.
*/
@Parameter( defaultValue = "${project.pluginArtifactRepositories}", readonly = true, required = true )
private List<ArtifactRepository> remotePluginRepositories;
/**
* Contains the full list of projects in the reactor.
*/
@Parameter( defaultValue = "${reactorProjects}", readonly = true )
protected List<MavenProject> reactorProjects;
/**
* The Maven session
*/
@Parameter( defaultValue = "${session}", readonly = true, required = true )
protected MavenSession session;
/**
* If the plugin should be silent.
*
* @since 2.0
*/
@Parameter( property = "silent", defaultValue = "false" )
private boolean silent;
/**
* Output absolute filename for resolved artifacts
*
* @since 2.0
*/
@Parameter( property = "outputAbsoluteArtifactFilename", defaultValue = "false" )
protected boolean outputAbsoluteArtifactFilename;
/**
* Skip plugin execution completely.
*
* @since 2.7
*/
@Parameter( property = "mdep.skip", defaultValue = "false" )
private boolean skip;
// Mojo methods -----------------------------------------------------------
/*
* @see org.apache.maven.plugin.Mojo#execute()
*/
@Override
public final void execute()
throws MojoExecutionException, MojoFailureException
{
if ( isSkip() )
{
getLog().info( "Skipping plugin execution" );
return;
}
doExecute();
}
/**
* @throws MojoExecutionException {@link MojoExecutionException}
* @throws MojoFailureException {@link MojoFailureException}
*/
protected abstract void doExecute()
throws MojoExecutionException, MojoFailureException;
/**
* @return Returns the archiverManager.
*/
public ArchiverManager getArchiverManager()
{
return this.archiverManager;
}
/**
* Does the actual copy of the file and logging.
*
* @param artifact represents the file to copy.
* @param destFile file name of destination file.
* @throws MojoExecutionException with a message if an error occurs.
*/
protected void copyFile( File artifact, File destFile )
throws MojoExecutionException
{
try
{
getLog().info( "Copying "
+ ( this.outputAbsoluteArtifactFilename ? artifact.getAbsolutePath() : artifact.getName() ) + " to "
+ destFile );
if ( artifact.isDirectory() )
{
// usual case is a future jar packaging, but there are special cases: classifier and other packaging
throw new MojoExecutionException( "Artifact has not been packaged yet. When used on reactor artifact, "
+ "copy should be executed after packaging: see MDEP-187." );
}
FileUtils.copyFile( artifact, destFile );
buildContext.refresh( destFile );
}
catch ( IOException e )
{
throw new MojoExecutionException( "Error copying artifact from " + artifact + " to " + destFile, e );
}
}
/**
* @param artifact {@link Artifact}
* @param location The location.
* @param encoding The encoding.
* @param fileMappers {@link FileMapper}s to be used for rewriting each target path, or {@code null} if no rewriting
* shall happen.
* @throws MojoExecutionException in case of an error.
*/
protected void unpack( Artifact artifact, File location, String encoding, FileMapper[] fileMappers )
throws MojoExecutionException
{
unpack( artifact, location, null, null, encoding, fileMappers );
}
/**
* Unpacks the archive file.
*
* @param artifact File to be unpacked.
* @param location Location where to put the unpacked files.
* @param includes Comma separated list of file patterns to include i.e. <code>**/.xml,
* **/*.properties</code>
* @param excludes Comma separated list of file patterns to exclude i.e. <code>**/*.xml,
* **/*.properties</code>
* @param encoding Encoding of artifact. Set {@code null} for default encoding.
* @param fileMappers {@link FileMapper}s to be used for rewriting each target path, or {@code null} if no rewriting
* shall happen.
* @throws MojoExecutionException In case of errors.
*/
protected void unpack( Artifact artifact, File location, String includes, String excludes, String encoding,
FileMapper[] fileMappers ) throws MojoExecutionException
{
unpack( artifact, artifact.getType(), location, includes, excludes, encoding, fileMappers );
}
/**
* @param artifact {@link Artifact}
* @param type The type.
* @param location The location.
* @param includes includes list.
* @param excludes excludes list.
* @param encoding the encoding.
* @param fileMappers {@link FileMapper}s to be used for rewriting each target path, or {@code null} if no rewriting
* shall happen.
* @throws MojoExecutionException in case of an error.
*/
protected void unpack( Artifact artifact, String type, File location, String includes, String excludes,
String encoding, FileMapper[] fileMappers )
throws MojoExecutionException
{
File file = artifact.getFile();
try
{
logUnpack( file, location, includes, excludes );
location.mkdirs();
if ( !location.exists() )
{
throw new MojoExecutionException( "Location to write unpacked files to could not be created: "
+ location );
}
if ( file.isDirectory() )
{
// usual case is a future jar packaging, but there are special cases: classifier and other packaging
throw new MojoExecutionException( "Artifact has not been packaged yet. When used on reactor artifact, "
+ "unpack should be executed after packaging: see MDEP-98." );
}
UnArchiver unArchiver;
try
{
unArchiver = archiverManager.getUnArchiver( type );
getLog().debug( "Found unArchiver by type: " + unArchiver );
}
catch ( NoSuchArchiverException e )
{
unArchiver = archiverManager.getUnArchiver( file );
getLog().debug( "Found unArchiver by extension: " + unArchiver );
}
if ( encoding != null && unArchiver instanceof ZipUnArchiver )
{
( (ZipUnArchiver) unArchiver ).setEncoding( encoding );
getLog().info( "Unpacks '" + type + "' with encoding '" + encoding + "'." );
}
unArchiver.setIgnorePermissions( ignorePermissions );
unArchiver.setSourceFile( file );
unArchiver.setDestDirectory( location );
if ( StringUtils.isNotEmpty( excludes ) || StringUtils.isNotEmpty( includes ) )
{
// Create the selectors that will filter
// based on include/exclude parameters
// MDEP-47
IncludeExcludeFileSelector[] selectors =
new IncludeExcludeFileSelector[] { new IncludeExcludeFileSelector() };
if ( StringUtils.isNotEmpty( excludes ) )
{
selectors[0].setExcludes( excludes.split( "," ) );
}
if ( StringUtils.isNotEmpty( includes ) )
{
selectors[0].setIncludes( includes.split( "," ) );
}
unArchiver.setFileSelectors( selectors );
}
if ( this.silent )
{
silenceUnarchiver( unArchiver );
}
unArchiver.setFileMappers( fileMappers );
unArchiver.extract();
}
catch ( NoSuchArchiverException e )
{
throw new MojoExecutionException( "Unknown archiver type", e );
}
catch ( ArchiverException e )
{
throw new MojoExecutionException( "Error unpacking file: " + file + " to: " + location, e );
}
buildContext.refresh( location );
}
private void silenceUnarchiver( UnArchiver unArchiver )
{
// dangerous but handle any errors. It's the only way to silence the unArchiver.
try
{
Field field = ReflectionUtils.getFieldByNameIncludingSuperclasses( "logger", unArchiver.getClass() );
field.setAccessible( true );
field.set( unArchiver, this.getLog() );
}
catch ( Exception e )
{
// was a nice try. Don't bother logging because the log is silent.
}
}
/**
* @return Returns a new ProjectBuildingRequest populated from the current session and the current project remote
* repositories, used to resolve artifacts.
*/
public ProjectBuildingRequest newResolveArtifactProjectBuildingRequest()
{
return newProjectBuildingRequest( remoteRepositories );
}
/**
* @return Returns a new ProjectBuildingRequest populated from the current session and the current project remote
* repositories, used to resolve plugins.
*/
protected ProjectBuildingRequest newResolvePluginProjectBuildingRequest()
{
return newProjectBuildingRequest( remotePluginRepositories );
}
private ProjectBuildingRequest newProjectBuildingRequest( List<ArtifactRepository> repositories )
{
ProjectBuildingRequest buildingRequest =
new DefaultProjectBuildingRequest( session.getProjectBuildingRequest() );
buildingRequest.setRemoteRepositories( repositories );
return buildingRequest;
}
/**
* @return Returns the project.
*/
public MavenProject getProject()
{
return this.project;
}
/**
* @param archiverManager The archiverManager to set.
*/
public void setArchiverManager( ArchiverManager archiverManager )
{
this.archiverManager = archiverManager;
}
/**
* @return {@link #useJvmChmod}
*/
public boolean isUseJvmChmod()
{
return useJvmChmod;
}
/**
* @param useJvmChmod {@link #useJvmChmod}
*/
public void setUseJvmChmod( boolean useJvmChmod )
{
this.useJvmChmod = useJvmChmod;
}
/**
* @return {@link #skip}
*/
public boolean isSkip()
{
if ( skipDuringIncrementalBuild && buildContext.isIncremental() )
{
return true;
}
return skip;
}
/**
* @param skip {@link #skip}
*/
public void setSkip( boolean skip )
{
this.skip = skip;
}
/**
* @return {@link #silent}
*/
protected final boolean isSilent()
{
return silent;
}
/**
* @param silent {@link #silent}
*/
public void setSilent( boolean silent )
{
this.silent = silent;
if ( silent )
{
setLog( new DependencySilentLog() );
}
}
private void logUnpack( File file, File location, String includes, String excludes )
{
if ( !getLog().isInfoEnabled() )
{
return;
}
StringBuilder msg = new StringBuilder();
msg.append( "Unpacking " );
msg.append( file );
msg.append( " to " );
msg.append( location );
if ( includes != null && excludes != null )
{
msg.append( " with includes \"" );
msg.append( includes );
msg.append( "\" and excludes \"" );
msg.append( excludes );
msg.append( "\"" );
}
else if ( includes != null )
{
msg.append( " with includes \"" );
msg.append( includes );
msg.append( "\"" );
}
else if ( excludes != null )
{
msg.append( " with excludes \"" );
msg.append( excludes );
msg.append( "\"" );
}
getLog().info( msg.toString() );
}
}