Coverage Report - org.apache.maven.ant.tasks.DependencyFilesetsTask
 
Classes in this File Line Coverage Branch Coverage Complexity
DependencyFilesetsTask
0%
0/62
0%
0/22
2
 
 1  
 package org.apache.maven.ant.tasks;
 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.LinkedHashSet;
 24  
 import java.util.Iterator;
 25  
 import java.util.Set;
 26  
 
 27  
 import org.apache.maven.ant.tasks.support.SpecificScopesArtifactFilter;
 28  
 import org.apache.maven.ant.tasks.support.TypesArtifactFilter;
 29  
 import org.apache.maven.artifact.Artifact;
 30  
 import org.apache.maven.artifact.repository.ArtifactRepository;
 31  
 import org.apache.maven.artifact.resolver.filter.AndArtifactFilter;
 32  
 import org.apache.maven.plugin.antrun.AntRunMojo;
 33  
 import org.apache.maven.project.MavenProject;
 34  
 import org.apache.tools.ant.BuildException;
 35  
 import org.apache.tools.ant.Task;
 36  
 import org.apache.tools.ant.types.FileSet;
 37  
 
 38  
 /**
 39  
  * Ant task which create a fileset for each dependency in a Maven project, and a 
 40  
  * fileset containing all selected dependencies.
 41  
  * 
 42  
  * @author pgier
 43  
  */
 44  
 public class DependencyFilesetsTask
 45  
     extends Task
 46  
 {
 47  
 
 48  
     public final static String DEFAULT_PROJECT_DEPENDENCIES_ID = "maven.project.dependencies";
 49  
 
 50  
     /**
 51  
      * The project ref Id of the project being used.
 52  
      */
 53  0
     private String mavenProjectId = AntRunMojo.DEFAULT_MAVEN_PROJECT_REFID;
 54  
 
 55  
     /**
 56  
      * The id to store the dependencies fileset.
 57  
      */
 58  0
     private String projectDependenciesId = DEFAULT_PROJECT_DEPENDENCIES_ID;
 59  
 
 60  
     public String getProjectDependenciesId()
 61  
     {
 62  0
         return projectDependenciesId;
 63  
     }
 64  
 
 65  
     public void setProjectDependenciesId( String projectDependenciesId )
 66  
     {
 67  0
         this.projectDependenciesId = projectDependenciesId;
 68  0
     }
 69  
 
 70  
     /**
 71  
      * The string to prepend to all dependency filesets.
 72  
      */
 73  0
     private String prefix = "";
 74  
 
 75  
     /**
 76  
      * A comma separated list of artifact types to include.
 77  
      */
 78  0
     private String types = "";
 79  
 
 80  
     /**
 81  
      * A comma separated list of dependency scopes to include.
 82  
      */
 83  0
     private String scopes = "";
 84  
 
 85  
     public DependencyFilesetsTask()
 86  0
     {
 87  
 
 88  0
     }
 89  
 
 90  
     public void execute()
 91  
     {
 92  0
         if ( this.getProject().getReference( mavenProjectId ) == null )
 93  
         {
 94  0
             throw new BuildException( "Maven project reference not found: " + mavenProjectId );
 95  
         }
 96  
 
 97  0
         MavenProject mavenProject = (MavenProject) this.getProject().getReference( "maven.project" );
 98  
 
 99  
         // Add filesets for depenedency artifacts
 100  0
         Set depArtifacts = filterArtifacts( mavenProject.getArtifacts() );
 101  
 
 102  0
         FileSet dependenciesFileSet = new FileSet();
 103  0
         dependenciesFileSet.setProject( getProject() );
 104  0
         ArtifactRepository localRepository = (ArtifactRepository) getProject().getReference( "maven.local.repository" );
 105  0
         dependenciesFileSet.setDir( new File( localRepository.getBasedir() ) );
 106  
 
 107  0
         for ( Iterator it = depArtifacts.iterator(); it.hasNext(); )
 108  
         {
 109  0
             Artifact artifact = (Artifact) it.next();
 110  
 
 111  0
             String relativeArtifactPath = localRepository.pathOf( artifact );
 112  0
             dependenciesFileSet.createInclude().setName( relativeArtifactPath );
 113  
 
 114  0
             String fileSetName = getPrefix() + artifact.getDependencyConflictId();
 115  
 
 116  0
             FileSet singleArtifactFileSet = new FileSet();
 117  0
             singleArtifactFileSet.setProject( getProject() );
 118  0
             singleArtifactFileSet.setFile( artifact.getFile() );
 119  0
             getProject().addReference( fileSetName, singleArtifactFileSet );
 120  0
         }
 121  
 
 122  0
         getProject().addReference( ( getPrefix() + projectDependenciesId ), dependenciesFileSet );
 123  0
     }
 124  
 
 125  
     public String getMavenProjectId()
 126  
     {
 127  0
         return mavenProjectId;
 128  
     }
 129  
 
 130  
     public void setMavenProjectId( String mavenProjectId )
 131  
     {
 132  0
         this.mavenProjectId = mavenProjectId;
 133  0
     }
 134  
 
 135  
     public String getPrefix()
 136  
     {
 137  0
         if ( prefix == null )
 138  
         {
 139  0
             prefix = "";
 140  
         }
 141  0
         return prefix;
 142  
     }
 143  
 
 144  
     /**
 145  
      * Prefix to be added to each of the dependency filesets. Default is empty string.
 146  
      */
 147  
     public void setPrefix( String prefix )
 148  
     {
 149  0
         this.prefix = prefix;
 150  0
     }
 151  
 
 152  
     public String getTypes()
 153  
     {
 154  0
         return types;
 155  
     }
 156  
 
 157  
     public void setTypes( String types )
 158  
     {
 159  0
         this.types = types;
 160  0
     }
 161  
 
 162  
     public String getScopes()
 163  
     {
 164  0
         return scopes;
 165  
     }
 166  
 
 167  
     public void setScopes( String scopes )
 168  
     {
 169  0
         this.scopes = scopes;
 170  0
     }
 171  
 
 172  
     /**
 173  
      * Filter a set of artifacts using the scopes and type filters.
 174  
      * 
 175  
      * @param artifacts
 176  
      * @return
 177  
      */
 178  
     public Set filterArtifacts( Set artifacts )
 179  
     {
 180  0
         if ( scopes == null )
 181  
         {
 182  0
             scopes = "";
 183  
         }
 184  0
         if ( types == null )
 185  
         {
 186  0
             types = "";
 187  
         }
 188  
 
 189  0
         if ( scopes.equals( "" ) && types.equals( "" ) )
 190  
         {
 191  0
             return artifacts;
 192  
         }
 193  
 
 194  0
         AndArtifactFilter filter = new AndArtifactFilter();
 195  0
         if ( !scopes.equals( "" ) )
 196  
         {
 197  0
             filter.add( new SpecificScopesArtifactFilter( getScopes() ) );
 198  
         }
 199  0
         if ( !types.equals( "" ) )
 200  
         {
 201  0
             filter.add( new TypesArtifactFilter( getTypes() ) );
 202  
         }
 203  
 
 204  0
         Set artifactsResult = new LinkedHashSet();
 205  0
         for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
 206  
         {
 207  0
             Artifact artifact = (Artifact) iter.next();
 208  0
             if ( filter.include( artifact ) )
 209  
             {
 210  0
                 artifactsResult.add( artifact );
 211  
             }
 212  0
         }
 213  0
         return artifactsResult;
 214  
     }
 215  
 }