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.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
40
41
42
43
44 public class DependencyFilesetsTask
45 extends Task
46 {
47
48
49
50 private String mavenProjectId = AntRunMojo.DEFAULT_MAVEN_PROJECT_REFID;
51
52 private DependencyFilesetsConfiguration configuration = new DependencyFilesetsConfiguration();
53
54
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
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
76
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
99
100 public String getMavenProjectId()
101 {
102 return mavenProjectId;
103 }
104
105
106
107
108 public void setMavenProjectId( String mavenProjectId )
109 {
110 this.mavenProjectId = mavenProjectId;
111 }
112
113
114
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
128
129
130 public void setPrefix( String prefix )
131 {
132 this.configuration.setPrefix( prefix );
133 }
134
135
136
137
138 public String getTypes()
139 {
140 return this.configuration.getTypes();
141 }
142
143
144
145
146 public void setTypes( String types )
147 {
148 this.configuration.setTypes( types );
149 }
150
151
152
153
154 public String getScopes()
155 {
156 return this.configuration.getScopes();
157 }
158
159
160
161
162 public void setScopes( String scopes )
163 {
164 this.configuration.setScopes( scopes );
165 }
166
167
168
169
170 public String getProjectDependenciesId()
171 {
172 return this.configuration.getProjectDependenciesId();
173 }
174
175
176
177
178 public void setProjectDependenciesId( String projectDependenciesId )
179 {
180 this.configuration.setProjectDependenciesId( projectDependenciesId );
181 }
182
183
184
185
186
187
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 }