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