Coverage Report - org.apache.maven.artifact.ant.DependenciesTask
 
Classes in this File Line Coverage Branch Coverage Complexity
DependenciesTask
0%
0/148
0%
0/44
2.28
 
 1  
 package org.apache.maven.artifact.ant;
 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 org.apache.maven.artifact.Artifact;
 23  
 import org.apache.maven.artifact.factory.ArtifactFactory;
 24  
 import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
 25  
 import org.apache.maven.artifact.repository.ArtifactRepository;
 26  
 import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
 27  
 import org.apache.maven.artifact.resolver.ArtifactResolutionException;
 28  
 import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
 29  
 import org.apache.maven.artifact.resolver.ArtifactResolver;
 30  
 import org.apache.maven.artifact.resolver.filter.AndArtifactFilter;
 31  
 import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
 32  
 import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter;
 33  
 import org.apache.maven.model.Dependency;
 34  
 import org.apache.maven.project.artifact.InvalidDependencyVersionException;
 35  
 import org.apache.maven.project.artifact.MavenMetadataSource;
 36  
 import org.apache.tools.ant.BuildException;
 37  
 import org.apache.tools.ant.Project;
 38  
 import org.apache.tools.ant.types.FileList;
 39  
 import org.apache.tools.ant.types.FileSet;
 40  
 import org.apache.tools.ant.types.Path;
 41  
 import org.codehaus.plexus.util.StringUtils;
 42  
 
 43  
 import java.io.File;
 44  
 import java.io.IOException;
 45  
 import java.io.InputStream;
 46  
 import java.util.ArrayList;
 47  
 import java.util.Collections;
 48  
 import java.util.HashSet;
 49  
 import java.util.Iterator;
 50  
 import java.util.List;
 51  
 import java.util.Map;
 52  
 import java.util.Properties;
 53  
 import java.util.Set;
 54  
 
 55  
 /**
 56  
  * Dependencies task, using maven-artifact.
 57  
  *
 58  
  * @author <a href="mailto:brett@apache.org">Brett Porter</a>
 59  
  * @author <a href="mailto:hboutemy@apache.org">Herve Boutemy</a>
 60  
  * @version $Id: DependenciesTask.java 775222 2009-05-15 16:37:22Z pgier $
 61  
  */
 62  0
 public class DependenciesTask
 63  
     extends AbstractArtifactWithRepositoryTask
 64  
 {
 65  0
     private List dependencies = new ArrayList();
 66  
 
 67  
     private String pathId;
 68  
 
 69  
     private String filesetId;
 70  
 
 71  
     private String sourcesFilesetId;
 72  
     
 73  
     private String javadocFilesetId;
 74  
     
 75  
     private String versionsId;
 76  
 
 77  
     private String useScope;
 78  
 
 79  
     private String scopes;
 80  
 
 81  
     private String type;
 82  
 
 83  
     private boolean verbose;
 84  
     
 85  
     private boolean addArtifactFileSetRefs;
 86  
 
 87  
     protected void doExecute()
 88  
     {
 89  0
         showVersion();
 90  
         
 91  0
         if ( useScope != null && scopes != null )
 92  
         {
 93  0
             throw new BuildException( "You cannot specify both useScope and scopes in the dependencies task." );
 94  
         }
 95  
         
 96  0
         ArtifactRepository localRepo = createLocalArtifactRepository();
 97  0
         log( "Using local repository: " + localRepo.getBasedir(), Project.MSG_VERBOSE );
 98  
 
 99  0
         ArtifactResolver resolver = (ArtifactResolver) lookup( ArtifactResolver.ROLE );
 100  0
         ArtifactFactory artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
 101  0
         MavenMetadataSource metadataSource = (MavenMetadataSource) lookup( ArtifactMetadataSource.ROLE );
 102  
 
 103  0
         List dependencies = this.dependencies;
 104  
 
 105  0
         Pom pom = buildPom( localRepo );
 106  0
         if ( pom != null )
 107  
         {
 108  0
             if ( !dependencies.isEmpty() )
 109  
             {
 110  0
                 throw new BuildException( "You cannot specify both dependencies and a pom in the dependencies task" );
 111  
             }
 112  
 
 113  0
             dependencies = pom.getDependencies();
 114  
         }
 115  
         else
 116  
         {
 117  
             // we have to have some sort of Pom object in order to satisfy the requirements for building the
 118  
             // originating Artifact below...
 119  0
             pom = createDummyPom( localRepo );
 120  
         }
 121  
 
 122  0
         if ( dependencies.isEmpty() )
 123  
         {
 124  0
             log( "There were no dependencies specified", Project.MSG_WARN );
 125  
         }
 126  
 
 127  0
         log( "Resolving dependencies...", Project.MSG_VERBOSE );
 128  
 
 129  
         ArtifactResolutionResult result;
 130  
         Set artifacts;
 131  
 
 132  0
         List remoteArtifactRepositories = createRemoteArtifactRepositories( pom.getRepositories() );
 133  
 
 134  
         try
 135  
         {
 136  0
             artifacts = MavenMetadataSource.createArtifacts( artifactFactory, dependencies, null, null, null );
 137  
 
 138  0
             Artifact pomArtifact = artifactFactory.createBuildArtifact( pom.getGroupId(), pom.getArtifactId(), pom
 139  
                 .getVersion(), pom.getPackaging() );
 140  
 
 141  0
             List listeners = Collections.singletonList( new AntResolutionListener( getProject(), verbose ) );
 142  
 
 143  0
             Map managedDependencies = pom.getMavenProject().getManagedVersionMap();
 144  
 
 145  0
             ArtifactFilter filter = null;
 146  0
             if ( useScope != null )
 147  
             {
 148  0
                 filter = new ScopeArtifactFilter( useScope );
 149  
             }
 150  0
             if ( scopes != null )
 151  
             {
 152  0
                 filter = new SpecificScopesArtifactFilter( scopes );
 153  
             }
 154  0
             if ( type != null )
 155  
             {
 156  0
                 ArtifactFilter typeArtifactFilter = new TypesArtifactFilter( type );
 157  0
                 if ( filter != null )
 158  
                 {
 159  0
                     AndArtifactFilter andFilter = new AndArtifactFilter();
 160  0
                     andFilter.add( filter );
 161  0
                     andFilter.add( typeArtifactFilter );
 162  0
                     filter = andFilter;
 163  0
                 }
 164  
                 else
 165  
                 {
 166  0
                     filter = typeArtifactFilter;
 167  
                 }
 168  
             }
 169  
 
 170  0
             result = resolver.resolveTransitively( artifacts, pomArtifact, managedDependencies, localRepo,
 171  
                                                    remoteArtifactRepositories, metadataSource, filter, listeners );
 172  
         }
 173  0
         catch ( ArtifactResolutionException e )
 174  
         {
 175  0
             throw new BuildException( "Unable to resolve artifact: " + e.getMessage(), e );
 176  
         }
 177  0
         catch ( ArtifactNotFoundException e )
 178  
         {
 179  0
             throw new BuildException( "Dependency not found: " + e.getMessage(), e );
 180  
         }
 181  0
         catch ( InvalidDependencyVersionException e )
 182  
         {
 183  0
             throw new BuildException( e.getMessage(), e );
 184  0
         }
 185  
 
 186  0
         FileList fileList = new FileList();
 187  0
         fileList.setDir( getLocalRepository().getPath() );
 188  
 
 189  0
         FileSet fileSet = new FileSet();
 190  0
         fileSet.setProject( getProject() );
 191  0
         fileSet.setDir( fileList.getDir( getProject() ) );
 192  
 
 193  0
         FileSet sourcesFileSet = new FileSet();
 194  0
         sourcesFileSet.setDir( getLocalRepository().getPath() );
 195  
 
 196  0
         FileSet javadocsFileSet = new FileSet();
 197  0
         javadocsFileSet.setDir( getLocalRepository().getPath() );
 198  
 
 199  0
         Set versions = new HashSet();
 200  
         
 201  0
         for ( Iterator i = result.getArtifacts().iterator(); i.hasNext(); )
 202  
         {
 203  0
             Artifact artifact = (Artifact) i.next();
 204  
 
 205  0
             addArtifactToResult( localRepo, artifact, fileSet, fileList );
 206  
 
 207  0
             versions.add( artifact.getVersion() );
 208  
 
 209  0
             if ( sourcesFilesetId != null )
 210  
             {
 211  0
                 resolveSource( artifactFactory, resolver, remoteArtifactRepositories, localRepo,
 212  
                                artifact, "sources", sourcesFileSet );
 213  
             }
 214  
 
 215  0
             if ( javadocFilesetId != null )
 216  
             {
 217  0
                 resolveSource( artifactFactory, resolver, remoteArtifactRepositories, localRepo,
 218  
                                artifact, "javadoc", javadocsFileSet );
 219  
             }
 220  0
         }
 221  
 
 222  0
         if ( pathId != null )
 223  
         {
 224  0
             Path path = new Path( getProject() );
 225  0
             if ( versions.size() > 0 )
 226  
             {
 227  0
                 path.addFilelist( fileList );
 228  
             }
 229  0
             getProject().addReference( pathId, path );
 230  
         }
 231  
 
 232  0
         defineFilesetReference( filesetId, fileSet );
 233  
 
 234  0
         defineFilesetReference( sourcesFilesetId, sourcesFileSet );
 235  
         
 236  0
         defineFilesetReference( javadocFilesetId, javadocsFileSet );
 237  
         
 238  0
         if ( versionsId != null )
 239  
         {
 240  0
             String versionsValue = StringUtils.join( versions.iterator(), File.pathSeparator );
 241  0
             getProject().setNewProperty( versionsId, versionsValue );
 242  
         }
 243  0
     }
 244  
 
 245  
     private void defineFilesetReference( String id, FileSet fileSet )
 246  
     {
 247  0
         if ( id != null )
 248  
         {
 249  0
             if ( !fileSet.hasPatterns() )
 250  
             {
 251  0
                 fileSet.createExclude().setName( "**/**" );
 252  
             }
 253  0
             getProject().addReference( id, fileSet );
 254  
         }
 255  0
     }
 256  
 
 257  
     private void addArtifactToResult( ArtifactRepository localRepo, Artifact artifact, FileSet toFileSet )
 258  
     {
 259  0
         addArtifactToResult( localRepo, artifact, toFileSet, null );
 260  0
     }
 261  
 
 262  
     private void addArtifactToResult( ArtifactRepository localRepo, Artifact artifact, FileSet toFileSet,
 263  
                                       FileList toFileList )
 264  
     {
 265  0
         String filename = localRepo.pathOf( artifact );
 266  
 
 267  0
         toFileSet.createInclude().setName( filename );
 268  
 
 269  0
         if ( toFileList != null )
 270  
         {
 271  0
             FileList.FileName file = new FileList.FileName();
 272  0
             file.setName( filename );
 273  
 
 274  0
             toFileList.addConfiguredFile( file );
 275  
         }
 276  
 
 277  0
         getProject().setProperty( artifact.getDependencyConflictId(), artifact.getFile().getAbsolutePath() );
 278  
         
 279  0
         if ( isAddArtifactFileSetRefs() )
 280  
         {
 281  0
             FileSet artifactFileSet = new FileSet();
 282  0
             artifactFileSet.setFile( artifact.getFile() );
 283  0
             getProject().addReference( artifact.getDependencyConflictId(), artifactFileSet );
 284  
         }
 285  0
     }
 286  
 
 287  
     private void resolveSource( ArtifactFactory artifactFactory, ArtifactResolver resolver,
 288  
                                 List remoteArtifactRepositories, ArtifactRepository localRepo,
 289  
                                 Artifact artifact, String classifier, FileSet sourcesFileSet )
 290  
     {
 291  0
         Artifact sourceArtifact =
 292  
             artifactFactory.createArtifactWithClassifier( artifact.getGroupId(), artifact.getArtifactId(),
 293  
                                                           artifact.getVersion(), "java-source", classifier );
 294  
         try
 295  
         {
 296  0
             resolver.resolve( sourceArtifact, remoteArtifactRepositories, localRepo );
 297  
 
 298  0
             addArtifactToResult( localRepo, sourceArtifact, sourcesFileSet );
 299  
         }
 300  0
         catch ( ArtifactResolutionException e )
 301  
         {
 302  0
             throw new BuildException( "Unable to resolve artifact: " + e.getMessage(), e );
 303  
         }
 304  0
         catch ( ArtifactNotFoundException e )
 305  
         {
 306  
             // no source available: no problem, it's optional
 307  0
         }
 308  0
     }
 309  
 
 310  
     public List getDependencies()
 311  
     {
 312  0
         return dependencies;
 313  
     }
 314  
 
 315  
     public void addDependency( Dependency dependency )
 316  
     {
 317  0
         dependencies.add( dependency );
 318  0
     }
 319  
 
 320  
     public String getPathId()
 321  
     {
 322  0
         return pathId;
 323  
     }
 324  
 
 325  
     public void setPathId( String pathId )
 326  
     {
 327  0
         this.pathId = pathId;
 328  0
     }
 329  
 
 330  
     public String getFilesetId()
 331  
     {
 332  0
         return filesetId;
 333  
     }
 334  
 
 335  
     public void setSourcesFilesetId( String filesetId )
 336  
     {
 337  0
         this.sourcesFilesetId = filesetId;
 338  0
     }
 339  
 
 340  
     public String getSourcesFilesetId()
 341  
     {
 342  0
         return sourcesFilesetId;
 343  
     }
 344  
 
 345  
     public void setJavadocFilesetId( String filesetId )
 346  
     {
 347  0
         this.javadocFilesetId = filesetId;
 348  0
     }
 349  
 
 350  
     public String getJavadocFilesetId()
 351  
     {
 352  0
         return javadocFilesetId;
 353  
     }
 354  
 
 355  
     public void setFilesetId( String filesetId )
 356  
     {
 357  0
         this.filesetId = filesetId;
 358  0
     }
 359  
 
 360  
     public String getVersionsId()
 361  
     {
 362  0
         return versionsId;
 363  
     }
 364  
 
 365  
     public void setVersionsId( String versionsId )
 366  
     {
 367  0
         this.versionsId = versionsId;
 368  0
     }
 369  
 
 370  
     public void setVerbose( boolean verbose )
 371  
     {
 372  0
         this.verbose = verbose;
 373  0
     }
 374  
 
 375  
     public void setUseScope( String useScope )
 376  
     {
 377  0
         this.useScope = useScope;
 378  0
     }
 379  
 
 380  
     public void setType( String type )
 381  
     {
 382  0
         this.type = type;
 383  0
     }
 384  
 
 385  
     public String getScopes()
 386  
     {
 387  0
         return scopes;
 388  
     }
 389  
 
 390  
     public void setScopes( String scopes )
 391  
     {
 392  0
         this.scopes = scopes;
 393  0
     }
 394  
 
 395  
     private void showVersion()
 396  
     {
 397  
         InputStream resourceAsStream;
 398  
         try
 399  
         {
 400  0
             Properties properties = new Properties();
 401  0
             resourceAsStream = DependenciesTask.class.getClassLoader().getResourceAsStream(
 402  
                 "META-INF/maven/org.apache.maven/maven-ant-tasks/pom.properties" );
 403  0
             if ( resourceAsStream != null )
 404  
             {
 405  0
                 properties.load( resourceAsStream );
 406  
             }
 407  
 
 408  0
             String version = properties.getProperty( "version", "unknown" );
 409  0
             String builtOn = properties.getProperty( "builtOn" );
 410  0
             if ( builtOn != null )
 411  
             {
 412  0
                 log( "Maven Ant Tasks version: " + version + " built on " + builtOn, Project.MSG_VERBOSE );
 413  
             }
 414  
             else
 415  
             {
 416  0
                 log( "Maven Ant Tasks version: " + version, Project.MSG_VERBOSE );
 417  
             }
 418  
         }
 419  0
         catch ( IOException e )
 420  
         {
 421  0
             log( "Unable to determine version from Maven Ant Tasks JAR file: " + e.getMessage(), Project.MSG_WARN );
 422  0
         }
 423  0
     }
 424  
 
 425  
     public boolean isAddArtifactFileSetRefs()
 426  
     {
 427  0
         return addArtifactFileSetRefs;
 428  
     }
 429  
 
 430  
     public void setAddArtifactFileSetRefs( boolean addArtifactFileSetRefs )
 431  
     {
 432  0
         this.addArtifactFileSetRefs = addArtifactFileSetRefs;
 433  0
     }
 434  
 }