1 package org.apache.maven.plugin.dependency;
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.lang.reflect.Field;
24 import java.util.List;
25
26 import org.apache.maven.artifact.factory.ArtifactFactory;
27 import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
28 import org.apache.maven.artifact.repository.ArtifactRepository;
29 import org.apache.maven.artifact.resolver.ArtifactCollector;
30 import org.apache.maven.artifact.resolver.ArtifactResolver;
31 import org.apache.maven.plugin.AbstractMojo;
32 import org.apache.maven.plugin.MojoExecutionException;
33 import org.apache.maven.plugin.dependency.utils.DependencySilentLog;
34 import org.apache.maven.plugin.logging.Log;
35 import org.apache.maven.project.MavenProject;
36 import org.codehaus.plexus.archiver.ArchiverException;
37 import org.codehaus.plexus.archiver.UnArchiver;
38 import org.codehaus.plexus.archiver.manager.ArchiverManager;
39 import org.codehaus.plexus.archiver.manager.NoSuchArchiverException;
40 import org.codehaus.plexus.components.io.fileselectors.IncludeExcludeFileSelector;
41 import org.codehaus.plexus.util.FileUtils;
42 import org.codehaus.plexus.util.ReflectionUtils;
43 import org.codehaus.plexus.util.StringUtils;
44
45
46
47
48
49
50 public abstract class AbstractDependencyMojo
51 extends AbstractMojo
52 {
53
54
55
56
57
58 protected ArtifactFactory factory;
59
60
61
62
63
64
65 protected ArtifactResolver resolver;
66
67
68
69
70
71
72
73
74 protected ArtifactCollector artifactCollector;
75
76
77
78
79
80
81
82 protected ArtifactMetadataSource artifactMetadataSource;
83
84
85
86
87
88
89
90
91 private ArtifactRepository local;
92
93
94
95
96
97
98
99
100 protected List<ArtifactRepository> remoteRepos;
101
102
103
104
105
106
107 protected ArchiverManager archiverManager;
108
109
110
111
112
113
114
115
116 protected MavenProject project;
117
118
119
120
121
122
123
124
125 protected List<MavenProject> reactorProjects;
126
127
128
129
130
131
132
133
134
135 public boolean silent;
136
137
138
139
140
141
142
143
144
145 protected boolean outputAbsoluteArtifactFilename;
146
147 private Log log;
148
149
150
151
152 public Log getLog()
153 {
154 if ( silent )
155 {
156 log = new DependencySilentLog();
157 }
158 else
159 {
160 log = super.getLog();
161 }
162
163 return this.log;
164 }
165
166
167
168
169 public ArchiverManager getArchiverManager()
170 {
171 return this.archiverManager;
172 }
173
174
175
176
177
178
179
180
181
182
183 protected void copyFile( File artifact, File destFile )
184 throws MojoExecutionException
185 {
186 Log theLog = this.getLog();
187 try
188 {
189 theLog.info( "Copying "
190 + ( this.outputAbsoluteArtifactFilename ? artifact.getAbsolutePath() : artifact.getName() ) + " to "
191 + destFile );
192 FileUtils.copyFile( artifact, destFile );
193
194 }
195 catch ( Exception e )
196 {
197 throw new MojoExecutionException( "Error copying artifact from " + artifact + " to " + destFile, e );
198 }
199 }
200
201 protected void unpack( File file, File location )
202 throws MojoExecutionException
203 {
204 unpack( file, location, null, null );
205 }
206
207
208
209
210
211
212
213
214
215
216
217 protected void unpack( File file, File location, String includes, String excludes )
218 throws MojoExecutionException
219 {
220 try
221 {
222 logUnpack( file, location, includes, excludes );
223
224 location.mkdirs();
225
226 UnArchiver unArchiver;
227
228 unArchiver = archiverManager.getUnArchiver( file );
229
230 unArchiver.setSourceFile( file );
231
232 unArchiver.setDestDirectory( location );
233
234 if ( StringUtils.isNotEmpty( excludes ) || StringUtils.isNotEmpty( includes ) )
235 {
236
237
238
239 IncludeExcludeFileSelector[] selectors = new IncludeExcludeFileSelector[] { new IncludeExcludeFileSelector() };
240
241 if ( StringUtils.isNotEmpty( excludes ) )
242 {
243 selectors[0].setExcludes( excludes.split( "," ) );
244 }
245
246 if ( StringUtils.isNotEmpty( includes ) )
247 {
248 selectors[0].setIncludes( includes.split( "," ) );
249 }
250
251 unArchiver.setFileSelectors( selectors );
252 }
253 if ( this.silent )
254 {
255 silenceUnarchiver( unArchiver );
256 }
257
258 unArchiver.extract();
259 }
260 catch ( NoSuchArchiverException e )
261 {
262 throw new MojoExecutionException( "Unknown archiver type", e );
263 }
264 catch ( ArchiverException e )
265 {
266 e.printStackTrace();
267 throw new MojoExecutionException( "Error unpacking file: " + file + " to: " + location + "\r\n"
268 + e.toString(), e );
269 }
270 }
271
272 private void silenceUnarchiver( UnArchiver unArchiver )
273 {
274
275 try
276 {
277 Field field = ReflectionUtils.getFieldByNameIncludingSuperclasses( "logger", unArchiver.getClass() );
278
279 field.setAccessible( true );
280
281 field.set( unArchiver, this.getLog() );
282 }
283 catch ( Exception e )
284 {
285
286 }
287 }
288
289
290
291
292 public ArtifactFactory getFactory()
293 {
294 return this.factory;
295 }
296
297
298
299
300 public void setFactory( ArtifactFactory factory )
301 {
302 this.factory = factory;
303 }
304
305
306
307
308 public MavenProject getProject()
309 {
310 return this.project;
311 }
312
313
314
315
316 protected ArtifactRepository getLocal()
317 {
318 return this.local;
319 }
320
321
322
323
324 public void setLocal( ArtifactRepository local )
325 {
326 this.local = local;
327 }
328
329
330
331
332 public List<ArtifactRepository> getRemoteRepos()
333 {
334 return this.remoteRepos;
335 }
336
337
338
339
340 public void setRemoteRepos( List<ArtifactRepository> remoteRepos )
341 {
342 this.remoteRepos = remoteRepos;
343 }
344
345
346
347
348 public org.apache.maven.artifact.resolver.ArtifactResolver getResolver()
349 {
350 return this.resolver;
351 }
352
353
354
355
356 public void setResolver( ArtifactResolver resolver )
357 {
358 this.resolver = resolver;
359 }
360
361
362
363
364 public void setArchiverManager( ArchiverManager archiverManager )
365 {
366 this.archiverManager = archiverManager;
367 }
368
369
370
371
372 public ArtifactCollector getArtifactCollector()
373 {
374 return this.artifactCollector;
375 }
376
377
378
379
380 public void setArtifactCollector( ArtifactCollector theArtifactCollector )
381 {
382 this.artifactCollector = theArtifactCollector;
383 }
384
385
386
387
388 public ArtifactMetadataSource getArtifactMetadataSource()
389 {
390 return this.artifactMetadataSource;
391 }
392
393
394
395
396 public void setArtifactMetadataSource( ArtifactMetadataSource theArtifactMetadataSource )
397 {
398 this.artifactMetadataSource = theArtifactMetadataSource;
399 }
400
401 private void logUnpack( File file, File location, String includes, String excludes )
402 {
403 if ( !getLog().isInfoEnabled() )
404 {
405 return;
406 }
407
408 StringBuffer msg = new StringBuffer();
409 msg.append( "Unpacking " );
410 msg.append( file );
411 msg.append( " to " );
412 msg.append( location );
413
414 if ( includes != null && excludes != null )
415 {
416 msg.append( " with includes \"" );
417 msg.append( includes );
418 msg.append( "\" and excludes \"" );
419 msg.append( excludes );
420 msg.append( "\"" );
421 }
422 else if ( includes != null )
423 {
424 msg.append( " with includes \"" );
425 msg.append( includes );
426 msg.append( "\"" );
427 }
428 else if ( excludes != null )
429 {
430 msg.append( " with excludes \"" );
431 msg.append( excludes );
432 msg.append( "\"" );
433 }
434
435 getLog().info( msg.toString() );
436 }
437 }