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.transfer.artifact.DefaultArtifactCoordinate;
30 import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResolver;
31 import org.apache.maven.shared.transfer.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 @Override
46 public File download( String groupId, String artifactId, String version, ArtifactRepository archetypeRepository,
47 ArtifactRepository localRepository, List<ArtifactRepository> remoteRepositories,
48 ProjectBuildingRequest buildingRequest )
49 throws DownloadException, DownloadNotFoundException
50 {
51 DefaultArtifactCoordinate jarCoordinate = new DefaultArtifactCoordinate();
52 jarCoordinate.setGroupId( groupId );
53 jarCoordinate.setArtifactId( artifactId );
54 jarCoordinate.setVersion( version );
55
56 DefaultArtifactCoordinate pomCoordinate = new DefaultArtifactCoordinate();
57 pomCoordinate.setGroupId( groupId );
58 pomCoordinate.setArtifactId( artifactId );
59 pomCoordinate.setVersion( version );
60 pomCoordinate.setExtension( "pom" );
61
62 List<ArtifactRepository> repositories = new ArrayList<>( remoteRepositories );
63 if ( repositories.isEmpty() && archetypeRepository != null )
64 {
65 repositories.add( archetypeRepository );
66 }
67 else if ( repositories.isEmpty() && localRepository != null )
68 {
69 repositories.add( localRepository );
70 }
71
72 ArtifactRepository localRepo = localRepository;
73
74 buildingRequest.setLocalRepository( localRepo );
75 buildingRequest.setRemoteRepositories( repositories );
76
77 Artifact artifact;
78 try
79 {
80 artifact = artifactResolver.resolveArtifact( buildingRequest, jarCoordinate ).getArtifact();
81 }
82 catch ( ArtifactResolverException e )
83 {
84 throw new DownloadException( "Error downloading " + jarCoordinate + ".", e );
85 }
86
87
88 try
89 {
90 artifactResolver.resolveArtifact( buildingRequest, pomCoordinate );
91 }
92 catch ( ArtifactResolverException e )
93 {
94 throw new DownloadException( "Error downloading POM for " + artifact.getId() + ".", e );
95 }
96
97 return artifact.getFile();
98 }
99
100 @Override
101 public File downloadOld( String groupId, String artifactId, String version, ArtifactRepository archetypeRepository,
102 ArtifactRepository localRepository, List<ArtifactRepository> remoteRepositories,
103 ProjectBuildingRequest buildingRequest )
104 throws DownloadException, DownloadNotFoundException
105 {
106 DefaultArtifactCoordinate jarCoordinate = new DefaultArtifactCoordinate();
107 jarCoordinate.setGroupId( groupId );
108 jarCoordinate.setArtifactId( artifactId );
109 jarCoordinate.setVersion( version );
110
111 try
112 {
113 return artifactResolver.resolveArtifact( buildingRequest, jarCoordinate ).getArtifact().getFile();
114 }
115 catch ( ArtifactResolverException e )
116 {
117 throw new DownloadException( "Error downloading " + jarCoordinate + ".", e );
118 }
119 }
120 }