View Javadoc
1   package org.apache.maven.plugin.surefire;
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.ArrayList;
23  import java.util.Collections;
24  import java.util.List;
25  import java.util.Map;
26  import org.apache.maven.artifact.Artifact;
27  import org.apache.maven.artifact.factory.ArtifactFactory;
28  import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
29  import org.apache.maven.artifact.repository.ArtifactRepository;
30  import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
31  import org.apache.maven.artifact.resolver.ArtifactResolutionException;
32  import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
33  import org.apache.maven.artifact.resolver.ArtifactResolver;
34  import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
35  import org.apache.maven.artifact.resolver.filter.ExcludesArtifactFilter;
36  import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
37  import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
38  import org.apache.maven.artifact.versioning.OverConstrainedVersionException;
39  import org.apache.maven.artifact.versioning.VersionRange;
40  import org.apache.maven.surefire.booter.Classpath;
41  import org.apache.maven.plugin.surefire.log.api.ConsoleLogger;
42  
43  import javax.annotation.Nonnull;
44  import javax.annotation.Nullable;
45  
46  /**
47   * Does dependency resolution and artifact handling for the surefire plugin.
48   *
49   * @author Stephen Connolly
50   * @author Kristian Rosenvold
51   */
52  public class SurefireDependencyResolver
53  {
54  
55      private final ArtifactResolver artifactResolver;
56  
57      private final ArtifactFactory artifactFactory;
58  
59      private final ConsoleLogger log;
60  
61      private final ArtifactRepository localRepository;
62  
63      private final List<ArtifactRepository> remoteRepositories;
64  
65      private final ArtifactMetadataSource artifactMetadataSource;
66  
67      private final String pluginName;
68  
69      protected SurefireDependencyResolver( ArtifactResolver artifactResolver, ArtifactFactory artifactFactory,
70                                            ConsoleLogger log,
71                                            ArtifactRepository localRepository,
72                                            List<ArtifactRepository> remoteRepositories,
73                                            ArtifactMetadataSource artifactMetadataSource, String pluginName )
74      {
75          this.artifactResolver = artifactResolver;
76          this.artifactFactory = artifactFactory;
77          this.log = log;
78          this.localRepository = localRepository;
79          this.remoteRepositories = remoteRepositories;
80          this.artifactMetadataSource = artifactMetadataSource;
81          this.pluginName = pluginName;
82      }
83  
84  
85      public boolean isWithinVersionSpec( @Nullable Artifact artifact, @Nonnull String versionSpec )
86      {
87          if ( artifact == null )
88          {
89              return false;
90          }
91          try
92          {
93              VersionRange range = VersionRange.createFromVersionSpec( versionSpec );
94              try
95              {
96                  return range.containsVersion( artifact.getSelectedVersion() );
97              }
98              catch ( NullPointerException e )
99              {
100                 return range.containsVersion( new DefaultArtifactVersion( artifact.getBaseVersion() ) );
101             }
102         }
103         catch ( InvalidVersionSpecificationException e )
104         {
105             throw new RuntimeException( "Bug in plugin. Please report with stacktrace" );
106         }
107         catch ( OverConstrainedVersionException e )
108         {
109             throw new RuntimeException( "Bug in plugin. Please report with stacktrace" );
110         }
111     }
112 
113 
114     public ArtifactResolutionResult resolveArtifact( @Nullable Artifact filteredArtifact, Artifact providerArtifact )
115         throws ArtifactResolutionException, ArtifactNotFoundException
116     {
117         ArtifactFilter filter = null;
118         if ( filteredArtifact != null )
119         {
120             filter = new ExcludesArtifactFilter(
121                 Collections.singletonList( filteredArtifact.getGroupId() + ":" + filteredArtifact.getArtifactId() ) );
122         }
123 
124         Artifact originatingArtifact = artifactFactory.createBuildArtifact( "dummy", "dummy", "1.0", "jar" );
125 
126         return artifactResolver.resolveTransitively( Collections.singleton( providerArtifact ), originatingArtifact,
127                                                      localRepository, remoteRepositories, artifactMetadataSource,
128                                                      filter );
129     }
130 
131     public Classpath getProviderClasspath( String provider, String version, Artifact filteredArtifact )
132         throws ArtifactNotFoundException, ArtifactResolutionException
133     {
134         Classpath classPath = ClasspathCache.getCachedClassPath( provider );
135         if ( classPath == null )
136         {
137             Artifact providerArtifact = artifactFactory.createDependencyArtifact( "org.apache.maven.surefire", provider,
138                                                                                   VersionRange.createFromVersion(
139                                                                                       version ), "jar", null,
140                                                                                   Artifact.SCOPE_TEST );
141             ArtifactResolutionResult result = resolveArtifact( filteredArtifact, providerArtifact );
142             List<String> files = new ArrayList<String>();
143 
144             for ( Object o : result.getArtifacts() )
145             {
146                 Artifact artifact = (Artifact) o;
147 
148                 log.debug(
149                     "Adding to " + pluginName + " test classpath: " + artifact.getFile().getAbsolutePath() + " Scope: "
150                         + artifact.getScope() );
151 
152                 files.add( artifact.getFile().getAbsolutePath() );
153             }
154             classPath = new Classpath( files );
155             ClasspathCache.setCachedClasspath( provider, classPath );
156         }
157         return classPath;
158     }
159 
160     public Classpath addProviderToClasspath( Map<String, Artifact> pluginArtifactMap, Artifact surefireArtifact )
161         throws ArtifactResolutionException, ArtifactNotFoundException
162     {
163         List<String> files = new ArrayList<String>();
164         if ( surefireArtifact != null )
165         {
166             final ArtifactResolutionResult artifactResolutionResult = resolveArtifact( null, surefireArtifact );
167             for ( Artifact artifact : pluginArtifactMap.values() )
168             {
169                 if ( !artifactResolutionResult.getArtifacts().contains( artifact ) )
170                 {
171                     files.add( artifact.getFile().getAbsolutePath() );
172                 }
173             }
174         }
175         else
176         {
177             // Bit of a brute force strategy if not found. Should probably be improved
178             for ( Artifact artifact : pluginArtifactMap.values() )
179             {
180                 files.add( artifact.getFile().getPath() );
181             }
182         }
183         return new Classpath( files );
184     }
185 
186 }