View Javadoc

1   package org.apache.maven.plugin.dependency;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
47   * @version $Id: AbstractDependencyMojo.java 552528
48   *          2007-07-02 16:12:47Z markh $
49   */
50  public abstract class AbstractDependencyMojo
51      extends AbstractMojo
52  {
53      /**
54       * Used to look up Artifacts in the remote repository.
55       *
56       * @component
57       */
58      protected ArtifactFactory factory;
59  
60      /**
61       * Used to look up Artifacts in the remote repository.
62       *
63       * @component
64       */
65      protected ArtifactResolver resolver;
66  
67      /**
68       * Artifact collector, needed to resolve dependencies.
69       *
70       * @component role="org.apache.maven.artifact.resolver.ArtifactCollector"
71       * @required
72       * @readonly
73       */
74      protected ArtifactCollector artifactCollector;
75  
76      /**
77       * @component role="org.apache.maven.artifact.metadata.ArtifactMetadataSource"
78       *            hint="maven"
79       * @required
80       * @readonly
81       */
82      protected ArtifactMetadataSource artifactMetadataSource;
83  
84      /**
85       * Location of the local repository.
86       *
87       * @parameter expression="${localRepository}"
88       * @readonly
89       * @required
90       */
91      private ArtifactRepository local;
92  
93      /**
94       * List of Remote Repositories used by the resolver
95       *
96       * @parameter expression="${project.remoteArtifactRepositories}"
97       * @readonly
98       * @required
99       */
100     protected List<ArtifactRepository> remoteRepos;
101 
102     /**
103      * To look up Archiver/UnArchiver implementations
104      *
105      * @component
106      */
107     protected ArchiverManager archiverManager;
108 
109     /**
110      * POM
111      *
112      * @parameter expression="${project}"
113      * @readonly
114      * @required
115      */
116     protected MavenProject project;
117 
118     /**
119      * Contains the full list of projects in the reactor.
120      *
121      * @parameter expression="${reactorProjects}"
122      * @required
123      * @readonly
124      */
125     protected List<MavenProject> reactorProjects;
126 
127     /**
128      * If the plugin should be silent.
129      *
130      * @optional
131      * @since 2.0
132      * @parameter expression="${silent}"
133      *            default-value="false"
134      */
135     public boolean silent;
136 
137     /**
138      * Output absolute filename for resolved artifacts
139      *
140      * @optional
141      * @since 2.0
142      * @parameter expression="${outputAbsoluteArtifactFilename}"
143      *            default-value="false"
144      */
145     protected boolean outputAbsoluteArtifactFilename;
146 
147     private Log log;
148 
149     /**
150      * @return Returns the log.
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      * @return Returns the archiverManager.
168      */
169     public ArchiverManager getArchiverManager()
170     {
171         return this.archiverManager;
172     }
173 
174     /**
175      * Does the actual copy of the file and logging.
176      *
177      * @param artifact represents the file to copy.
178      * @param destFile file name of destination file.
179      *
180      * @throws MojoExecutionException with a message if an
181      *             error occurs.
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      * Unpacks the archive file.
209      *
210      * @param file File to be unpacked.
211      * @param location Location where to put the unpacked files.
212      * @param includes Comma separated list of file patterns to include i.e. <code>**&#47;.xml,
213      *            **&#47;*.properties</code>
214      * @param excludes Comma separated list of file patterns to exclude i.e. <code>**&#47;*.xml,
215      *            **&#47;*.properties</code>
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                 // Create the selectors that will filter
237                 // based on include/exclude parameters
238                 // MDEP-47
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         // dangerous but handle any errors. It's the only way to silence the unArchiver.
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             // was a nice try. Don't bother logging because the log is silent.
286         }
287     }
288 
289     /**
290      * @return Returns the factory.
291      */
292     public ArtifactFactory getFactory()
293     {
294         return this.factory;
295     }
296 
297     /**
298      * @param factory The factory to set.
299      */
300     public void setFactory( ArtifactFactory factory )
301     {
302         this.factory = factory;
303     }
304 
305     /**
306      * @return Returns the project.
307      */
308     public MavenProject getProject()
309     {
310         return this.project;
311     }
312 
313     /**
314      * @return Returns the local.
315      */
316     protected ArtifactRepository getLocal()
317     {
318         return this.local;
319     }
320 
321     /**
322      * @param local The local to set.
323      */
324     public void setLocal( ArtifactRepository local )
325     {
326         this.local = local;
327     }
328 
329     /**
330      * @return Returns the remoteRepos.
331      */
332     public List<ArtifactRepository> getRemoteRepos()
333     {
334         return this.remoteRepos;
335     }
336 
337     /**
338      * @param remoteRepos The remoteRepos to set.
339      */
340     public void setRemoteRepos( List<ArtifactRepository> remoteRepos )
341     {
342         this.remoteRepos = remoteRepos;
343     }
344 
345     /**
346      * @return Returns the resolver.
347      */
348     public org.apache.maven.artifact.resolver.ArtifactResolver getResolver()
349     {
350         return this.resolver;
351     }
352 
353     /**
354      * @param resolver The resolver to set.
355      */
356     public void setResolver( ArtifactResolver resolver )
357     {
358         this.resolver = resolver;
359     }
360 
361     /**
362      * @param archiverManager The archiverManager to set.
363      */
364     public void setArchiverManager( ArchiverManager archiverManager )
365     {
366         this.archiverManager = archiverManager;
367     }
368 
369     /**
370      * @return Returns the artifactCollector.
371      */
372     public ArtifactCollector getArtifactCollector()
373     {
374         return this.artifactCollector;
375     }
376 
377     /**
378      * @param theArtifactCollector The artifactCollector to set.
379      */
380     public void setArtifactCollector( ArtifactCollector theArtifactCollector )
381     {
382         this.artifactCollector = theArtifactCollector;
383     }
384 
385     /**
386      * @return Returns the artifactMetadataSource.
387      */
388     public ArtifactMetadataSource getArtifactMetadataSource()
389     {
390         return this.artifactMetadataSource;
391     }
392 
393     /**
394      * @param theArtifactMetadataSource The artifactMetadataSource to set.
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 }