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 org.apache.maven.artifact.Artifact;
23  import org.apache.maven.artifact.factory.ArtifactFactory;
24  import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
25  import org.apache.maven.artifact.repository.ArtifactRepository;
26  import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
27  import org.apache.maven.artifact.resolver.ArtifactResolutionException;
28  import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
29  import org.apache.maven.artifact.resolver.ArtifactResolver;
30  import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
31  import org.apache.maven.artifact.resolver.filter.ExcludesArtifactFilter;
32  import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
33  import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
34  import org.apache.maven.artifact.versioning.OverConstrainedVersionException;
35  import org.apache.maven.artifact.versioning.VersionRange;
36  import org.apache.maven.plugin.logging.Log;
37  import org.apache.maven.surefire.booter.Classpath;
38  
39  import java.util.ArrayList;
40  import java.util.Collections;
41  import java.util.Iterator;
42  import java.util.LinkedHashSet;
43  import java.util.List;
44  import java.util.Map;
45  import java.util.Set;
46  
47  /**
48   * Does dependency resolution and artifact handling for the surefire plugin.
49   *
50   * @author Stephen Connolly
51   * @author Kristian Rosenvold
52   */
53  public class SurefireDependencyResolver
54  {
55  
56      private final ArtifactResolver artifactResolver;
57  
58      private final ArtifactFactory artifactFactory;
59  
60      private final org.apache.maven.plugin.logging.Log log;
61  
62      private final ArtifactRepository localRepository;
63  
64      private final List remoteRepositories;
65  
66      private final ArtifactMetadataSource artifactMetadataSource;
67  
68      private final String pluginName;
69  
70      protected SurefireDependencyResolver( ArtifactResolver artifactResolver, ArtifactFactory artifactFactory, Log log,
71                                            ArtifactRepository localRepository, List 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( Artifact artifact, 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      * Return a new set containing only the artifacts accepted by the given filter.
114      *
115      * @param artifacts The unfiltered artifacts
116      * @param filter    The filter to apply
117      * @return The filtered result
118      * @noinspection UnusedDeclaration
119      */
120     public Set filterArtifacts( Set artifacts, ArtifactFilter filter )
121     {
122         Set filteredArtifacts = new LinkedHashSet();
123 
124         for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
125         {
126             Artifact artifact = (Artifact) iter.next();
127             if ( !filter.include( artifact ) )
128             {
129                 filteredArtifacts.add( artifact );
130             }
131         }
132 
133         return filteredArtifacts;
134     }
135 
136 
137     public ArtifactResolutionResult resolveArtifact( Artifact filteredArtifact, Artifact providerArtifact )
138         throws ArtifactResolutionException, ArtifactNotFoundException
139     {
140         ArtifactFilter filter = null;
141         if ( filteredArtifact != null )
142         {
143             filter = new ExcludesArtifactFilter(
144                 Collections.singletonList( filteredArtifact.getGroupId() + ":" + filteredArtifact.getArtifactId() ) );
145         }
146 
147         Artifact originatingArtifact = artifactFactory.createBuildArtifact( "dummy", "dummy", "1.0", "jar" );
148         
149         return artifactResolver.resolveTransitively( Collections.singleton( providerArtifact ), originatingArtifact,
150                                                      localRepository, remoteRepositories, artifactMetadataSource,
151                                                      filter );
152     }
153 
154     public Classpath getProviderClasspath( String provider, String version, Artifact filteredArtifact )
155         throws ArtifactNotFoundException, ArtifactResolutionException
156     {
157         Artifact providerArtifact = artifactFactory.createDependencyArtifact( "org.apache.maven.surefire", provider,
158                                                                               VersionRange.createFromVersion( version ),
159                                                                               "jar", null, Artifact.SCOPE_TEST );
160         ArtifactResolutionResult result = resolveArtifact( filteredArtifact, providerArtifact );
161         List files = new ArrayList();
162 
163         for ( Iterator i = result.getArtifacts().iterator(); i.hasNext(); )
164         {
165             Artifact artifact = (Artifact) i.next();
166 
167             log.debug(
168                 "Adding to " + pluginName + " test classpath: " + artifact.getFile().getAbsolutePath() + " Scope: "
169                     + artifact.getScope() );
170 
171             files.add( artifact.getFile().getAbsolutePath() );
172         }
173         return new Classpath( files );
174     }
175 
176     public Classpath addProviderToClasspath( Map pluginArtifactMap, Artifact surefireArtifact )
177         throws ArtifactResolutionException, ArtifactNotFoundException
178     {
179         List files = new ArrayList();
180         if ( surefireArtifact != null )
181         {
182             final ArtifactResolutionResult artifactResolutionResult = resolveArtifact( null, surefireArtifact );
183             for ( Iterator iterator = pluginArtifactMap.values().iterator(); iterator.hasNext(); )
184             {
185                 Artifact artifact = (Artifact) iterator.next();
186                 if ( !artifactResolutionResult.getArtifacts().contains( artifact ) )
187                 {
188                     files.add( artifact.getFile().getAbsolutePath() );
189                 }
190             }
191         }
192         else
193         {
194             // Bit of a brute force strategy if not found. Should probably be improved
195             for ( Iterator iterator = pluginArtifactMap.values().iterator(); iterator.hasNext(); )
196             {
197                 Artifact artifact = (Artifact) iterator.next();
198                 files.add( artifact.getFile().getPath() );
199             }
200         }
201         return new Classpath( files );
202     }
203 
204     public Classpath getResolvedArtifactClasspath( Artifact surefireArtifact )
205         throws ArtifactNotFoundException, ArtifactResolutionException
206     {
207         ArtifactResolutionResult result = resolveArtifact( null, surefireArtifact );
208         List classpath = new ArrayList();
209 
210         for ( Iterator i = result.getArtifacts().iterator(); i.hasNext(); )
211         {
212             Artifact artifact = (Artifact) i.next();
213 
214             classpath.add( artifact.getFile().getAbsolutePath() );
215         }
216         return new Classpath( classpath);
217     }
218 
219 }