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