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