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