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.Iterator;
25 import java.util.List;
26 import java.util.Map;
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;
43
44
45
46
47
48
49
50 public class SurefireDependencyResolver
51 {
52
53 private final ArtifactResolver artifactResolver;
54
55 private final ArtifactFactory artifactFactory;
56
57 private final org.apache.maven.plugin.logging.Log log;
58
59 private final ArtifactRepository localRepository;
60
61 private final List remoteRepositories;
62
63 private final ArtifactMetadataSource artifactMetadataSource;
64
65 private final String pluginName;
66
67 protected SurefireDependencyResolver( ArtifactResolver artifactResolver, ArtifactFactory artifactFactory, Log log,
68 ArtifactRepository localRepository, List 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 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();
135
136 for ( Iterator i = result.getArtifacts().iterator(); i.hasNext(); )
137 {
138 Artifact artifact = (Artifact) i.next();
139
140 log.debug(
141 "Adding to " + pluginName + " test classpath: " + artifact.getFile().getAbsolutePath() + " Scope: "
142 + artifact.getScope() );
143
144 files.add( artifact.getFile().getAbsolutePath() );
145 }
146 return new Classpath( files );
147 }
148
149 public Classpath addProviderToClasspath( Map pluginArtifactMap, Artifact surefireArtifact )
150 throws ArtifactResolutionException, ArtifactNotFoundException
151 {
152 List files = new ArrayList();
153 if ( surefireArtifact != null )
154 {
155 final ArtifactResolutionResult artifactResolutionResult = resolveArtifact( null, surefireArtifact );
156 for ( Iterator iterator = pluginArtifactMap.values().iterator(); iterator.hasNext(); )
157 {
158 Artifact artifact = (Artifact) iterator.next();
159 if ( !artifactResolutionResult.getArtifacts().contains( artifact ) )
160 {
161 files.add( artifact.getFile().getAbsolutePath() );
162 }
163 }
164 }
165 else
166 {
167
168 for ( Iterator iterator = pluginArtifactMap.values().iterator(); iterator.hasNext(); )
169 {
170 Artifact artifact = (Artifact) iterator.next();
171 files.add( artifact.getFile().getPath() );
172 }
173 }
174 return new Classpath( files );
175 }
176
177 }