1 package org.apache.maven.artifact.ant;
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.artifact.Artifact;
23 import org.apache.maven.artifact.factory.ArtifactFactory;
24 import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
25 import org.apache.maven.artifact.repository.ArtifactRepository;
26 import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
27 import org.apache.maven.artifact.resolver.ArtifactResolutionException;
28 import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
29 import org.apache.maven.artifact.resolver.ArtifactResolver;
30 import org.apache.maven.artifact.resolver.filter.AndArtifactFilter;
31 import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
32 import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter;
33 import org.apache.maven.model.Dependency;
34 import org.apache.maven.project.artifact.InvalidDependencyVersionException;
35 import org.apache.maven.project.artifact.MavenMetadataSource;
36 import org.apache.tools.ant.BuildException;
37 import org.apache.tools.ant.Project;
38 import org.apache.tools.ant.types.FileList;
39 import org.apache.tools.ant.types.FileSet;
40 import org.apache.tools.ant.types.Path;
41 import org.codehaus.plexus.util.StringUtils;
42
43 import java.io.File;
44 import java.io.IOException;
45 import java.io.InputStream;
46 import java.util.ArrayList;
47 import java.util.Collections;
48 import java.util.HashSet;
49 import java.util.Iterator;
50 import java.util.List;
51 import java.util.Map;
52 import java.util.Properties;
53 import java.util.Set;
54
55
56
57
58
59
60
61
62 public class DependenciesTask
63 extends AbstractArtifactWithRepositoryTask
64 {
65 private List dependencies = new ArrayList();
66
67 private String pathId;
68
69 private String filesetId;
70
71 private String sourcesFilesetId;
72
73 private String javadocFilesetId;
74
75 private String versionsId;
76
77 private String useScope;
78
79 private String scopes;
80
81 private String type;
82
83 private boolean verbose;
84
85 private boolean addArtifactFileSetRefs;
86
87 protected void doExecute()
88 {
89 showVersion();
90
91 if ( useScope != null && scopes != null )
92 {
93 throw new BuildException( "You cannot specify both useScope and scopes in the dependencies task." );
94 }
95
96 ArtifactRepository localRepo = createLocalArtifactRepository();
97 log( "Using local repository: " + localRepo.getBasedir(), Project.MSG_VERBOSE );
98
99 ArtifactResolver resolver = (ArtifactResolver) lookup( ArtifactResolver.ROLE );
100 ArtifactFactory artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
101 MavenMetadataSource metadataSource = (MavenMetadataSource) lookup( ArtifactMetadataSource.ROLE );
102
103 List dependencies = this.dependencies;
104
105 Pom pom = buildPom( localRepo );
106 if ( pom != null )
107 {
108 if ( !dependencies.isEmpty() )
109 {
110 throw new BuildException( "You cannot specify both dependencies and a pom in the dependencies task" );
111 }
112
113 dependencies = pom.getDependencies();
114 }
115 else
116 {
117
118
119 pom = createDummyPom( localRepo );
120 }
121
122 if ( dependencies.isEmpty() )
123 {
124 log( "There were no dependencies specified", Project.MSG_WARN );
125 }
126
127 log( "Resolving dependencies...", Project.MSG_VERBOSE );
128
129 ArtifactResolutionResult result;
130 Set artifacts;
131
132 List remoteArtifactRepositories = createRemoteArtifactRepositories( pom.getRepositories() );
133
134 try
135 {
136 artifacts = MavenMetadataSource.createArtifacts( artifactFactory, dependencies, null, null, null );
137
138 Artifact pomArtifact = artifactFactory.createBuildArtifact( pom.getGroupId(), pom.getArtifactId(), pom
139 .getVersion(), pom.getPackaging() );
140
141 List listeners = Collections.singletonList( new AntResolutionListener( getProject(), verbose ) );
142
143 Map managedDependencies = pom.getMavenProject().getManagedVersionMap();
144
145 ArtifactFilter filter = null;
146 if ( useScope != null )
147 {
148 filter = new ScopeArtifactFilter( useScope );
149 }
150 if ( scopes != null )
151 {
152 filter = new SpecificScopesArtifactFilter( scopes );
153 }
154 if ( type != null )
155 {
156 ArtifactFilter typeArtifactFilter = new TypesArtifactFilter( type );
157 if ( filter != null )
158 {
159 AndArtifactFilter andFilter = new AndArtifactFilter();
160 andFilter.add( filter );
161 andFilter.add( typeArtifactFilter );
162 filter = andFilter;
163 }
164 else
165 {
166 filter = typeArtifactFilter;
167 }
168 }
169
170 result = resolver.resolveTransitively( artifacts, pomArtifact, managedDependencies, localRepo,
171 remoteArtifactRepositories, metadataSource, filter, listeners );
172 }
173 catch ( ArtifactResolutionException e )
174 {
175 throw new BuildException( "Unable to resolve artifact: " + e.getMessage(), e );
176 }
177 catch ( ArtifactNotFoundException e )
178 {
179 throw new BuildException( "Dependency not found: " + e.getMessage(), e );
180 }
181 catch ( InvalidDependencyVersionException e )
182 {
183 throw new BuildException( e.getMessage(), e );
184 }
185
186 FileList fileList = new FileList();
187 fileList.setDir( getLocalRepository().getPath() );
188
189 FileSet fileSet = new FileSet();
190 fileSet.setProject( getProject() );
191 fileSet.setDir( fileList.getDir( getProject() ) );
192
193 FileSet sourcesFileSet = new FileSet();
194 sourcesFileSet.setDir( getLocalRepository().getPath() );
195
196 FileSet javadocsFileSet = new FileSet();
197 javadocsFileSet.setDir( getLocalRepository().getPath() );
198
199 Set versions = new HashSet();
200
201 for ( Iterator i = result.getArtifacts().iterator(); i.hasNext(); )
202 {
203 Artifact artifact = (Artifact) i.next();
204
205 addArtifactToResult( localRepo, artifact, fileSet, fileList );
206
207 versions.add( artifact.getVersion() );
208
209 if ( sourcesFilesetId != null )
210 {
211 resolveSource( artifactFactory, resolver, remoteArtifactRepositories, localRepo,
212 artifact, "sources", sourcesFileSet );
213 }
214
215 if ( javadocFilesetId != null )
216 {
217 resolveSource( artifactFactory, resolver, remoteArtifactRepositories, localRepo,
218 artifact, "javadoc", javadocsFileSet );
219 }
220 }
221
222 if ( pathId != null )
223 {
224 Path path = new Path( getProject() );
225 if ( versions.size() > 0 )
226 {
227 path.addFilelist( fileList );
228 }
229 getProject().addReference( pathId, path );
230 }
231
232 defineFilesetReference( filesetId, fileSet );
233
234 defineFilesetReference( sourcesFilesetId, sourcesFileSet );
235
236 defineFilesetReference( javadocFilesetId, javadocsFileSet );
237
238 if ( versionsId != null )
239 {
240 String versionsValue = StringUtils.join( versions.iterator(), File.pathSeparator );
241 getProject().setNewProperty( versionsId, versionsValue );
242 }
243 }
244
245 private void defineFilesetReference( String id, FileSet fileSet )
246 {
247 if ( id != null )
248 {
249 if ( !fileSet.hasPatterns() )
250 {
251 fileSet.createExclude().setName( "**/**" );
252 }
253 getProject().addReference( id, fileSet );
254 }
255 }
256
257 private void addArtifactToResult( ArtifactRepository localRepo, Artifact artifact, FileSet toFileSet )
258 {
259 addArtifactToResult( localRepo, artifact, toFileSet, null );
260 }
261
262 private void addArtifactToResult( ArtifactRepository localRepo, Artifact artifact, FileSet toFileSet,
263 FileList toFileList )
264 {
265 String filename = localRepo.pathOf( artifact );
266
267 toFileSet.createInclude().setName( filename );
268
269 if ( toFileList != null )
270 {
271 FileList.FileName file = new FileList.FileName();
272 file.setName( filename );
273
274 toFileList.addConfiguredFile( file );
275 }
276
277 getProject().setProperty( artifact.getDependencyConflictId(), artifact.getFile().getAbsolutePath() );
278
279 if ( isAddArtifactFileSetRefs() )
280 {
281 FileSet artifactFileSet = new FileSet();
282 artifactFileSet.setFile( artifact.getFile() );
283 getProject().addReference( artifact.getDependencyConflictId(), artifactFileSet );
284 }
285 }
286
287 private void resolveSource( ArtifactFactory artifactFactory, ArtifactResolver resolver,
288 List remoteArtifactRepositories, ArtifactRepository localRepo,
289 Artifact artifact, String classifier, FileSet sourcesFileSet )
290 {
291 Artifact sourceArtifact =
292 artifactFactory.createArtifactWithClassifier( artifact.getGroupId(), artifact.getArtifactId(),
293 artifact.getVersion(), "java-source", classifier );
294 try
295 {
296 resolver.resolve( sourceArtifact, remoteArtifactRepositories, localRepo );
297
298 addArtifactToResult( localRepo, sourceArtifact, sourcesFileSet );
299 }
300 catch ( ArtifactResolutionException e )
301 {
302 throw new BuildException( "Unable to resolve artifact: " + e.getMessage(), e );
303 }
304 catch ( ArtifactNotFoundException e )
305 {
306
307 }
308 }
309
310 public List getDependencies()
311 {
312 return dependencies;
313 }
314
315 public void addDependency( Dependency dependency )
316 {
317 dependencies.add( dependency );
318 }
319
320 public String getPathId()
321 {
322 return pathId;
323 }
324
325 public void setPathId( String pathId )
326 {
327 this.pathId = pathId;
328 }
329
330 public String getFilesetId()
331 {
332 return filesetId;
333 }
334
335 public void setSourcesFilesetId( String filesetId )
336 {
337 this.sourcesFilesetId = filesetId;
338 }
339
340 public String getSourcesFilesetId()
341 {
342 return sourcesFilesetId;
343 }
344
345 public void setJavadocFilesetId( String filesetId )
346 {
347 this.javadocFilesetId = filesetId;
348 }
349
350 public String getJavadocFilesetId()
351 {
352 return javadocFilesetId;
353 }
354
355 public void setFilesetId( String filesetId )
356 {
357 this.filesetId = filesetId;
358 }
359
360 public String getVersionsId()
361 {
362 return versionsId;
363 }
364
365 public void setVersionsId( String versionsId )
366 {
367 this.versionsId = versionsId;
368 }
369
370 public void setVerbose( boolean verbose )
371 {
372 this.verbose = verbose;
373 }
374
375 public void setUseScope( String useScope )
376 {
377 this.useScope = useScope;
378 }
379
380 public void setType( String type )
381 {
382 this.type = type;
383 }
384
385 public String getScopes()
386 {
387 return scopes;
388 }
389
390 public void setScopes( String scopes )
391 {
392 this.scopes = scopes;
393 }
394
395 private void showVersion()
396 {
397 InputStream resourceAsStream;
398 try
399 {
400 Properties properties = new Properties();
401 resourceAsStream = DependenciesTask.class.getClassLoader().getResourceAsStream(
402 "META-INF/maven/org.apache.maven/maven-ant-tasks/pom.properties" );
403 if ( resourceAsStream != null )
404 {
405 properties.load( resourceAsStream );
406 }
407
408 String version = properties.getProperty( "version", "unknown" );
409 String builtOn = properties.getProperty( "builtOn" );
410 if ( builtOn != null )
411 {
412 log( "Maven Ant Tasks version: " + version + " built on " + builtOn, Project.MSG_VERBOSE );
413 }
414 else
415 {
416 log( "Maven Ant Tasks version: " + version, Project.MSG_VERBOSE );
417 }
418 }
419 catch ( IOException e )
420 {
421 log( "Unable to determine version from Maven Ant Tasks JAR file: " + e.getMessage(), Project.MSG_WARN );
422 }
423 }
424
425 public boolean isAddArtifactFileSetRefs()
426 {
427 return addArtifactFileSetRefs;
428 }
429
430 public void setAddArtifactFileSetRefs( boolean addArtifactFileSetRefs )
431 {
432 this.addArtifactFileSetRefs = addArtifactFileSetRefs;
433 }
434 }