Skip to content

Commit 242c0e8

Browse files
committed
[SUREFIRE-1564] Can't override platform version through project/plugin dependencies
1 parent 25fadfc commit 242c0e8

File tree

24 files changed

+786
-521
lines changed

24 files changed

+786
-521
lines changed

maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/AbstractSurefireMojo.java

+91-129
Large diffs are not rendered by default.

maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/ClasspathCache.java

+17
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,12 @@
1919
* under the License.
2020
*/
2121

22+
import java.util.ArrayList;
23+
import java.util.Collection;
24+
import java.util.Set;
2225
import java.util.concurrent.ConcurrentHashMap;
26+
27+
import org.apache.maven.artifact.Artifact;
2328
import org.apache.maven.surefire.booter.Classpath;
2429

2530
import javax.annotation.Nonnull;
@@ -41,4 +46,16 @@ public static void setCachedClasspath( @Nonnull String key, @Nonnull Classpath c
4146
{
4247
CLASSPATHS.put( key, classpath );
4348
}
49+
50+
public static Classpath setCachedClasspath( @Nonnull String key, @Nonnull Set<Artifact> artifacts )
51+
{
52+
Collection<String> files = new ArrayList<String>();
53+
for ( Artifact artifact : artifacts )
54+
{
55+
files.add( artifact.getFile().getAbsolutePath() );
56+
}
57+
Classpath classpath = new Classpath( files );
58+
setCachedClasspath( key, classpath );
59+
return classpath;
60+
}
4461
}

maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/ProviderInfo.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@
1919
* under the License.
2020
*/
2121

22-
import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
23-
import org.apache.maven.artifact.resolver.ArtifactResolutionException;
22+
import org.apache.maven.artifact.Artifact;
23+
import org.apache.maven.artifact.resolver.AbstractArtifactResolutionException;
2424
import org.apache.maven.plugin.MojoExecutionException;
25-
import org.apache.maven.surefire.booter.Classpath;
2625

2726
import javax.annotation.Nonnull;
27+
import java.util.Set;
2828

2929
/**
3030
* @author Kristian Rosenvold
@@ -37,8 +37,8 @@ public interface ProviderInfo
3737
boolean isApplicable();
3838

3939
@Nonnull
40-
Classpath getProviderClasspath()
41-
throws ArtifactResolutionException, ArtifactNotFoundException;
40+
Set<Artifact> getProviderClasspath()
41+
throws AbstractArtifactResolutionException;
4242

4343
void addProviderProperties() throws MojoExecutionException;
4444
}

maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/SurefireDependencyResolver.java

+29-35
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,6 @@
1919
* under the License.
2020
*/
2121

22-
import java.util.ArrayList;
23-
import java.util.Collections;
24-
import java.util.List;
25-
import java.util.Map;
2622
import org.apache.maven.artifact.Artifact;
2723
import org.apache.maven.artifact.factory.ArtifactFactory;
2824
import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
@@ -37,11 +33,19 @@
3733
import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
3834
import org.apache.maven.artifact.versioning.OverConstrainedVersionException;
3935
import org.apache.maven.artifact.versioning.VersionRange;
40-
import org.apache.maven.surefire.booter.Classpath;
4136
import org.apache.maven.plugin.surefire.log.api.ConsoleLogger;
4237

4338
import javax.annotation.Nonnull;
4439
import javax.annotation.Nullable;
40+
import java.util.Collections;
41+
import java.util.LinkedHashSet;
42+
import java.util.List;
43+
import java.util.Map;
44+
import java.util.Set;
45+
46+
import static java.util.Collections.singleton;
47+
import static org.apache.maven.artifact.Artifact.SCOPE_TEST;
48+
import static org.apache.maven.artifact.versioning.VersionRange.createFromVersion;
4549

