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.plugins.antrun.taskconfig.DependencyFilesetsConfiguration;
29  import org.apache.maven.project.MavenProject;
30  import org.apache.tools.ant.BuildException;
31  import org.apache.tools.ant.Task;
32  import org.apache.tools.ant.types.FileSet;
33  
34  import java.io.File;
35  import java.util.LinkedHashSet;
36  import java.util.Set;
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       * The project ref Id of the project being used.
49       */
50      private String mavenProjectId = AntRunMojo.DEFAULT_MAVEN_PROJECT_REFID;
51  
52      private DependencyFilesetsConfiguration configuration = new DependencyFilesetsConfiguration();
53  
54      /** {@inheritDoc} */
55      @Override
56      public void execute()
57      {
58          if ( this.getProject().getReference( mavenProjectId ) == null )
59          {
60              throw new BuildException( "Maven project reference not found: " + mavenProjectId );
61          }
62  
63          MavenProject mavenProject = this.getProject().getReference( "maven.project" );
64  
65          // Add filesets for depenedency artifacts
66          Set<Artifact> depArtifacts = filterArtifacts( mavenProject.getArtifacts() );
67  
68          FileSet dependenciesFileSet = new FileSet();
69          dependenciesFileSet.setProject( getProject() );
70          ArtifactRepository localRepository = getProject().getReference( "maven.local.repository" );
71          dependenciesFileSet.setDir( new File( localRepository.getBasedir() ) );
72  
73          if ( depArtifacts.isEmpty() )
74          {
75              // For performance reasons in case of huge local repo, tell Ant to include a single thing, otherwise the
76              // whole directory is scanned (even though ** is excluded).
77              dependenciesFileSet.createInclude().setName( "." );
78              dependenciesFileSet.createExclude().setName( "**" );
79          }
80  
81          for ( Artifact artifact : depArtifacts )
82          {
83              String relativeArtifactPath = localRepository.pathOf( artifact );
84              dependenciesFileSet.createInclude().setName( relativeArtifactPath );
85  
86              String fileSetName = getPrefix() + artifact.getDependencyConflictId();
87  
88              FileSet singleArtifactFileSet = new FileSet();
89              singleArtifactFileSet.setProject( getProject() );
90              singleArtifactFileSet.setFile( artifact.getFile() );
91              getProject().addReference( fileSetName, singleArtifactFileSet );
92          }
93  
94          getProject().addReference( ( getPrefix() + getProjectDependenciesId() ), dependenciesFileSet );
95      }
96  
97      /**
98       * @return {@link #mavenProjectId}
99       */
100     public String getMavenProjectId()
101     {
102         return mavenProjectId;
103     }
104 
105     /**
106      * @param mavenProjectId {@link #mavenProjectId}
107      */
108     public void setMavenProjectId( String mavenProjectId )
109     {
110         this.mavenProjectId = mavenProjectId;
111     }
112 
113     /**
114      * @return prefix Prefix to be added to each of the dependency filesets
115      */
116     public String getPrefix()
117     {
118         String prefix = configuration.getPrefix();
119         if ( prefix == null )
120         {
121             prefix = "";
122         }
123         return prefix;
124     }
125 
126     /**
127      * Prefix to be added to each of the dependency filesets. Default is empty string.
128      * @param prefix String to prepend to all fileset IDs.
129      */
130     public void setPrefix( String prefix )
131     {
132         this.configuration.setPrefix( prefix );
133     }
134 
135     /**
136      * @return types Comma separated list of artifact types to include.
137      */
138     public String getTypes()
139     {
140         return this.configuration.getTypes();
141     }
142 
143     /**
144      * @param types Comma separated list of artifact types to include.
145      */
146     public void setTypes( String types )
147     {
148         this.configuration.setTypes( types );
149     }
150 
151     /**
152      * @return scopes Comma separated list of artifact scopes to include.
153      */
154     public String getScopes()
155     {
156         return this.configuration.getScopes();
157     }
158 
159     /**
160      * @param scopes Comma separated list of artifact scopes to include.
161      */
162     public void setScopes( String scopes )
163     {
164         this.configuration.setScopes( scopes );
165     }
166 
167     /**
168      * @return RefId for the fileset containing all project dependencies - default maven.project.dependencies
169      */
170     public String getProjectDependenciesId()
171     {
172         return this.configuration.getProjectDependenciesId();
173     }
174 
175     /**
176      * @param projectDependenciesId RefId for the fileset containing all project dependencies
177      */
178     public void setProjectDependenciesId( String projectDependenciesId )
179     {
180         this.configuration.setProjectDependenciesId( projectDependenciesId );
181     }
182 
183     /**
184      * Filter a set of artifacts using the scopes and type filters.
185      *
186      * @param artifacts {@link Artifact} set.
187      * @return The set of filtered artifacts.
188      */
189     public Set<Artifact> filterArtifacts( Set<Artifact> artifacts )
190     {
191         String scopes = getScopes();
192         if ( scopes == null )
193         {
194             scopes = "";
195         }
196         
197         String types = getTypes();
198         if ( types == null )
199         {
200             types = "";
201         }
202 
203         if ( "".equals( scopes ) && "".equals( types ) )
204         {
205             return artifacts;
206         }
207 
208         AndArtifactFilter filter = new AndArtifactFilter();
209         if ( !"".equals( scopes ) )
210         {
211             filter.add( new SpecificScopesArtifactFilter( getScopes() ) );
212         }
213         if ( !"".equals( types ) )
214         {
215             filter.add( new TypesArtifactFilter( getTypes() ) );
216         }
217 
218         Set<Artifact> artifactsResult = new LinkedHashSet<>();
219         for ( Artifact artifact : artifacts )
220         {
221             if ( filter.include( artifact ) )
222             {
223                 artifactsResult.add( artifact );
224             }
225         }
226         return artifactsResult;
227     }
228 }