View Javadoc
1   package org.apache.maven.archetype.downloader;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * @author Jason van Zyl
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          // still required???
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 }