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