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