View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.ant.tasks;
20  
21  import java.io.File;
22  import java.util.LinkedHashSet;
23  import java.util.Set;
24  
25  import org.apache.maven.ant.tasks.support.SpecificScopesArtifactFilter;
26  import org.apache.maven.ant.tasks.support.TypesArtifactFilter;
27  import org.apache.maven.artifact.Artifact;
28  import org.apache.maven.artifact.repository.ArtifactRepository;
29  import org.apache.maven.artifact.resolver.filter.AndArtifactFilter;
30  import org.apache.maven.plugins.antrun.AntRunMojo;
31  import org.apache.maven.plugins.antrun.taskconfig.DependencyFilesetsConfiguration;
32  import org.apache.maven.project.MavenProject;
33  import org.apache.tools.ant.BuildException;
34  import org.apache.tools.ant.Task;
35  import org.apache.tools.ant.types.FileSet;
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 extends Task {
44      /**
45       * The project ref Id of the project being used.
46       */
47      private String mavenProjectId = AntRunMojo.DEFAULT_MAVEN_PROJECT_REFID;
48  
49      private DependencyFilesetsConfiguration configuration = new DependencyFilesetsConfiguration();
50  
51      /** {@inheritDoc} */
52      @Override
53      public void execute() {
54          if (this.getProject().getReference(mavenProjectId) == null) {
55              throw new BuildException("Maven project reference not found: " + mavenProjectId);
56          }
57  
58          MavenProject mavenProject = this.getProject().getReference("maven.project");
59  
60          // Add filesets for depenedency artifacts
61          Set<Artifact> depArtifacts = filterArtifacts(mavenProject.getArtifacts());
62  
63          FileSet dependenciesFileSet = new FileSet();
64          dependenciesFileSet.setProject(getProject());
65          ArtifactRepository localRepository = getProject().getReference("maven.local.repository");
66          dependenciesFileSet.setDir(new File(localRepository.getBasedir()));
67  
68          if (depArtifacts.isEmpty()) {
69              // For performance reasons in case of huge local repo, tell Ant to include a single thing, otherwise the
70              // whole directory is scanned (even though ** is excluded).
71              dependenciesFileSet.createInclude().setName(".");
72              dependenciesFileSet.createExclude().setName("**");
73          }
74  
75          for (Artifact artifact : depArtifacts) {
76              String relativeArtifactPath = localRepository.pathOf(artifact);
77              dependenciesFileSet.createInclude().setName(relativeArtifactPath);
78  
79              String fileSetName = getPrefix() + artifact.getDependencyConflictId();
80  
81              FileSet singleArtifactFileSet = new FileSet();
82              singleArtifactFileSet.setProject(getProject());
83              singleArtifactFileSet.setFile(artifact.getFile());
84              getProject().addReference(fileSetName, singleArtifactFileSet);
85          }
86  
87          getProject().addReference((getPrefix() + getProjectDependenciesId()), dependenciesFileSet);
88      }
89  
90      /**
91       * @return {@link #mavenProjectId}
92       */
93      public String getMavenProjectId() {
94          return mavenProjectId;
95      }
96  
97      /**
98       * @param mavenProjectId {@link #mavenProjectId}
99       */
100     public void setMavenProjectId(String mavenProjectId) {
101         this.mavenProjectId = mavenProjectId;
102     }
103 
104     /**
105      * @return prefix Prefix to be added to each of the dependency filesets
106      */
107     public String getPrefix() {
108         String prefix = configuration.getPrefix();
109         if (prefix == null) {
110             prefix = "";
111         }
112         return prefix;
113     }
114 
115     /**
116      * Prefix to be added to each of the dependency filesets. Default is empty string.
117      * @param prefix String to prepend to all fileset IDs.
118      */
119     public void setPrefix(String prefix) {
120         this.configuration.setPrefix(prefix);
121     }
122 
123     /**
124      * @return types Comma separated list of artifact types to include.
125      */
126     public String getTypes() {
127         return this.configuration.getTypes();
128     }
129 
130     /**
131      * @param types Comma separated list of artifact types to include.
132      */
133     public void setTypes(String types) {
134         this.configuration.setTypes(types);
135     }
136 
137     /**
138      * @return scopes Comma separated list of artifact scopes to include.
139      */
140     public String getScopes() {
141         return this.configuration.getScopes();
142     }
143 
144     /**
145      * @param scopes Comma separated list of artifact scopes to include.
146      */
147     public void setScopes(String scopes) {
148         this.configuration.setScopes(scopes);
149     }
150 
151     /**
152      * @return RefId for the fileset containing all project dependencies - default maven.project.dependencies
153      */
154     public String getProjectDependenciesId() {
155         return this.configuration.getProjectDependenciesId();
156     }
157 
158     /**
159      * @param projectDependenciesId RefId for the fileset containing all project dependencies
160      */
161     public void setProjectDependenciesId(String projectDependenciesId) {
162         this.configuration.setProjectDependenciesId(projectDependenciesId);
163     }
164 
165     /**
166      * Filter a set of artifacts using the scopes and type filters.
167      *
168      * @param artifacts {@link Artifact} set.
169      * @return The set of filtered artifacts.
170      */
171     public Set<Artifact> filterArtifacts(Set<Artifact> artifacts) {
172         String scopes = getScopes();
173         if (scopes == null) {
174             scopes = "";
175         }
176 
177         String types = getTypes();
178         if (types == null) {
179             types = "";
180         }
181 
182         if ("".equals(scopes) && "".equals(types)) {
183             return artifacts;
184         }
185 
186         AndArtifactFilter filter = new AndArtifactFilter();
187         if (!"".equals(scopes)) {
188             filter.add(new SpecificScopesArtifactFilter(getScopes()));
189         }
190         if (!"".equals(types)) {
191             filter.add(new TypesArtifactFilter(getTypes()));
192         }
193 
194         Set<Artifact> artifactsResult = new LinkedHashSet<>();
195         for (Artifact artifact : artifacts) {
196             if (filter.include(artifact)) {
197                 artifactsResult.add(artifact);
198             }
199         }
200         return artifactsResult;
201     }
202 }