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.plugins.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     @Override
103     public void execute()
104     {
105         if ( this.getProject().getReference( mavenProjectId ) == null )
106         {
107             throw new BuildException( "Maven project reference not found: " + mavenProjectId );
108         }
109 
110         MavenProject mavenProject = this.getProject().getReference( "maven.project" );
111 
112         // Add filesets for depenedency artifacts
113         Set<Artifact> depArtifacts = filterArtifacts( mavenProject.getArtifacts() );
114 
115         FileSet dependenciesFileSet = new FileSet();
116         dependenciesFileSet.setProject( getProject() );
117         ArtifactRepository localRepository = getProject().getReference( "maven.local.repository" );
118         dependenciesFileSet.setDir( new File( localRepository.getBasedir() ) );
119 
120         if ( depArtifacts.isEmpty() )
121         {
122             // For performance reasons in case of huge local repo, tell Ant to include a single thing, otherwise the
123             // whole directory is scanned (even though ** is excluded).
124             dependenciesFileSet.createInclude().setName( "." );
125             dependenciesFileSet.createExclude().setName( "**" );
126         }
127 
128         for ( Artifact artifact : depArtifacts )
129         {
130             String relativeArtifactPath = localRepository.pathOf( artifact );
131             dependenciesFileSet.createInclude().setName( relativeArtifactPath );
132 
133             String fileSetName = getPrefix() + artifact.getDependencyConflictId();
134 
135             FileSet singleArtifactFileSet = new FileSet();
136             singleArtifactFileSet.setProject( getProject() );
137             singleArtifactFileSet.setFile( artifact.getFile() );
138             getProject().addReference( fileSetName, singleArtifactFileSet );
139         }
140 
141         getProject().addReference( ( getPrefix() + projectDependenciesId ), dependenciesFileSet );
142     }
143 
144     /**
145      * @return {@link #mavenProjectId}
146      */
147     public String getMavenProjectId()
148     {
149         return mavenProjectId;
150     }
151 
152     /**
153      * @param mavenProjectId {@link #mavenProjectId}
154      */
155     public void setMavenProjectId( String mavenProjectId )
156     {
157         this.mavenProjectId = mavenProjectId;
158     }
159 
160     /**
161      * @return {@link #prefix}
162      */
163     public String getPrefix()
164     {
165         if ( prefix == null )
166         {
167             prefix = "";
168         }
169         return prefix;
170     }
171 
172     /**
173      * Prefix to be added to each of the dependency filesets. Default is empty string.
174      * @param prefix {@link #prefix}
175      */
176     public void setPrefix( String prefix )
177     {
178         this.prefix = prefix;
179     }
180 
181     /**
182      * @return {@link #types}
183      */
184     public String getTypes()
185     {
186         return types;
187     }
188 
189     /**
190      * @param types {@link #types}
191      */
192     public void setTypes( String types )
193     {
194         this.types = types;
195     }
196 
197     /**
198      * @return {@link #scopes}
199      */
200     public String getScopes()
201     {
202         return scopes;
203     }
204 
205     /**
206      * @param scopes {@link #scopes}
207      */
208     public void setScopes( String scopes )
209     {
210         this.scopes = scopes;
211     }
212 
213     /**
214      * Filter a set of artifacts using the scopes and type filters.
215      *
216      * @param artifacts {@link Artifact} set.
217      * @return The set of filtered artifacts.
218      */
219     public Set<Artifact> filterArtifacts( Set<Artifact> artifacts )
220     {
221         if ( scopes == null )
222         {
223             scopes = "";
224         }
225         if ( types == null )
226         {
227             types = "";
228         }
229 
230         if ( scopes.equals( "" ) && types.equals( "" ) )
231         {
232             return artifacts;
233         }
234 
235         AndArtifactFilter filter = new AndArtifactFilter();
236         if ( !scopes.equals( "" ) )
237         {
238             filter.add( new SpecificScopesArtifactFilter( getScopes() ) );
239         }
240         if ( !types.equals( "" ) )
241         {
242             filter.add( new TypesArtifactFilter( getTypes() ) );
243         }
244 
245         Set<Artifact> artifactsResult = new LinkedHashSet<>();
246         for ( Artifact artifact : artifacts )
247         {
248             if ( filter.include( artifact ) )
249             {
250                 artifactsResult.add( artifact );
251             }
252         }
253         return artifactsResult;
254     }
255 }