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