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.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   * @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      @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          // still required???
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 }