Coverage Report - org.apache.maven.plugin.dependency.AbstractDependencyMojo
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractDependencyMojo
66%
42/64
93%
13/14
1.6
 
 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.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  
  * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
 44  
  * @version $Id: AbstractDependencyMojo.java 552528
 45  
  *          2007-07-02 16:12:47Z markh $
 46  
  */
 47  135
 public abstract class AbstractDependencyMojo
 48  
     extends AbstractMojo
 49  
 {
 50  
     /**
 51  
      * Used to look up Artifacts in the remote repository.
 52  
      *
 53  
      * @component
 54  
      */
 55  
     protected org.apache.maven.artifact.factory.ArtifactFactory factory;
 56  
 
 57  
     /**
 58  
      * Used to look up Artifacts in the remote repository.
 59  
      *
 60  
      * @component
 61  
      */
 62  
     protected org.apache.maven.artifact.resolver.ArtifactResolver resolver;
 63  
 
 64  
     /**
 65  
      * Artifact collector, needed to resolve dependencies.
 66  
      *
 67  
      * @component role="org.apache.maven.artifact.resolver.ArtifactCollector"
 68  
      * @required
 69  
      * @readonly
 70  
      */
 71  
     protected ArtifactCollector artifactCollector;
 72  
 
 73  
     /**
 74  
      * @component role="org.apache.maven.artifact.metadata.ArtifactMetadataSource"
 75  
      *            hint="maven"
 76  
      * @required
 77  
      * @readonly
 78  
      */
 79  
     protected ArtifactMetadataSource artifactMetadataSource;
 80  
 
 81  
     /**
 82  
      * Location of the local repository.
 83  
      *
 84  
      * @parameter expression="${localRepository}"
 85  
      * @readonly
 86  
      * @required
 87  
      */
 88  
     protected org.apache.maven.artifact.repository.ArtifactRepository local;
 89  
 
 90  
     /**
 91  
      * List of Remote Repositories used by the resolver
 92  
      *
 93  
      * @parameter expression="${project.remoteArtifactRepositories}"
 94  
      * @readonly
 95  
      * @required
 96  
      */
 97  
     protected java.util.List remoteRepos;
 98  
 
 99  
     /**
 100  
      * To look up Archiver/UnArchiver implementations
 101  
      *
 102  
      * @component
 103  
      */
 104  
     protected ArchiverManager archiverManager;
 105  
 
 106  
     /**
 107  
      * POM
 108  
      *
 109  
      * @parameter expression="${project}"
 110  
      * @readonly
 111  
      * @required
 112  
      */
 113  
     protected MavenProject project;
 114  
 
 115  
     /**
 116  
      * Contains the full list of projects in the reactor.
 117  
      *
 118  
      * @parameter expression="${reactorProjects}"
 119  
      * @required
 120  
      * @readonly
 121  
      */
 122  
     protected List reactorProjects;
 123  
 
 124  
     /**
 125  
      * If the plugin should be silent.
 126  
      *
 127  
      * @optional
 128  
      * @since 2.0
 129  
      * @parameter expression="${silent}"
 130  
      *            default-value="false"
 131  
      */
 132  
     public boolean silent;
 133  
 
 134  
     /**
 135  
      * Output absolute filename for resolved artifacts
 136  
      *
 137  
      * @optional
 138  
      * @since 2.0
 139  
      * @parameter expression="${outputAbsoluteArtifactFilename}"
 140  
      *            default-value="false"
 141  
      */
 142  
     protected boolean outputAbsoluteArtifactFilename;
 143  
 
 144  
     private Log log;
 145  
 
 146  
     /**
 147  
      * @return Returns the log.
 148  
      */
 149  
     public Log getLog ()
 150  
     {
 151  746
         if ( silent )
 152  
         {
 153  712
             log = new DependencySilentLog();
 154  
         }
 155  
         else
 156  
         {
 157  34
             log = super.getLog();
 158  
         }
 159  
 
 160  746
         return this.log;
 161  
     }
 162  
 
 163  
     /**
 164  
      * @return Returns the archiverManager.
 165  
      */
 166  
     public ArchiverManager getArchiverManager ()
 167  
     {
 168  49
         return this.archiverManager;
 169  
     }
 170  
 
 171  
     /**
 172  
      * Does the actual copy of the file and logging.
 173  
      *
 174  
      * @param artifact represents the file to copy.
 175  
      * @param destFile file name of destination file.
 176  
      *
 177  
      * @throws MojoExecutionException with a message if an
 178  
      *             error occurs.
 179  
      */
 180  
     protected void copyFile ( File artifact, File destFile )
 181  
         throws MojoExecutionException
 182  
     {
 183  174
         Log theLog = this.getLog();
 184  
         try
 185  
         {
 186  174
             theLog.info( "Copying "
 187  
                 + ( this.outputAbsoluteArtifactFilename ? artifact.getAbsolutePath() : artifact.getName() ) + " to "
 188  
                 + destFile );
 189  174
             FileUtils.copyFile( artifact, destFile );
 190  
 
 191  
         }
 192  0
         catch ( Exception e )
 193  
         {
 194  0
             throw new MojoExecutionException( "Error copying artifact from " + artifact + " to " + destFile, e );
 195  174
         }
 196  174
     }
 197  
 
 198  
     protected void unpack ( File file, File location )
 199  
         throws MojoExecutionException
 200  
     {
 201  0
         unpack( file, location, null, null );
 202  0
     }
 203  
 
 204  
     /**
 205  
      * Unpacks the archive file.
 206  
      *
 207  
      * @param file File to be unpacked.
 208  
      * @param location Location where to put the unpacked
 209  
      *            files.
 210  
      * @param includes Comma separated list of file patterns
 211  
      *            to include i.e. **&#47;.xml,
 212  
      *            **&#47;*.properties
 213  
      * @param excludes Comma separated list of file patterns
 214  
      *            to exclude i.e. **&#47;*.xml,
 215  
      *            **&#47;*.properties
 216  
      */
 217  
     protected void unpack ( File file, File location, String includes, String excludes )
 218  
         throws MojoExecutionException
 219  
     {
 220  
         try
 221  
         {
 222  183
             getLog().info(
 223  
                             "Unpacking " + file.getPath() + " to\n  " + location.getPath()
 224  
                                 + "\n   with includes " + includes + " and excludes:" + excludes );
 225  
 
 226  183
             location.mkdirs();
 227  
 
 228  
             UnArchiver unArchiver;
 229  
 
 230  183
             unArchiver = archiverManager.getUnArchiver( file );
 231  
 
 232  183
             unArchiver.setSourceFile( file );
 233  
 
 234  183
             unArchiver.setDestDirectory( location );
 235  
 
 236  183
             if ( StringUtils.isNotEmpty( excludes ) || StringUtils.isNotEmpty( includes ) )
 237  
             {
 238  
                 // Create the selectors that will filter
 239  
                 // based on include/exclude parameters
 240  
                 // MDEP-47
 241  54
                 IncludeExcludeFileSelector[] selectors = new IncludeExcludeFileSelector[] { new IncludeExcludeFileSelector() };
 242  
 
 243  54
                 if ( StringUtils.isNotEmpty( excludes ) )
 244  
                 {
 245  25
                     selectors[0].setExcludes( excludes.split( "," ) );
 246  
                 }
 247  
 
 248  54
                 if ( StringUtils.isNotEmpty( includes ) )
 249  
                 {
 250  29
                     selectors[0].setIncludes( includes.split( "," ) );
 251  
                 }
 252  
 
 253  54
                 unArchiver.setFileSelectors( selectors );
 254  
             }
 255  183
             if ( this.silent )
 256  
             {
 257  170
                 silenceUnarchiver( unArchiver );
 258  
             }
 259  
 
 260  183
             unArchiver.extract();
 261  
         }
 262  0
         catch ( NoSuchArchiverException e )
 263  
         {
 264  0
             throw new MojoExecutionException( "Unknown archiver type", e );
 265  
         }
 266  0
         catch ( ArchiverException e )
 267  
         {
 268  0
             e.printStackTrace();
 269  0
             throw new MojoExecutionException( "Error unpacking file: " + file + " to: " + location + "\r\n"
 270  
                 + e.toString(), e );
 271  183
         }
 272  183
     }
 273  
 
 274  
     private void silenceUnarchiver ( UnArchiver unArchiver )
 275  
     {
 276  
         // dangerous but handle any errors. It's the only
 277  
         // way to silence the
 278  
         // unArchiver.
 279  
         try
 280  
         {
 281  170
             Field field = ReflectionUtils.getFieldByNameIncludingSuperclasses( "logger", unArchiver.getClass() );
 282  
 
 283  170
             field.setAccessible( true );
 284  
 
 285  170
             field.set( unArchiver, this.getLog() );
 286  
         }
 287  0
         catch ( Exception e )
 288  
         {
 289  
             // was a nice try. Don't bother logging because
 290  
             // the log is silent.
 291  170
         }
 292  170
     }
 293  
 
 294  
     /**
 295  
      * @return Returns the factory.
 296  
      */
 297  
     public org.apache.maven.artifact.factory.ArtifactFactory getFactory ()
 298  
     {
 299  0
         return this.factory;
 300  
     }
 301  
 
 302  
     /**
 303  
      * @param factory The factory to set.
 304  
      */
 305  
     public void setFactory ( org.apache.maven.artifact.factory.ArtifactFactory factory )
 306  
     {
 307  59
         this.factory = factory;
 308  59
     }
 309  
 
 310  
     /**
 311  
      * @return Returns the project.
 312  
      */
 313  
     public MavenProject getProject ()
 314  
     {
 315  225
         return this.project;
 316  
     }
 317  
 
 318  
     /**
 319  
      * @return Returns the local.
 320  
      */
 321  
     public org.apache.maven.artifact.repository.ArtifactRepository getLocal ()
 322  
     {
 323  0
         return this.local;
 324  
     }
 325  
 
 326  
     /**
 327  
      * @param local The local to set.
 328  
      */
 329  
     public void setLocal ( org.apache.maven.artifact.repository.ArtifactRepository local )
 330  
     {
 331  50
         this.local = local;
 332  50
     }
 333  
 
 334  
     /**
 335  
      * @return Returns the remoteRepos.
 336  
      */
 337  
     public java.util.List getRemoteRepos ()
 338  
     {
 339  0
         return this.remoteRepos;
 340  
     }
 341  
 
 342  
     /**
 343  
      * @param remoteRepos The remoteRepos to set.
 344  
      */
 345  
     public void setRemoteRepos ( java.util.List remoteRepos )
 346  
     {
 347  0
         this.remoteRepos = remoteRepos;
 348  0
     }
 349  
 
 350  
     /**
 351  
      * @return Returns the resolver.
 352  
      */
 353  
     public org.apache.maven.artifact.resolver.ArtifactResolver getResolver ()
 354  
     {
 355  0
         return this.resolver;
 356  
     }
 357  
 
 358  
     /**
 359  
      * @param resolver The resolver to set.
 360  
      */
 361  
     public void setResolver ( org.apache.maven.artifact.resolver.ArtifactResolver resolver )
 362  
     {
 363  60
         this.resolver = resolver;
 364  60
     }
 365  
 
 366  
     /**
 367  
      * @param archiverManager The archiverManager to set.
 368  
      */
 369  
     public void setArchiverManager ( ArchiverManager archiverManager )
 370  
     {
 371  0
         this.archiverManager = archiverManager;
 372  0
     }
 373  
 
 374  
     /**
 375  
      * @return Returns the artifactCollector.
 376  
      */
 377  
     public ArtifactCollector getArtifactCollector ()
 378  
     {
 379  0
         return this.artifactCollector;
 380  
     }
 381  
 
 382  
     /**
 383  
      * @param theArtifactCollector The artifactCollector to
 384  
      *            set.
 385  
      */
 386  
     public void setArtifactCollector ( ArtifactCollector theArtifactCollector )
 387  
     {
 388  50
         this.artifactCollector = theArtifactCollector;
 389  50
     }
 390  
 
 391  
     /**
 392  
      * @return Returns the artifactMetadataSource.
 393  
      */
 394  
     public ArtifactMetadataSource getArtifactMetadataSource ()
 395  
     {
 396  0
         return this.artifactMetadataSource;
 397  
     }
 398  
 
 399  
     /**
 400  
      * @param theArtifactMetadataSource The
 401  
      *            artifactMetadataSource to set.
 402  
      */
 403  
     public void setArtifactMetadataSource ( ArtifactMetadataSource theArtifactMetadataSource )
 404  
     {
 405  0
         this.artifactMetadataSource = theArtifactMetadataSource;
 406  0
     }
 407  
 }