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