4650
/**
4751
* Does dependency resolution and artifact handling for the surefire plugin.
@@ -123,65 +127,55 @@ private ArtifactResolutionResult resolveArtifact( Artifact filteredArtifact, Art
123127

124128
Artifact originatingArtifact = artifactFactory.createBuildArtifact( "dummy", "dummy", "1.0", "jar" );
125129

126-
return artifactResolver.resolveTransitively( Collections.singleton( providerArtifact ), originatingArtifact,
130+
return artifactResolver.resolveTransitively( singleton( providerArtifact ), originatingArtifact,
127131
localRepository, remoteRepositories, artifactMetadataSource,
128132
filter );
129133
}
130134

131135
@Nonnull
132-
public Classpath getProviderClasspath( String provider, String version, Artifact filteredArtifact )
136+
@SuppressWarnings( "unchecked" )
137+
public Set<Artifact> getProviderClasspath( String provider, String version, Artifact filteredArtifact )
133138
throws ArtifactNotFoundException, ArtifactResolutionException
134139
{
135-
Classpath classPath = ClasspathCache.getCachedClassPath( provider );
136-
if ( classPath == null )
137-
{
138-
Artifact providerArtifact = artifactFactory.createDependencyArtifact( "org.apache.maven.surefire", provider,
139-
VersionRange.createFromVersion(
140-
version ), "jar", null,
141-
Artifact.SCOPE_TEST );
142-
ArtifactResolutionResult result = resolveArtifact( filteredArtifact, providerArtifact );
143-
List<String> files = new ArrayList<String>();
140+
Artifact providerArtifact = artifactFactory.createDependencyArtifact( "org.apache.maven.surefire",
141+
provider, createFromVersion( version ), "jar", null, SCOPE_TEST );
142+
143+
ArtifactResolutionResult result = resolveArtifact( filteredArtifact, providerArtifact );
144144

145+
if ( log.isDebugEnabled() )
146+
{
145147
for ( Object o : result.getArtifacts() )
146148
{
147149
Artifact artifact = (Artifact) o;
148-
149-
log.debug(
150-
"Adding to " + pluginName + " test classpath: " + artifact.getFile().getAbsolutePath() + " Scope: "
151-
+ artifact.getScope() );
152-
153-
files.add( artifact.getFile().getAbsolutePath() );
150+
String artifactPath = artifact.getFile().getAbsolutePath();
151+
String scope = artifact.getScope();
152+
log.debug( "Adding to " + pluginName + " test classpath: " + artifactPath + " Scope: " + scope );
154153
}
155-
classPath = new Classpath( files );
156-
ClasspathCache.setCachedClasspath( provider, classPath );
157154
}
158-
return classPath;
155+
156+
return result.getArtifacts();
159157
}
160158

161-
public Classpath addProviderToClasspath( Map<String, Artifact> pluginArtifactMap, Artifact surefireArtifact )
159+
public Set<Artifact> addProviderToClasspath( Map<String, Artifact> pluginArtifactMap, Artifact surefireArtifact )
162160
throws ArtifactResolutionException, ArtifactNotFoundException
163161
{
164-
List<String> files = new ArrayList<String>();
162+
Set<Artifact> providerArtifacts = new LinkedHashSet<Artifact>();
165163
if ( surefireArtifact != null )
166164
{
167-
final ArtifactResolutionResult artifactResolutionResult = resolveArtifact( null, surefireArtifact );
165+
ArtifactResolutionResult artifactResolutionResult = resolveArtifact( null, surefireArtifact );
168166
for ( Artifact artifact : pluginArtifactMap.values() )
169167
{
170168
if ( !artifactResolutionResult.getArtifacts().contains( artifact ) )
171169
{
172-
files.add( artifact.getFile().getAbsolutePath() );
170+
providerArtifacts.add( artifact );
173171
}
174172
}
175173
}
176174
else
177175
{
178176
// Bit of a brute force strategy if not found. Should probably be improved
179-
for ( Artifact artifact : pluginArtifactMap.values() )
180-
{
181-
files.add( artifact.getFile().getPath() );
182-
}
177+
providerArtifacts.addAll( pluginArtifactMap.values() );
183178
}
184-
return new Classpath( files );
179+
return providerArtifacts;
185180
}
186-
187181
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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.surefire.booter.Classpath;
24+
import org.codehaus.plexus.logging.Logger;
25+
26+
import java.io.File;
27+
import java.util.ArrayList;
28+
import java.util.Iterator;
29+
import java.util.List;
30+
import java.util.Set;
31+
32+
import static java.util.Collections.addAll;
33+
import static org.apache.maven.shared.utils.StringUtils.split;
34+
35+
final class TestClassPath
36+
{
37+
private final Iterable<Artifact> artifacts;
38+
private final File classesDirectory;
39+
private final File testClassesDirectory;
40+
private final String[] additionalClasspathElements;
41+
private final Logger logger;
42+
43+
TestClassPath( Iterable<Artifact> artifacts,
44+
File classesDirectory,
45+
File testClassesDirectory,
46+
String[] additionalClasspathElements,
47+
Logger logger )
48+
{
49+
this.artifacts = artifacts;
50+
this.classesDirectory = classesDirectory;
51+
this.testClassesDirectory = testClassesDirectory;
52+
this.additionalClasspathElements = additionalClasspathElements;
53+
this.logger = logger;
54+
}
55+
56+
void avoidArtifactDuplicates( Set<Artifact> providerArtifacts )
57+
{
58+
for ( Artifact artifact : artifacts )
59+
{
60+
Iterator<Artifact> it = providerArtifacts.iterator();
61+
while ( it.hasNext() )
62+
{
63+
Artifact providerArtifact = it.next();
64+
String classifier1 = providerArtifact.getClassifier();
65+
String classifier2 = artifact.getClassifier();
66+
if ( providerArtifact.getGroupId().equals( artifact.getGroupId() )
67+
&& providerArtifact.getArtifactId().equals( artifact.getArtifactId() )
68+
&& providerArtifact.getType().equals( artifact.getType() )
69+
&& ( classifier1 == null ? classifier2 == null : classifier1.equals( classifier2 ) ) )
70+
{
71+
it.remove();
72+
if ( logger.isDebugEnabled() )
73+
{
74+
logger.debug( "Removed artifact " + providerArtifact + " from provider. "
75+
+ "Already appears in test classpath." );
76+
}
77+
}
78+
}
79+
}
80+
}
81+
82+
Classpath toClasspath()
83+
{
84+
List<String> classpath = new ArrayList<String>();
85+
classpath.add( testClassesDirectory.getAbsolutePath() );
86+
classpath.add( classesDirectory.getAbsolutePath() );
87+
for ( Artifact artifact : artifacts )
88+
{
89+
if ( artifact.getArtifactHandler().isAddedToClasspath() )
90+
{
91+
File file = artifact.getFile();
92+
if ( file != null )
93+
{
94+
classpath.add( file.getAbsolutePath() );
95+
}
96+
}
97+
}
98+
if ( additionalClasspathElements != null )
99+
{
100+
for ( String additionalClasspathElement : additionalClasspathElements )
101+
{
102+
if ( additionalClasspathElement != null )
103+
{
104+
addAll( classpath, split( additionalClasspathElement, "," ) );
105+
}
106+
}
107+
}
108+
109+
return new Classpath( classpath );
110+
}
111+
}

0 commit comments

Comments
 (0)