1 | |
package org.apache.maven.archetype.downloader; |
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.repository.ArtifactRepository; |
25 | |
import org.apache.maven.artifact.resolver.ArtifactNotFoundException; |
26 | |
import org.apache.maven.artifact.resolver.ArtifactResolutionException; |
27 | |
import org.apache.maven.artifact.resolver.ArtifactResolver; |
28 | |
import org.codehaus.plexus.component.annotations.Component; |
29 | |
import org.codehaus.plexus.component.annotations.Requirement; |
30 | |
|
31 | |
import java.io.File; |
32 | |
import java.util.ArrayList; |
33 | |
import java.util.List; |
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
@Component( role = Downloader.class ) |
39 | 112 | public class DefaultDownloader |
40 | |
implements Downloader |
41 | |
{ |
42 | |
@Requirement |
43 | |
private ArtifactResolver artifactResolver; |
44 | |
|
45 | |
@Requirement |
46 | |
private ArtifactFactory artifactFactory; |
47 | |
|
48 | |
public File download( String groupId, String artifactId, String version, ArtifactRepository archetypeRepository, |
49 | |
ArtifactRepository localRepository, List<ArtifactRepository> remoteRepositories ) |
50 | |
throws DownloadException, DownloadNotFoundException |
51 | |
{ |
52 | 30 | Artifact artifact = |
53 | |
artifactFactory.createArtifact( groupId, artifactId, version, Artifact.SCOPE_RUNTIME, "jar" ); |
54 | 30 | Artifact artifactPom = |
55 | |
artifactFactory.createArtifact( groupId, artifactId, version, Artifact.SCOPE_RUNTIME, "pom" ); |
56 | |
|
57 | 30 | List<ArtifactRepository> repositories = new ArrayList<ArtifactRepository>( remoteRepositories ); |
58 | 30 | if ( repositories.isEmpty() && archetypeRepository != null ) |
59 | |
{ |
60 | 0 | repositories.add( archetypeRepository ); |
61 | |
} |
62 | 30 | else if ( repositories.isEmpty() && localRepository != null ) |
63 | |
{ |
64 | 0 | repositories.add( localRepository ); |
65 | |
} |
66 | |
|
67 | 30 | ArtifactRepository localRepo = localRepository; |
68 | |
try |
69 | |
{ |
70 | 30 | artifactResolver.resolve( artifact, repositories, localRepo ); |
71 | |
} |
72 | 0 | catch ( ArtifactResolutionException e ) |
73 | |
{ |
74 | 0 | throw new DownloadException( "Error downloading " + artifact.getId() + ".", e ); |
75 | |
} |
76 | 0 | catch ( ArtifactNotFoundException e ) |
77 | |
{ |
78 | 0 | throw new DownloadNotFoundException( "Requested " + artifact.getId() + " download does not exist.", e ); |
79 | 30 | } |
80 | |
try |
81 | |
{ |
82 | 30 | artifactResolver.resolve( artifactPom, repositories, localRepo ); |
83 | |
} |
84 | 0 | catch ( ArtifactResolutionException e ) |
85 | |
{ |
86 | 0 | throw new DownloadException( "Error downloading POM for " + artifact.getId() + ".", e ); |
87 | |
} |
88 | 0 | catch ( ArtifactNotFoundException e ) |
89 | |
{ |
90 | 0 | throw new DownloadNotFoundException( "Requested " + artifact.getId() + " download's POM does not exist.", e ); |
91 | 30 | } |
92 | |
|
93 | 30 | return artifact.getFile(); |
94 | |
} |
95 | |
|
96 | |
public File downloadOld( String groupId, String artifactId, String version, ArtifactRepository archetypeRepository, |
97 | |
ArtifactRepository localRepository, List<ArtifactRepository> remoteRepositories ) |
98 | |
throws DownloadException, DownloadNotFoundException |
99 | |
{ |
100 | 0 | Artifact artifact = |
101 | |
artifactFactory.createArtifact( groupId, artifactId, version, Artifact.SCOPE_RUNTIME, "jar" ); |
102 | |
try |
103 | |
{ |
104 | 0 | artifactResolver.resolve( artifact, remoteRepositories, localRepository ); |
105 | |
} |
106 | 0 | catch ( ArtifactResolutionException e ) |
107 | |
{ |
108 | 0 | throw new DownloadException( "Error downloading " + artifact.getId() + ".", e ); |
109 | |
} |
110 | 0 | catch ( ArtifactNotFoundException e ) |
111 | |
{ |
112 | 0 | throw new DownloadNotFoundException( "Requested " + artifact.getId() + " download does not exist.", e ); |
113 | 0 | } |
114 | |
|
115 | 0 | return artifact.getFile(); |
116 | |
} |
117 | |
} |