Skip to content

Commit

Permalink
[SUREFIRE-1564] Can't override platform version through project/plugi…
Browse files Browse the repository at this point in the history
…n dependencies
  • Loading branch information
sormuras committed Oct 6, 2018
1 parent 25fadfc commit 242c0e8
Show file tree
Hide file tree
Showing 24 changed files with 786 additions and 521 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@
* under the License.
*/

import java.util.ArrayList;
import java.util.Collection;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.surefire.booter.Classpath;

import javax.annotation.Nonnull;
Expand All @@ -41,4 +46,16 @@ public static void setCachedClasspath( @Nonnull String key, @Nonnull Classpath c
{
CLASSPATHS.put( key, classpath );
}

public static Classpath setCachedClasspath( @Nonnull String key, @Nonnull Set<Artifact> artifacts )
{
Collection<String> files = new ArrayList<String>();
for ( Artifact artifact : artifacts )
{
files.add( artifact.getFile().getAbsolutePath() );
}
Classpath classpath = new Classpath( files );
setCachedClasspath( key, classpath );
return classpath;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
* under the License.
*/

import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
import org.apache.maven.artifact.resolver.ArtifactResolutionException;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.resolver.AbstractArtifactResolutionException;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.surefire.booter.Classpath;

import javax.annotation.Nonnull;
import java.util.Set;

/**
* @author Kristian Rosenvold
Expand All @@ -37,8 +37,8 @@ public interface ProviderInfo
boolean isApplicable();

@Nonnull
Classpath getProviderClasspath()
throws ArtifactResolutionException, ArtifactNotFoundException;
Set<Artifact> getProviderClasspath()
throws AbstractArtifactResolutionException;

void addProviderProperties() throws MojoExecutionException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@
* under the License.
*/

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.factory.ArtifactFactory;
import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
Expand All @@ -37,11 +33,19 @@
import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
import org.apache.maven.artifact.versioning.OverConstrainedVersionException;
import org.apache.maven.artifact.versioning.VersionRange;
import org.apache.maven.surefire.booter.Classpath;
import org.apache.maven.plugin.surefire.log.api.ConsoleLogger;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static java.util.Collections.singleton;
import static org.apache.maven.artifact.Artifact.SCOPE_TEST;
import static org.apache.maven.artifact.versioning.VersionRange.createFromVersion;

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

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

return artifactResolver.resolveTransitively( Collections.singleton( providerArtifact ), originatingArtifact,
return artifactResolver.resolveTransitively( singleton( providerArtifact ), originatingArtifact,
localRepository, remoteRepositories, artifactMetadataSource,
filter );
}

@Nonnull
public Classpath getProviderClasspath( String provider, String version, Artifact filteredArtifact )
@SuppressWarnings( "unchecked" )
public Set<Artifact> getProviderClasspath( String provider, String version, Artifact filteredArtifact )
throws ArtifactNotFoundException, ArtifactResolutionException
{
Classpath classPath = ClasspathCache.getCachedClassPath( provider );
if ( classPath == null )
{
Artifact providerArtifact = artifactFactory.createDependencyArtifact( "org.apache.maven.surefire", provider,
VersionRange.createFromVersion(
version ), "jar", null,
Artifact.SCOPE_TEST );
ArtifactResolutionResult result = resolveArtifact( filteredArtifact, providerArtifact );
List<String> files = new ArrayList<String>();
Artifact providerArtifact = artifactFactory.createDependencyArtifact( "org.apache.maven.surefire",
provider, createFromVersion( version ), "jar", null, SCOPE_TEST );

ArtifactResolutionResult result = resolveArtifact( filteredArtifact, providerArtifact );

if ( log.isDebugEnabled() )
{
for ( Object o : result.getArtifacts() )
{
Artifact artifact = (Artifact) o;

log.debug(
"Adding to " + pluginName + " test classpath: " + artifact.getFile().getAbsolutePath() + " Scope: "
+ artifact.getScope() );

files.add( artifact.getFile().getAbsolutePath() );
String artifactPath = artifact.getFile().getAbsolutePath();
String scope = artifact.getScope();
log.debug( "Adding to " + pluginName + " test classpath: " + artifactPath + " Scope: " + scope );
}
classPath = new Classpath( files );
ClasspathCache.setCachedClasspath( provider, classPath );
}
return classPath;

return result.getArtifacts();
}

public Classpath addProviderToClasspath( Map<String, Artifact> pluginArtifactMap, Artifact surefireArtifact )
public Set<Artifact> addProviderToClasspath( Map<String, Artifact> pluginArtifactMap, Artifact surefireArtifact )
throws ArtifactResolutionException, ArtifactNotFoundException
{
List<String> files = new ArrayList<String>();
Set<Artifact> providerArtifacts = new LinkedHashSet<Artifact>();
if ( surefireArtifact != null )
{
final ArtifactResolutionResult artifactResolutionResult = resolveArtifact( null, surefireArtifact );
ArtifactResolutionResult artifactResolutionResult = resolveArtifact( null, surefireArtifact );
for ( Artifact artifact : pluginArtifactMap.values() )
{
if ( !artifactResolutionResult.getArtifacts().contains( artifact ) )
{
files.add( artifact.getFile().getAbsolutePath() );
providerArtifacts.add( artifact );
}
}
}
else
{
// Bit of a brute force strategy if not found. Should probably be improved
for ( Artifact artifact : pluginArtifactMap.values() )
{
files.add( artifact.getFile().getPath() );
}
providerArtifacts.addAll( pluginArtifactMap.values() );
}
return new Classpath( files );
return providerArtifacts;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package org.apache.maven.plugin.surefire;

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import org.apache.maven.artifact.Artifact;
import org.apache.maven.surefire.booter.Classpath;
import org.codehaus.plexus.logging.Logger;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import static java.util.Collections.addAll;
import static org.apache.maven.shared.utils.StringUtils.split;

final class TestClassPath
{
private final Iterable<Artifact> artifacts;
private final File classesDirectory;
private final File testClassesDirectory;
private final String[] additionalClasspathElements;
private final Logger logger;

TestClassPath( Iterable<Artifact> artifacts,
File classesDirectory,
File testClassesDirectory,
String[] additionalClasspathElements,
Logger logger )
{
this.artifacts = artifacts;
this.classesDirectory = classesDirectory;
this.testClassesDirectory = testClassesDirectory;
this.additionalClasspathElements = additionalClasspathElements;
this.logger = logger;
}

void avoidArtifactDuplicates( Set<Artifact> providerArtifacts )
{
for ( Artifact artifact : artifacts )
{
Iterator<Artifact> it = providerArtifacts.iterator();
while ( it.hasNext() )
{
Artifact providerArtifact = it.next();
String classifier1 = providerArtifact.getClassifier();
String classifier2 = artifact.getClassifier();
if ( providerArtifact.getGroupId().equals( artifact.getGroupId() )
&& providerArtifact.getArtifactId().equals( artifact.getArtifactId() )
&& providerArtifact.getType().equals( artifact.getType() )
&& ( classifier1 == null ? classifier2 == null : classifier1.equals( classifier2 ) ) )
{
it.remove();
if ( logger.isDebugEnabled() )
{
logger.debug( "Removed artifact " + providerArtifact + " from provider. "
+ "Already appears in test classpath." );
}
}
}
}
}

Classpath toClasspath()
{
List<String> classpath = new ArrayList<String>();
classpath.add( testClassesDirectory.getAbsolutePath() );
classpath.add( classesDirectory.getAbsolutePath() );
for ( Artifact artifact : artifacts )
{
if ( artifact.getArtifactHandler().isAddedToClasspath() )
{
File file = artifact.getFile();
if ( file != null )
{
classpath.add( file.getAbsolutePath() );
}
}
}
if ( additionalClasspathElements != null )
{
for ( String additionalClasspathElement : additionalClasspathElements )
{
if ( additionalClasspathElement != null )
{
addAll( classpath, split( additionalClasspathElement, "," ) );
}
}
}

return new Classpath( classpath );
}
}
Loading

0 comments on commit 242c0e8

Please sign in to comment.