View Javadoc

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      private String mavenProjectId = AntRunMojo.DEFAULT_MAVEN_PROJECT_REFID;
54  
55      /**
56       * The id to store the dependencies fileset.
57       */
58      private String projectDependenciesId = DEFAULT_PROJECT_DEPENDENCIES_ID;
59  
60      public String getProjectDependenciesId()
61      {
62          return projectDependenciesId;
63      }
64  
65      public void setProjectDependenciesId( String projectDependenciesId )
66      {
67          this.projectDependenciesId = projectDependenciesId;
68      }
69  
70      /**
71       * The string to prepend to all dependency filesets.
72       */
73      private String prefix = "";
74  
75      /**
76       * A comma separated list of artifact types to include.
77       */
78      private String types = "";
79  
80      /**
81       * A comma separated list of dependency scopes to include.
82       */
83      private String scopes = "";
84  
85      public DependencyFilesetsTask()
86      {
87  
88      }
89  
90      public void execute()
91      {
92          if ( this.getProject().getReference( mavenProjectId ) == null )
93          {
94              throw new BuildException( "Maven project reference not found: " + mavenProjectId );
95          }
96  
97          MavenProject mavenProject = (MavenProject) this.getProject().getReference( "maven.project" );
98  
99          // Add filesets for depenedency artifacts
100         Set depArtifacts = filterArtifacts( mavenProject.getArtifacts() );
101 
102         FileSet dependenciesFileSet = new FileSet();
103         dependenciesFileSet.setProject( getProject() );
104         ArtifactRepository localRepository = (ArtifactRepository) getProject().getReference( "maven.local.repository" );
105         dependenciesFileSet.setDir( new File( localRepository.getBasedir() ) );
106 
107         for ( Iterator it = depArtifacts.iterator(); it.hasNext(); )
108         {
109             Artifact artifact = (Artifact) it.next();
110 
111             String relativeArtifactPath = localRepository.pathOf( artifact );
112             dependenciesFileSet.createInclude().setName( relativeArtifactPath );
113 
114             String fileSetName = getPrefix() + artifact.getDependencyConflictId();
115 
116             FileSet singleArtifactFileSet = new FileSet();
117             singleArtifactFileSet.setProject( getProject() );
118             singleArtifactFileSet.setFile( artifact.getFile() );
119             getProject().addReference( fileSetName, singleArtifactFileSet );
120         }
121 
122         getProject().addReference( ( getPrefix() + projectDependenciesId ), dependenciesFileSet );
123     }
124 
125     public String getMavenProjectId()
126     {
127         return mavenProjectId;
128     }
129 
130     public void setMavenProjectId( String mavenProjectId )
131     {
132         this.mavenProjectId = mavenProjectId;
133     }
134 
135     public String getPrefix()
136     {
137         if ( prefix == null )
138         {
139             prefix = "";
140         }
141         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         this.prefix = prefix;
150     }
151 
152     public String getTypes()
153     {
154         return types;
155     }
156 
157     public void setTypes( String types )
158     {
159         this.types = types;
160     }
161 
162     public String getScopes()
163     {
164         return scopes;
165     }
166 
167     public void setScopes( String scopes )
168     {
169         this.scopes = scopes;
170     }
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         if ( scopes == null )
181         {
182             scopes = "";
183         }
184         if ( types == null )
185         {
186             types = "";
187         }
188 
189         if ( scopes.equals( "" ) && types.equals( "" ) )
190         {
191             return artifacts;
192         }
193 
194         AndArtifactFilter filter = new AndArtifactFilter();
195         if ( !scopes.equals( "" ) )
196         {
197             filter.add( new SpecificScopesArtifactFilter( getScopes() ) );
198         }
199         if ( !types.equals( "" ) )
200         {
201             filter.add( new TypesArtifactFilter( getTypes() ) );
202         }
203 
204         Set artifactsResult = new LinkedHashSet();
205         for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
206         {
207             Artifact artifact = (Artifact) iter.next();
208             if ( filter.include( artifact ) )
209             {
210                 artifactsResult.add( artifact );
211             }
212         }
213         return artifactsResult;
214     }
215 }