1 package org.apache.maven.plugin.cxx;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 import org.apache.maven.artifact.Artifact;
22 import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
23 import org.apache.maven.artifact.resolver.ArtifactResolutionException;
24 import org.apache.maven.plugin.MojoExecutionException;
25 import org.apache.maven.plugin.dependency.utils.DependencyStatusSets;
26 import org.apache.maven.plugin.dependency.utils.DependencyUtil;
27 import org.apache.maven.plugin.dependency.utils.filters.MarkerFileFilter;
28 import org.apache.maven.plugin.dependency.utils.markers.DefaultFileMarkerHandler;
29 import org.apache.maven.plugins.annotations.LifecyclePhase;
30 import org.apache.maven.plugins.annotations.Mojo;
31 import org.apache.maven.plugins.annotations.Parameter;
32 import org.apache.maven.plugins.annotations.Component;
33 import org.apache.maven.plugins.annotations.ResolutionScope;
34 import org.apache.maven.shared.artifact.filter.collection.ArtifactsFilter;
35
36
37
38
39 import org.codehaus.plexus.util.StringUtils;
40
41 import java.io.File;
42 import java.io.IOException;
43 import java.util.ArrayList;
44 import java.util.Iterator;
45 import java.util.List;
46 import java.util.HashSet;
47 import java.util.Set;
48
49 import java.nio.file.Files;
50 import java.nio.file.Paths;
51 import java.nio.file.StandardCopyOption;
52
53 import java.lang.reflect.Field;
54 import org.codehaus.plexus.util.ReflectionUtils;
55 import org.codehaus.plexus.components.io.fileselectors.IncludeExcludeFileSelector;
56 import org.codehaus.plexus.archiver.manager.NoSuchArchiverException;
57 import org.codehaus.plexus.archiver.ArchiverException;
58
59 import org.codehaus.plexus.archiver.ArchiveContentLister;
60 import org.codehaus.plexus.archiver.ArchiveContentEntry;
61 import org.codehaus.plexus.archiver.manager.ArchiveContentListerManager;
62
63 import org.apache.maven.shared.artifact.filter.collection.ArtifactFilterException;
64 import org.apache.maven.plugin.cxx.utils.ClassifierRegexFilter;
65 import org.apache.maven.shared.artifact.filter.collection.ArtifactIdFilter;
66 import org.apache.maven.shared.artifact.filter.collection.ClassifierFilter;
67 import org.apache.maven.shared.artifact.filter.collection.FilterArtifacts;
68 import org.apache.maven.shared.artifact.filter.collection.GroupIdFilter;
69 import org.apache.maven.shared.artifact.filter.collection.ProjectTransitivityFilter;
70 import org.apache.maven.shared.artifact.filter.collection.ScopeFilter;
71 import org.apache.maven.shared.artifact.filter.collection.TypeFilter;
72
73 import org.apache.maven.project.MavenProject;
74 import org.apache.maven.project.MavenProjectBuilder;
75 import org.apache.maven.project.ProjectBuildingException;
76
77
78
79
80
81
82
83
84 @Mojo( name = "unpack-dependencies", requiresDependencyResolution = ResolutionScope.TEST,
85 defaultPhase = LifecyclePhase.PROCESS_SOURCES, threadSafe = true )
86 public class UnpackDependenciesMojo
87 extends org.apache.maven.plugin.dependency.fromDependencies.UnpackDependenciesMojo
88 {
89
90
91
92
93
94
95 @Component
96 protected ArchiveContentListerManager archiveContentListerManager;
97
98
99
100
101
102
103
104 @Parameter( property = "mdep.unpack.flattenDestDirs" )
105 protected List flattenDestDirs = new ArrayList();
106
107
108
109
110
111
112
113 @Parameter( property = "includeRegexClassifiers", defaultValue = "" )
114 protected String includeRegexClassifiers;
115
116
117
118
119
120
121
122 @Parameter( property = "excludeRegexClassifiers", defaultValue = "" )
123 protected String excludeRegexClassifiers;
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 protected void listAndFlatCopy( Artifact artifact, File srcRoot, File location, String includes, String excludes )
240 throws MojoExecutionException
241 {
242 File file = artifact.getFile();
243 try
244 {
245
246
247 location.mkdirs();
248
249 if ( file.isDirectory() )
250 {
251
252 throw new MojoExecutionException( "Artifact has not been packaged yet. When used on reactor artifact, "
253 + "unpack should be executed after packaging: see MDEP-98." );
254 }
255
256 ArchiveContentLister archiveContentLister;
257
258 try
259 {
260 archiveContentLister = archiveContentListerManager.getArchiveContentLister( artifact.getType() );
261 getLog().debug( "Found archiveContentLister by type: " + archiveContentLister );
262 }
263 catch ( NoSuchArchiverException e )
264 {
265 archiveContentLister = archiveContentListerManager.getArchiveContentLister( file );
266 getLog().debug( "Found unArchiver by extension: " + archiveContentLister );
267 }
268
269
270
271
272
273 archiveContentLister.setSourceFile( file );
274
275
276
277 if ( StringUtils.isNotEmpty( excludes ) || StringUtils.isNotEmpty( includes ) )
278 {
279
280
281
282 IncludeExcludeFileSelector[] selectors =
283 new IncludeExcludeFileSelector[]{ new IncludeExcludeFileSelector() };
284
285 if ( StringUtils.isNotEmpty( excludes ) )
286 {
287 selectors[0].setExcludes( excludes.split( "," ) );
288 }
289
290 if ( StringUtils.isNotEmpty( includes ) )
291 {
292 selectors[0].setIncludes( includes.split( "," ) );
293 }
294
295 archiveContentLister.setFileSelectors( selectors );
296 }
297 if ( this.silent )
298 {
299 silenceArchiveContentLister( archiveContentLister );
300 }
301
302 List<ArchiveContentEntry> contents = archiveContentLister.list();
303
304 for ( ArchiveContentEntry content : contents )
305 {
306 if ( content.getType() == ArchiveContentEntry.FILE )
307 {
308 String sSubFileName = ( new File( content.getName() ) ).getName();
309 String src = srcRoot.getAbsolutePath() + File.separator + content.getName();
310 String dst = location + File.separator + sSubFileName;
311 try
312 {
313 Files.copy( Paths.get( src ), Paths.get( dst ), StandardCopyOption.REPLACE_EXISTING );
314 getLog().debug( "Copy " + src + " to " + dst );
315 }
316 catch ( IOException e )
317 {
318 getLog().error( "Copy of " + src + " to " + dst + " failed : " + e );
319 }
320 }
321 }
322
323 }
324 catch ( NoSuchArchiverException e )
325 {
326 throw new MojoExecutionException( "Unknown archiver type", e );
327 }
328 catch ( ArchiverException e )
329 {
330 throw new MojoExecutionException(
331 "Error unpacking file: " + file + " to: " + location + "\r\n" + e.toString(), e );
332 }
333 }
334
335
336
337
338 private void silenceArchiveContentLister( ArchiveContentLister archiveContentLister )
339 {
340
341 try
342 {
343 Field field = ReflectionUtils.getFieldByNameIncludingSuperclasses(
344 "logger", archiveContentLister.getClass() );
345
346 field.setAccessible( true );
347
348 field.set( archiveContentLister, this.getLog() );
349 }
350 catch ( Exception e )
351 {
352
353 }
354 }
355
356
357
358
359 @Component
360 MavenProjectBuilder myProjectBuilder;
361
362
363
364
365 private MavenProject buildProjectFromArtifact( Artifact artifact )
366 throws MojoExecutionException
367 {
368 try
369 {
370 return myProjectBuilder.buildFromRepository( artifact, remoteRepos, getLocal() );
371 }
372 catch ( ProjectBuildingException e )
373 {
374 throw new MojoExecutionException( e.getMessage(), e );
375 }
376 }
377
378
379
380
381 private void addParentArtifacts( MavenProject project, Set<Artifact> artifacts )
382 throws MojoExecutionException
383 {
384 while ( project.hasParent() )
385 {
386 project = project.getParent();
387
388 if ( project.getArtifact() == null )
389 {
390
391 Artifact artifact =
392 factory.createBuildArtifact( project.getGroupId(), project.getArtifactId(), project.getVersion(),
393 project.getPackaging() );
394 project.setArtifact( artifact );
395 }
396
397 if ( !artifacts.add( project.getArtifact() ) )
398 {
399
400 break;
401 }
402 try
403 {
404 resolver.resolve( project.getArtifact(), this.remoteRepos, this.getLocal() );
405 }
406 catch ( ArtifactResolutionException e )
407 {
408 throw new MojoExecutionException( e.getMessage(), e );
409 }
410 catch ( ArtifactNotFoundException e )
411 {
412 throw new MojoExecutionException( e.getMessage(), e );
413 }
414 }
415 }
416
417
418
419
420
421
422
423
424
425
426
427
428
429 protected DependencyStatusSets getDependencySets( boolean stopOnFailure, boolean includeParents )
430 throws MojoExecutionException
431 {
432
433 FilterArtifacts filter = new FilterArtifacts();
434
435 filter.addFilter( new ProjectTransitivityFilter( project.getDependencyArtifacts(), this.excludeTransitive ) );
436
437 filter.addFilter( new ScopeFilter( DependencyUtil.cleanToBeTokenizedString( this.includeScope ),
438 DependencyUtil.cleanToBeTokenizedString( this.excludeScope ) ) );
439
440 filter.addFilter( new TypeFilter( DependencyUtil.cleanToBeTokenizedString( this.includeTypes ),
441 DependencyUtil.cleanToBeTokenizedString( this.excludeTypes ) ) );
442
443 filter.addFilter( new ClassifierFilter( DependencyUtil.cleanToBeTokenizedString( this.includeClassifiers ),
444 DependencyUtil.cleanToBeTokenizedString( this.excludeClassifiers ) ) );
445
446 filter.addFilter( new ClassifierRegexFilter (
447 DependencyUtil.cleanToBeTokenizedString( this.includeRegexClassifiers ),
448 DependencyUtil.cleanToBeTokenizedString( this.excludeRegexClassifiers ) ) );
449
450 filter.addFilter( new GroupIdFilter( DependencyUtil.cleanToBeTokenizedString( this.includeGroupIds ),
451 DependencyUtil.cleanToBeTokenizedString( this.excludeGroupIds ) ) );
452
453 filter.addFilter( new ArtifactIdFilter( DependencyUtil.cleanToBeTokenizedString( this.includeArtifactIds ),
454 DependencyUtil.cleanToBeTokenizedString( this.excludeArtifactIds ) ) );
455
456
457
458 @SuppressWarnings( "unchecked" ) Set<Artifact> artifacts = project.getArtifacts();
459
460 if ( includeParents )
461 {
462
463 for ( Artifact dep : new ArrayList<Artifact>( artifacts ) )
464 {
465 addParentArtifacts( buildProjectFromArtifact( dep ), artifacts );
466 }
467
468
469 addParentArtifacts( project, artifacts );
470 }
471
472
473 try
474 {
475 artifacts = filter.filter( artifacts );
476 }
477 catch ( ArtifactFilterException e )
478 {
479 throw new MojoExecutionException( e.getMessage(), e );
480 }
481
482
483 DependencyStatusSets status;
484 if ( StringUtils.isNotEmpty( classifier ) )
485 {
486 status = getClassifierTranslatedDependencies( artifacts, stopOnFailure );
487 }
488 else
489 {
490 status = filterMarkedDependencies( artifacts );
491 }
492
493 return status;
494 }
495
496
497
498
499
500
501
502
503
504
505
506
507 protected void doExecute()
508 throws MojoExecutionException
509 {
510 DependencyStatusSets dss = getDependencySets( this.failOnMissingClassifierArtifact );
511
512 for ( Artifact artifact : dss.getResolvedDependencies() )
513 {
514 File destDir;
515 destDir = DependencyUtil.getFormattedOutputDirectory( useSubDirectoryPerScope, useSubDirectoryPerType,
516 useSubDirectoryPerArtifact, useRepositoryLayout,
517 stripVersion, outputDirectory, artifact );
518
519 unpack( artifact, destDir, getIncludes(), getExcludes() );
520 DefaultFileMarkerHandler handler = new DefaultFileMarkerHandler( artifact, this.markersDirectory );
521 handler.setMarker();
522
523
524 Iterator it = flattenDestDirs.iterator();
525 HashSet<String> incudedSet = new HashSet<String>();
526 while ( it.hasNext() )
527 {
528 String flattenDestDir = it.next().toString();
529 listAndFlatCopy( artifact, destDir, new File( flattenDestDir ), getIncludes(), getExcludes() );
530 }
531
532 }
533
534 for ( Artifact artifact : dss.getSkippedDependencies() )
535 {
536 getLog().info( artifact.getFile().getName() + " already exists in destination." );
537 }
538 }
539
540 protected ArtifactsFilter getMarkedArtifactFilter()
541 {
542 return new MarkerFileFilter( this.overWriteReleases, this.overWriteSnapshots, this.overWriteIfNewer,
543 new DefaultFileMarkerHandler( this.markersDirectory ) );
544 }
545 }