1
package org.apache.maven.plugin.surefire;
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
12
* http://www.apache.org/licenses/LICENSE-2.0
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
22
import java.util.ArrayList;
23
import java.util.Collections;
24
import java.util.Iterator;
25
import java.util.List;
27
import org.apache.maven.artifact.Artifact;
28
import org.apache.maven.artifact.factory.ArtifactFactory;
29
import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
30
import org.apache.maven.artifact.repository.ArtifactRepository;
31
import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
32
import org.apache.maven.artifact.resolver.ArtifactResolutionException;
33
import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
34
import org.apache.maven.artifact.resolver.ArtifactResolver;
35
import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
36
import org.apache.maven.artifact.resolver.filter.ExcludesArtifactFilter;
37
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
38
import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
39
import org.apache.maven.artifact.versioning.OverConstrainedVersionException;
40
import org.apache.maven.artifact.versioning.VersionRange;
41
import org.apache.maven.plugin.logging.Log;
42
import org.apache.maven.surefire.booter.Classpath;
45
* Does dependency resolution and artifact handling for the surefire plugin.
47
* @author Stephen Connolly
48
* @author Kristian Rosenvold
50
public class SurefireDependencyResolver
53
private final ArtifactResolver artifactResolver;
55
private final ArtifactFactory artifactFactory;
57
private final org.apache.maven.plugin.logging.Log log;
59
private final ArtifactRepository localRepository;
61
private final List remoteRepositories;
63
private final ArtifactMetadataSource artifactMetadataSource;
65
private final String pluginName;
67
protected SurefireDependencyResolver( ArtifactResolver artifactResolver, ArtifactFactory artifactFactory, Log log,
68
ArtifactRepository localRepository, List remoteRepositories,
69
ArtifactMetadataSource artifactMetadataSource, String pluginName )
71
this.artifactResolver = artifactResolver;
72
this.artifactFactory = artifactFactory;
74
this.localRepository = localRepository;
75
this.remoteRepositories = remoteRepositories;
76
this.artifactMetadataSource = artifactMetadataSource;
77
this.pluginName = pluginName;
81
public boolean isWithinVersionSpec( Artifact artifact, String versionSpec )
83
if ( artifact == null )
89
VersionRange range = VersionRange.createFromVersionSpec( versionSpec );
92
return range.containsVersion( artifact.getSelectedVersion() );
94
catch ( NullPointerException e )
96
return range.containsVersion( new DefaultArtifactVersion( artifact.getBaseVersion() ) );
99
catch ( InvalidVersionSpecificationException e )
101
throw new RuntimeException( "Bug in plugin. Please report with stacktrace" );
103
catch ( OverConstrainedVersionException e )
105
throw new RuntimeException( "Bug in plugin. Please report with stacktrace" );
110
public ArtifactResolutionResult resolveArtifact( Artifact filteredArtifact, Artifact providerArtifact )
111
throws ArtifactResolutionException, ArtifactNotFoundException
113
ArtifactFilter filter = null;
114
if ( filteredArtifact != null )
116
filter = new ExcludesArtifactFilter(
117
Collections.singletonList( filteredArtifact.getGroupId() + ":" + filteredArtifact.getArtifactId() ) );
120
Artifact originatingArtifact = artifactFactory.createBuildArtifact( "dummy", "dummy", "1.0", "jar" );
122
return artifactResolver.resolveTransitively( Collections.singleton( providerArtifact ), originatingArtifact,
123
localRepository, remoteRepositories, artifactMetadataSource,
127
public Classpath getProviderClasspath( String provider, String version, Artifact filteredArtifact )
128
throws ArtifactNotFoundException, ArtifactResolutionException
130
Artifact providerArtifact = artifactFactory.createDependencyArtifact( "org.apache.maven.surefire", provider,
131
VersionRange.createFromVersion( version ),
132
"jar", null, Artifact.SCOPE_TEST );
133
ArtifactResolutionResult result = resolveArtifact( filteredArtifact, providerArtifact );
134
List files = new ArrayList();
136
for ( Iterator i = result.getArtifacts().iterator(); i.hasNext(); )
138
Artifact artifact = (Artifact) i.next();
141
"Adding to " + pluginName + " test classpath: " + artifact.getFile().getAbsolutePath() + " Scope: "
142
+ artifact.getScope() );
144
files.add( artifact.getFile().getAbsolutePath() );
146
return new Classpath( files );
149
public Classpath addProviderToClasspath( Map pluginArtifactMap, Artifact surefireArtifact )
150
throws ArtifactResolutionException, ArtifactNotFoundException
152
List files = new ArrayList();
153
if ( surefireArtifact != null )
155
final ArtifactResolutionResult artifactResolutionResult = resolveArtifact( null, surefireArtifact );
156
for ( Iterator iterator = pluginArtifactMap.values().iterator(); iterator.hasNext(); )
158
Artifact artifact = (Artifact) iterator.next();
159
if ( !artifactResolutionResult.getArtifacts().contains( artifact ) )
161
files.add( artifact.getFile().getAbsolutePath() );
167
// Bit of a brute force strategy if not found. Should probably be improved
168
for ( Iterator iterator = pluginArtifactMap.values().iterator(); iterator.hasNext(); )
170
Artifact artifact = (Artifact) iterator.next();
171
files.add( artifact.getFile().getPath() );
174
return new Classpath( files );