View Javadoc
1   package org.apache.maven.plugin.dependency.utils.resolvers;
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.util.HashSet;
23  import java.util.List;
24  import java.util.Set;
25  
26  import org.apache.maven.artifact.Artifact;
27  import org.apache.maven.artifact.repository.ArtifactRepository;
28  import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
29  import org.apache.maven.artifact.resolver.ArtifactResolutionException;
30  import org.apache.maven.artifact.resolver.ArtifactResolver;
31  import org.apache.maven.plugin.MojoExecutionException;
32  import org.apache.maven.plugin.logging.Log;
33  
34  /**
35   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
36   * @version $Id: DefaultArtifactsResolver.html 937155 2015-01-21 21:53:50Z khmarbaise $
37   */
38  public class DefaultArtifactsResolver
39      implements ArtifactsResolver
40  {
41      ArtifactResolver resolver;
42  
43      ArtifactRepository local;
44  
45      List<ArtifactRepository> remoteRepositories;
46  
47      boolean stopOnFailure;
48  
49      public DefaultArtifactsResolver( ArtifactResolver theResolver, ArtifactRepository theLocal,
50                                      List<ArtifactRepository> theRemoteRepositories, boolean theStopOnFailure )
51      {
52          this.resolver = theResolver;
53          this.local = theLocal;
54          this.remoteRepositories = theRemoteRepositories;
55          this.stopOnFailure = theStopOnFailure;
56      }
57  
58      /*
59       * (non-Javadoc)
60       * 
61       * @see org.apache.mojo.dependency.utils.resolvers.ArtifactsResolver#resolve(java.util.Set,
62       *      org.apache.maven.plugin.logging.Log)
63       */
64      public Set<Artifact> resolve( Set<Artifact> artifacts, Log log )
65          throws MojoExecutionException
66      {
67  
68          Set<Artifact> resolvedArtifacts = new HashSet<Artifact>();
69          for ( Artifact artifact : artifacts )
70          {
71              try
72              {
73                  resolver.resolve( artifact, remoteRepositories, local );
74                  resolvedArtifacts.add( artifact );
75              }
76              catch ( ArtifactResolutionException ex )
77              {
78                  // an error occurred during resolution, log it an continue
79                  log.debug( "error resolving: " + artifact.getId() );
80                  log.debug( ex );
81                  if ( stopOnFailure )
82                  {
83                      throw new MojoExecutionException( "error resolving: " + artifact.getId(), ex );
84                  }
85              }
86              catch ( ArtifactNotFoundException ex )
87              {
88                  // not found, log it and continue
89                  log.debug( "not found in any repository: " + artifact.getId() );
90                  if ( stopOnFailure )
91                  {
92                      throw new MojoExecutionException( "not found in any repository: " + artifact.getId(), ex );
93                  }
94              }
95          }
96          return resolvedArtifacts;
97      }
98  
99  }