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