1 package org.apache.maven.ant.tasks;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
39
40
41
42
43 public class DependencyFilesetsTask
44 extends Task
45 {
46
47
48
49
50 public static final String DEFAULT_PROJECT_DEPENDENCIES_ID = "maven.project.dependencies";
51
52
53
54
55 private String mavenProjectId = AntRunMojo.DEFAULT_MAVEN_PROJECT_REFID;
56
57
58
59
60 private String projectDependenciesId = DEFAULT_PROJECT_DEPENDENCIES_ID;
61
62
63
64
65 public String getProjectDependenciesId()
66 {
67 return projectDependenciesId;
68 }
69
70
71
72
73 public void setProjectDependenciesId( String projectDependenciesId )
74 {
75 this.projectDependenciesId = projectDependenciesId;
76 }
77
78
79
80
81 private String prefix = "";
82
83
84
85
86 private String types = "";
87
88
89
90
91 private String scopes = "";
92
93
94
95
96 public DependencyFilesetsTask()
97 {
98
99 }
100
101
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
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
137
138 public String getMavenProjectId()
139 {
140 return mavenProjectId;
141 }
142
143
144
145
146 public void setMavenProjectId( String mavenProjectId )
147 {
148 this.mavenProjectId = mavenProjectId;
149 }
150
151
152
153
154 public String getPrefix()
155 {
156 if ( prefix == null )
157 {
158 prefix = "";
159 }
160 return prefix;
161 }
162
163
164
165
166
167 public void setPrefix( String prefix )
168 {
169 this.prefix = prefix;
170 }
171
172
173
174
175 public String getTypes()
176 {
177 return types;
178 }
179
180
181
182
183 public void setTypes( String types )
184 {
185 this.types = types;
186 }
187
188
189
190
191 public String getScopes()
192 {
193 return scopes;
194 }
195
196
197
198
199 public void setScopes( String scopes )
200 {
201 this.scopes = scopes;
202 }
203
204
205
206
207
208
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 }