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