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.ArtifactUtils;
24 import org.apache.maven.artifact.factory.ArtifactFactory;
25 import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
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.versioning.InvalidVersionSpecificationException;
31 import org.apache.maven.artifact.versioning.VersionRange;
32 import org.apache.maven.project.artifact.MavenMetadataSource;
33 import org.apache.tools.ant.BuildException;
34 import org.apache.tools.ant.Project;
35 import org.codehaus.plexus.PlexusContainerException;
36
37 import java.util.Collections;
38 import java.util.Iterator;
39 import java.util.List;
40
41
42
43
44
45
46
47 public class InstallWagonProviderTask
48 extends AbstractArtifactWithRepositoryTask
49 {
50 private String groupId = "org.apache.maven.wagon";
51
52 private String artifactId;
53
54 private String version;
55
56 public String getGroupId()
57 {
58 return groupId;
59 }
60
61 public void setGroupId( String groupId )
62 {
63 this.groupId = groupId;
64 }
65
66 public String getArtifactId()
67 {
68 return artifactId;
69 }
70
71 public void setArtifactId( String artifactId )
72 {
73 this.artifactId = artifactId;
74 }
75
76 public String getVersion()
77 {
78 return version;
79 }
80
81 public void setVersion( String version )
82 {
83 this.version = version;
84 }
85
86 public void doExecute()
87 {
88 VersionRange versionRange;
89 try
90 {
91 versionRange = VersionRange.createFromVersionSpec( version );
92 }
93 catch ( InvalidVersionSpecificationException e )
94 {
95 throw new BuildException( "Unable to get extension '"
96 + ArtifactUtils.versionlessKey( groupId, artifactId ) + "' because version '"
97 + version + " is invalid: " + e.getMessage(), e );
98 }
99
100 ArtifactFactory factory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
101 Artifact providerArtifact = factory.createExtensionArtifact( groupId, artifactId, versionRange );
102
103 log( "Installing provider: " + providerArtifact );
104
105 ArtifactResolutionResult result;
106 try
107 {
108 MavenMetadataSource metadataSource = (MavenMetadataSource) lookup( ArtifactMetadataSource.ROLE );
109 ArtifactResolver resolver = (ArtifactResolver) lookup( ArtifactResolver.ROLE );
110 List remoteRepositories = createRemoteArtifactRepositories();
111
112 result = resolver.resolveTransitively( Collections.singleton( providerArtifact ),
113 createDummyArtifact(), createLocalArtifactRepository(),
114 remoteRepositories, metadataSource, null );
115 }
116 catch ( ArtifactResolutionException e )
117 {
118 throw new BuildException( "Error downloading wagon provider from the remote repository: " + e.getMessage(),
119 e );
120 }
121 catch ( ArtifactNotFoundException e )
122 {
123 throw new BuildException( "Unable to locate wagon provider in remote repository: " + e.getMessage(), e );
124 }
125
126 try
127 {
128 for ( Iterator i = result.getArtifacts().iterator(); i.hasNext(); )
129 {
130 Artifact a = (Artifact) i.next();
131
132 getContainer().addJarResource( a.getFile() );
133 }
134 }
135 catch ( PlexusContainerException e )
136 {
137 throw new BuildException( "Unable to locate wagon provider in remote repository", e );
138 }
139
140 log( "Protocols now supported: " + getSupportedProtocolsAsString(), Project.MSG_VERBOSE );
141 }
142
143 }