1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
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  
39  
40  
41  
42  
43  public class DependencyFilesetsTask extends Task {
44      
45  
46  
47      private String mavenProjectId = AntRunMojo.DEFAULT_MAVEN_PROJECT_REFID;
48  
49      private DependencyFilesetsConfiguration configuration = new DependencyFilesetsConfiguration();
50  
51      
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          
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              
70              
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  
92  
93      public String getMavenProjectId() {
94          return mavenProjectId;
95      }
96  
97      
98  
99  
100     public void setMavenProjectId(String mavenProjectId) {
101         this.mavenProjectId = mavenProjectId;
102     }
103 
104     
105 
106 
107     public String getPrefix() {
108         String prefix = configuration.getPrefix();
109         if (prefix == null) {
110             prefix = "";
111         }
112         return prefix;
113     }
114 
115     
116 
117 
118 
119     public void setPrefix(String prefix) {
120         this.configuration.setPrefix(prefix);
121     }
122 
123     
124 
125 
126     public String getTypes() {
127         return this.configuration.getTypes();
128     }
129 
130     
131 
132 
133     public void setTypes(String types) {
134         this.configuration.setTypes(types);
135     }
136 
137     
138 
139 
140     public String getScopes() {
141         return this.configuration.getScopes();
142     }
143 
144     
145 
146 
147     public void setScopes(String scopes) {
148         this.configuration.setScopes(scopes);
149     }
150 
151     
152 
153 
154     public String getProjectDependenciesId() {
155         return this.configuration.getProjectDependenciesId();
156     }
157 
158     
159 
160 
161     public void setProjectDependenciesId(String projectDependenciesId) {
162         this.configuration.setProjectDependenciesId(projectDependenciesId);
163     }
164 
165     
166 
167 
168 
169 
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 }