Coverage Report - org.apache.maven.plugin.dependency.fromConfiguration.AbstractFromConfigurationMojo
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractFromConfigurationMojo
91%
62/68
88%
37/42
2.188
 
 1  
 package org.apache.maven.plugin.dependency.fromConfiguration;
 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.util.ArrayList;
 24  
 import java.util.Iterator;
 25  
 import java.util.List;
 26  
 
 27  
 import org.apache.maven.artifact.Artifact;
 28  
 import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
 29  
 import org.apache.maven.artifact.resolver.ArtifactResolutionException;
 30  
 import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
 31  
 import org.apache.maven.artifact.versioning.VersionRange;
 32  
 import org.apache.maven.model.Dependency;
 33  
 import org.apache.maven.plugin.MojoExecutionException;
 34  
 import org.apache.maven.plugin.dependency.AbstractDependencyMojo;
 35  
 import org.apache.maven.plugin.dependency.utils.DependencyUtil;
 36  
 import org.apache.maven.plugin.dependency.utils.filters.ArtifactItemFilter;
 37  
 import org.apache.maven.shared.artifact.filter.collection.ArtifactFilterException;
 38  
 import org.codehaus.plexus.util.StringUtils;
 39  
 
 40  
 /**
 41  
  * Abstract Parent class used by mojos that get Artifact information from the
 42  
  * plugin configuration as an ArrayList of ArtifactItems
 43  
  * 
 44  
  * @see ArtifactItem
 45  
  * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
 46  
  * @version $Id: AbstractFromConfigurationMojo.java 728546 2008-12-21 22:56:51Z bentmann $
 47  
  * 
 48  
  */
 49  51
 public abstract class AbstractFromConfigurationMojo
 50  
     extends AbstractDependencyMojo
 51  
 {
 52  
 
 53  
     /**
 54  
      * Default location used for mojo unless overridden in ArtifactItem
 55  
      * 
 56  
      * @parameter expression="${outputDirectory}"
 57  
      *            default-value="${project.build.directory}/dependency"
 58  
      * @optional
 59  
      * @since 1.0
 60  
      */
 61  
     private File outputDirectory;
 62  
 
 63  
     /**
 64  
      * Overwrite release artifacts
 65  
      * 
 66  
      * @optional
 67  
      * @since 1.0
 68  
      * @parameter expression="${mdep.overWriteReleases}" default-value="false"
 69  
      */
 70  
     private boolean overWriteReleases;
 71  
 
 72  
     /**
 73  
      * Overwrite snapshot artifacts
 74  
      * 
 75  
      * @optional
 76  
      * @since 1.0
 77  
      * @parameter expression="${mdep.overWriteSnapshots}" default-value="false"
 78  
      */
 79  
     private boolean overWriteSnapshots;
 80  
 
 81  
     /**
 82  
      * Overwrite if newer
 83  
      * 
 84  
      * @optional
 85  
      * @since 2.0
 86  
      * @parameter expression="${mdep.overIfNewer}" default-value="true"
 87  
      */
 88  
     private boolean overWriteIfNewer;
 89  
 
 90  
     /**
 91  
      * Collection of ArtifactItems to work on. (ArtifactItem contains groupId,
 92  
      * artifactId, version, type, classifier, location, destFile, markerFile and overwrite.)
 93  
      * See "Usage" and "Javadoc" for details.
 94  
      * 
 95  
      * @parameter
 96  
      * @required
 97  
      * @since 1.0
 98  
      */
 99  
     private ArrayList artifactItems;
 100  
 
 101  
     abstract ArtifactItemFilter getMarkedArtifactFilter( ArtifactItem item );
 102  
 
 103  
     /**
 104  
      * Preprocesses the list of ArtifactItems. This method defaults the
 105  
      * outputDirectory if not set and creates the output Directory if it doesn't
 106  
      * exist.
 107  
      * 
 108  
      * @param removeVersion
 109  
      *            remove the version from the filename.
 110  
      * @return An ArrayList of preprocessed ArtifactItems
 111  
      * 
 112  
      * @throws MojoExecutionException
 113  
      *             with a message if an error occurs.
 114  
      * 
 115  
      * @see ArtifactItem
 116  
      */
 117  
     protected ArrayList getProcessedArtifactItems( boolean removeVersion )
 118  
         throws MojoExecutionException
 119  
     {
 120  62
         if ( artifactItems == null || artifactItems.size() < 1 )
 121  
         {
 122  2
             throw new MojoExecutionException( "There are no artifactItems configured." );
 123  
         }
 124  
 
 125  60
         Iterator iter = artifactItems.iterator();
 126  137
         while ( iter.hasNext() )
 127  
         {
 128  83
             ArtifactItem artifactItem = (ArtifactItem) iter.next();
 129  83
             this.getLog().info( "Configured Artifact: " + artifactItem.toString() );
 130  
 
 131  83
             if ( artifactItem.getOutputDirectory() == null )
 132  
             {
 133  66
                 artifactItem.setOutputDirectory( this.outputDirectory );
 134  
             }
 135  83
             artifactItem.getOutputDirectory().mkdirs();
 136  
 
 137  
             // make sure we have a version.
 138  83
             if ( StringUtils.isEmpty( artifactItem.getVersion() ) )
 139  
             {
 140  12
                 fillMissingArtifactVersion( artifactItem );
 141  
             }
 142  
 
 143  81
             artifactItem.setArtifact( this.getArtifact( artifactItem ) );
 144  
 
 145  77
             if ( StringUtils.isEmpty( artifactItem.getDestFileName() ) )
 146  
             {
 147  63
                 artifactItem.setDestFileName( DependencyUtil.getFormattedFileName( artifactItem.getArtifact(),
 148  
                                                                                    removeVersion ) );
 149  
             }
 150  
 
 151  
             try
 152  
             {
 153  77
                 artifactItem.setNeedsProcessing( checkIfProcessingNeeded( artifactItem ) );
 154  
             }
 155  0
             catch ( ArtifactFilterException e )
 156  
             {
 157  0
                 throw new MojoExecutionException (e.getMessage(),e);
 158  77
             }
 159  77
         }
 160  54
         return artifactItems;
 161  
     }
 162  
 
 163  
     private boolean checkIfProcessingNeeded( ArtifactItem item )
 164  
         throws MojoExecutionException, ArtifactFilterException
 165  
     {
 166  77
         boolean result = false;
 167  77
         if ( StringUtils.equalsIgnoreCase( item.getOverWrite(), "true" ) )
 168  
         {
 169  0
             result = true;
 170  
         }
 171  
         else
 172  
         {
 173  77
             ArtifactItemFilter filter = getMarkedArtifactFilter( item );
 174  77
             result = filter.isArtifactIncluded( item );
 175  
         }
 176  77
         return result;
 177  
     }
 178  
 
 179  
     /**
 180  
      * Resolves the Artifact from the remote repository if nessessary. If no
 181  
      * version is specified, it will be retrieved from the dependency list or
 182  
      * from the DependencyManagement section of the pom.
 183  
      * 
 184  
      * @param artifactItem
 185  
      *            containing information about artifact from plugin
 186  
      *            configuration.
 187  
      * @return Artifact object representing the specified file.
 188  
      * 
 189  
      * @throws MojoExecutionException
 190  
      *             with a message if the version can't be found in
 191  
      *             DependencyManagement.
 192  
      */
 193  
     protected Artifact getArtifact( ArtifactItem artifactItem )
 194  
         throws MojoExecutionException
 195  
     {
 196  
         Artifact artifact;
 197  
 
 198  
         // Map managedVersions = createManagedVersionMap( factory,
 199  
         // project.getId(), project.getDependencyManagement() );
 200  
         VersionRange vr;
 201  
         try
 202  
         {
 203  81
             vr = VersionRange.createFromVersionSpec( artifactItem.getVersion() );
 204  
         }
 205  0
         catch ( InvalidVersionSpecificationException e1 )
 206  
         {
 207  
             // TODO Auto-generated catch block
 208  0
             e1.printStackTrace();
 209  0
             vr = VersionRange.createFromVersion( artifactItem.getVersion() );
 210  81
         }
 211  
 
 212  81
         if ( StringUtils.isEmpty( artifactItem.getClassifier() ) )
 213  
         {
 214  52
             artifact = factory.createDependencyArtifact( artifactItem.getGroupId(), artifactItem.getArtifactId(), vr,
 215  
                                                          artifactItem.getType(), null, Artifact.SCOPE_COMPILE );
 216  
         }
 217  
         else
 218  
         {
 219  29
             artifact = factory.createDependencyArtifact( artifactItem.getGroupId(), artifactItem.getArtifactId(), vr,
 220  
                                                          artifactItem.getType(), artifactItem.getClassifier(),
 221  
                                                          Artifact.SCOPE_COMPILE );
 222  
         }
 223  
 
 224  
         try
 225  
         {
 226  
             // mdep-50 - rolledback for now because it's breaking some
 227  
             // functionality.
 228  
             /*
 229  
              * List listeners = new ArrayList();
 230  
              * 
 231  
              * Set theSet = new HashSet(); theSet.add( artifact );
 232  
              * ArtifactResolutionResult artifactResolutionResult =
 233  
              * artifactCollector.collect( theSet, project .getArtifact(),
 234  
              * managedVersions, this.local,
 235  
              * project.getRemoteArtifactRepositories(), artifactMetadataSource,
 236  
              * null, listeners ); Iterator iter =
 237  
              * artifactResolutionResult.getArtifactResolutionNodes().iterator();
 238  
              * while ( iter.hasNext() ) { ResolutionNode node = (ResolutionNode)
 239  
              * iter.next(); artifact = node.getArtifact(); }
 240  
              */
 241  
 
 242  81
             resolver.resolve( artifact, remoteRepos, local );
 243  
         }
 244  2
         catch ( ArtifactResolutionException e )
 245  
         {
 246  2
             throw new MojoExecutionException( "Unable to resolve artifact.", e );
 247  
         }
 248  2
         catch ( ArtifactNotFoundException e )
 249  
         {
 250  2
             throw new MojoExecutionException( "Unable to find artifact.", e );
 251  77
         }
 252  
 
 253  77
         return artifact;
 254  
     }
 255  
 
 256  
     /**
 257  
      * Tries to find missing version from dependancy list and dependency
 258  
      * management. If found, the artifact is updated with the correct version.
 259  
      * 
 260  
      * It will first look for an exact match on artifactId/groupId/classifier/type and if it doesn't find
 261  
      * a match, it will try again looking for artifactId and groupId only.
 262  
      * @param artifact
 263  
      *            representing configured file.
 264  
      * @throws MojoExecutionException
 265  
      */
 266  
     private void fillMissingArtifactVersion( ArtifactItem artifact )
 267  
         throws MojoExecutionException
 268  
     {
 269  12
         if ( !findDependencyVersion( artifact, project.getDependencies(), false )
 270  
             && ( project.getDependencyManagement() == null || !findDependencyVersion( artifact, project
 271  
                 .getDependencyManagement().getDependencies(), false ) )
 272  
             && !findDependencyVersion( artifact, project.getDependencies(), true )
 273  
             && ( project.getDependencyManagement() == null || !findDependencyVersion( artifact, project
 274  
                 .getDependencyManagement().getDependencies(), true ) ) )
 275  
         {
 276  2
             throw new MojoExecutionException( "Unable to find artifact version of " + artifact.getGroupId() + ":"
 277  
                 + artifact.getArtifactId() + " in either dependency list or in project's dependency management." );
 278  
         }
 279  10
     }
 280  
 
 281  
     /**
 282  
      * Tries to find missing version from a list of dependencies. If found, the
 283  
      * artifact is updated with the correct version.
 284  
      * 
 285  
      * @param artifact
 286  
      *            representing configured file.
 287  
      * @param list
 288  
      *            list of dependencies to search.
 289  
      * @param looseMatch
 290  
      *            only look at artifactId and groupId
 291  
      * @return the found dependency
 292  
      */
 293  
     private boolean findDependencyVersion( ArtifactItem artifact, List list, boolean looseMatch )
 294  
     {
 295  27
         boolean result = false;
 296  
 
 297  47
         for ( int i = 0; i < list.size(); i++ )
 298  
         {
 299  30
             Dependency dependency = (Dependency) list.get( i );
 300  30
             if ( StringUtils.equals( dependency.getArtifactId(), artifact.getArtifactId() )
 301  
                 && StringUtils.equals( dependency.getGroupId(), artifact.getGroupId() )
 302  
                 && ( looseMatch || StringUtils.equals( dependency.getClassifier(), artifact.getClassifier() ) )
 303  
                 && ( looseMatch || StringUtils.equals( dependency.getType(), artifact.getType() ) ) )
 304  
             {
 305  
 
 306  10
                 artifact.setVersion( dependency.getVersion() );
 307  
 
 308  10
                 result = true;
 309  10
                 break;
 310  
             }
 311  
         }
 312  
 
 313  27
         return result;
 314  
     }
 315  
 
 316  
    /* private Map createManagedVersionMap( ArtifactFactory artifactFactory, String projectId,
 317  
                                          DependencyManagement dependencyManagement )
 318  
         throws MojoExecutionException
 319  
     {
 320  
         Map map;
 321  
         if ( dependencyManagement != null && dependencyManagement.getDependencies() != null )
 322  
         {
 323  
             map = new HashMap();
 324  
             for ( Iterator i = dependencyManagement.getDependencies().iterator(); i.hasNext(); )
 325  
             {
 326  
                 Dependency d = (Dependency) i.next();
 327  
 
 328  
                 try
 329  
                 {
 330  
                     VersionRange versionRange = VersionRange.createFromVersionSpec( d.getVersion() );
 331  
                     Artifact artifact = artifactFactory.createDependencyArtifact( d.getGroupId(), d.getArtifactId(),
 332  
                                                                                   versionRange, d.getType(), d
 333  
                                                                                       .getClassifier(), d.getScope(), d
 334  
                                                                                       .isOptional() );
 335  
                     map.put( d.getManagementKey(), artifact );
 336  
                 }
 337  
                 catch ( InvalidVersionSpecificationException e )
 338  
                 {
 339  
                     throw new MojoExecutionException( "Unable to parse version", e );
 340  
                 }
 341  
             }
 342  
         }
 343  
         else
 344  
         {
 345  
             map = Collections.EMPTY_MAP;
 346  
         }
 347  
         return map;
 348  
     }*/
 349  
 
 350  
     /**
 351  
      * @return Returns the artifactItems.
 352  
      */
 353  
     public ArrayList getArtifactItems()
 354  
     {
 355  2
         return this.artifactItems;
 356  
     }
 357  
 
 358  
     /**
 359  
      * @param theArtifactItems
 360  
      *            The artifactItems to set.
 361  
      */
 362  
     public void setArtifactItems( ArrayList theArtifactItems )
 363  
     {
 364  51
         this.artifactItems = theArtifactItems;
 365  51
     }
 366  
 
 367  
     /**
 368  
      * @return Returns the outputDirectory.
 369  
      */
 370  
     public File getOutputDirectory()
 371  
     {
 372  53
         return this.outputDirectory;
 373  
     }
 374  
 
 375  
     /**
 376  
      * @param theOutputDirectory
 377  
      *            The outputDirectory to set.
 378  
      */
 379  
     public void setOutputDirectory( File theOutputDirectory )
 380  
     {
 381  50
         this.outputDirectory = theOutputDirectory;
 382  50
     }
 383  
 
 384  
     /**
 385  
      * @return Returns the overWriteIfNewer.
 386  
      */
 387  
     public boolean isOverWriteIfNewer()
 388  
     {
 389  77
         return this.overWriteIfNewer;
 390  
     }
 391  
 
 392  
     /**
 393  
      * @param theOverWriteIfNewer
 394  
      *            The overWriteIfNewer to set.
 395  
      */
 396  
     public void setOverWriteIfNewer( boolean theOverWriteIfNewer )
 397  
     {
 398  10
         this.overWriteIfNewer = theOverWriteIfNewer;
 399  10
     }
 400  
 
 401  
     /**
 402  
      * @return Returns the overWriteReleases.
 403  
      */
 404  
     public boolean isOverWriteReleases()
 405  
     {
 406  77
         return this.overWriteReleases;
 407  
     }
 408  
 
 409  
     /**
 410  
      * @param theOverWriteReleases
 411  
      *            The overWriteReleases to set.
 412  
      */
 413  
     public void setOverWriteReleases( boolean theOverWriteReleases )
 414  
     {
 415  4
         this.overWriteReleases = theOverWriteReleases;
 416  4
     }
 417  
 
 418  
     /**
 419  
      * @return Returns the overWriteSnapshots.
 420  
      */
 421  
     public boolean isOverWriteSnapshots()
 422  
     {
 423  77
         return this.overWriteSnapshots;
 424  
     }
 425  
 
 426  
     /**
 427  
      * @param theOverWriteSnapshots
 428  
      *            The overWriteSnapshots to set.
 429  
      */
 430  
     public void setOverWriteSnapshots( boolean theOverWriteSnapshots )
 431  
     {
 432  2
         this.overWriteSnapshots = theOverWriteSnapshots;
 433  2
     }
 434  
 }