1 | |
package org.apache.maven.plugin.surefire; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
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 | |
|
48 | |
|
49 | |
|
50 | |
|
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 | 0 | { |
74 | 0 | this.artifactResolver = artifactResolver; |
75 | 0 | this.artifactFactory = artifactFactory; |
76 | 0 | this.log = log; |
77 | 0 | this.localRepository = localRepository; |
78 | 0 | this.remoteRepositories = remoteRepositories; |
79 | 0 | this.artifactMetadataSource = artifactMetadataSource; |
80 | 0 | this.pluginName = pluginName; |
81 | 0 | } |
82 | |
|
83 | |
|
84 | |
public boolean isWithinVersionSpec( @Nullable Artifact artifact, @Nonnull String versionSpec ) |
85 | |
{ |
86 | 0 | if ( artifact == null ) |
87 | |
{ |
88 | 0 | return false; |
89 | |
} |
90 | |
try |
91 | |
{ |
92 | 0 | VersionRange range = VersionRange.createFromVersionSpec( versionSpec ); |
93 | |
try |
94 | |
{ |
95 | 0 | return range.containsVersion( artifact.getSelectedVersion() ); |
96 | |
} |
97 | 0 | catch ( NullPointerException e ) |
98 | |
{ |
99 | 0 | return range.containsVersion( new DefaultArtifactVersion( artifact.getBaseVersion() ) ); |
100 | |
} |
101 | |
} |
102 | 0 | catch ( InvalidVersionSpecificationException e ) |
103 | |
{ |
104 | 0 | throw new RuntimeException( "Bug in plugin. Please report with stacktrace" ); |
105 | |
} |
106 | 0 | catch ( OverConstrainedVersionException e ) |
107 | |
{ |
108 | 0 | 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 | 0 | ArtifactFilter filter = null; |
117 | 0 | if ( filteredArtifact != null ) |
118 | |
{ |
119 | 0 | filter = new ExcludesArtifactFilter( |
120 | |
Collections.singletonList( filteredArtifact.getGroupId() + ":" + filteredArtifact.getArtifactId() ) ); |
121 | |
} |
122 | |
|
123 | 0 | Artifact originatingArtifact = artifactFactory.createBuildArtifact( "dummy", "dummy", "1.0", "jar" ); |
124 | |
|
125 | 0 | 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 | 0 | Classpath classPath = ClasspathCache.getCachedClassPath( provider ); |
134 | 0 | if ( classPath == null ) |
135 | |
{ |
136 | 0 | Artifact providerArtifact = artifactFactory.createDependencyArtifact( "org.apache.maven.surefire", provider, |
137 | |
VersionRange.createFromVersion( |
138 | |
version ), "jar", null, |
139 | |
Artifact.SCOPE_TEST ); |
140 | 0 | ArtifactResolutionResult result = resolveArtifact( filteredArtifact, providerArtifact ); |
141 | 0 | List<String> files = new ArrayList<String>(); |
142 | |
|
143 | 0 | for ( Object o : result.getArtifacts() ) |
144 | |
{ |
145 | 0 | Artifact artifact = (Artifact) o; |
146 | |
|
147 | 0 | log.debug( |
148 | |
"Adding to " + pluginName + " test classpath: " + artifact.getFile().getAbsolutePath() + " Scope: " |
149 | |
+ artifact.getScope() ); |
150 | |
|
151 | 0 | files.add( artifact.getFile().getAbsolutePath() ); |
152 | 0 | } |
153 | 0 | classPath = new Classpath( files ); |
154 | 0 | ClasspathCache.setCachedClasspath( provider, classPath ); |
155 | |
} |
156 | 0 | return classPath; |
157 | |
} |
158 | |
|
159 | |
public Classpath addProviderToClasspath( Map<String, Artifact> pluginArtifactMap, Artifact surefireArtifact ) |
160 | |
throws ArtifactResolutionException, ArtifactNotFoundException |
161 | |
{ |
162 | 0 | List<String> files = new ArrayList<String>(); |
163 | 0 | if ( surefireArtifact != null ) |
164 | |
{ |
165 | 0 | final ArtifactResolutionResult artifactResolutionResult = resolveArtifact( null, surefireArtifact ); |
166 | 0 | for ( Artifact artifact : pluginArtifactMap.values() ) |
167 | |
{ |
168 | 0 | if ( !artifactResolutionResult.getArtifacts().contains( artifact ) ) |
169 | |
{ |
170 | 0 | files.add( artifact.getFile().getAbsolutePath() ); |
171 | |
} |
172 | 0 | } |
173 | 0 | } |
174 | |
else |
175 | |
{ |
176 | |
|
177 | 0 | for ( Artifact artifact : pluginArtifactMap.values() ) |
178 | |
{ |
179 | 0 | files.add( artifact.getFile().getPath() ); |
180 | 0 | } |
181 | |
} |
182 | 0 | return new Classpath( files ); |
183 | |
} |
184 | |
|
185 | |
} |