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