Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 46 additions & 56 deletions maven-core/src/main/java/org/apache/maven/ReactorReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,20 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.inject.Inject;
import javax.inject.Named;

import org.apache.maven.artifact.ArtifactUtils;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Model;
Expand All @@ -49,6 +49,13 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.inject.Inject;
import javax.inject.Named;

import static java.util.function.Function.identity;
import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.toMap;

/**
* An implementation of a workspace reader that knows how to search the Maven reactor for artifacts, either as packaged
* jar if it has been built, or only compile output directory if packaging hasn't happened yet.
Expand All @@ -72,26 +79,22 @@ class ReactorReader
private final Map<String, List<MavenProject>> projectsByGA;
private final WorkspaceRepository repository;

private Function<MavenProject, String> projectIntoKey =
s -> ArtifactUtils.key( s.getGroupId(), s.getArtifactId(), s.getVersion() );

private Function<MavenProject, String> projectIntoVersionlessKey =
s -> ArtifactUtils.versionlessKey( s.getGroupId(), s.getArtifactId() );

@Inject
ReactorReader( MavenSession session )
{
this.session = session;
this.projectsByGAV = new HashMap<>( session.getAllProjects().size() * 2 );
session.getAllProjects().forEach( project ->
{
String projectId = ArtifactUtils.key( project.getGroupId(), project.getArtifactId(), project.getVersion() );
this.projectsByGAV.put( projectId, project );
} );

projectsByGA = new HashMap<>( projectsByGAV.size() * 2 );
for ( MavenProject project : projectsByGAV.values() )
{
String key = ArtifactUtils.versionlessKey( project.getGroupId(), project.getArtifactId() );

List<MavenProject> projects = projectsByGA.computeIfAbsent( key, k -> new ArrayList<>( 1 ) );
this.projectsByGAV =
session.getAllProjects().stream()
.collect( toMap( projectIntoKey, identity() ) );

projects.add( project );
}
this.projectsByGA = projectsByGAV.values().stream()
.collect( groupingBy( projectIntoVersionlessKey ) );

repository = new WorkspaceRepository( "reactor", new HashSet<>( projectsByGAV.keySet() ) );
}
Expand Down Expand Up @@ -128,23 +131,11 @@ public List<String> findVersions( Artifact artifact )
{
String key = ArtifactUtils.versionlessKey( artifact.getGroupId(), artifact.getArtifactId() );

List<MavenProject> projects = projectsByGA.get( key );
if ( projects == null || projects.isEmpty() )
{
return Collections.emptyList();
}

List<String> versions = new ArrayList<>();

for ( MavenProject project : projects )
{
if ( find( project, artifact ) != null )
{
versions.add( project.getVersion() );
}
}

return Collections.unmodifiableList( versions );
return Optional.ofNullable( projectsByGA.get( key ) )
.orElse( Collections.emptyList() ).stream()
.filter( s -> Objects.nonNull( find( s, artifact ) ) )
.map( MavenProject::getVersion )
.collect( Collectors.collectingAndThen( Collectors.toList(), Collections::unmodifiableList ) );
}

@Override
Expand Down Expand Up @@ -334,28 +325,27 @@ private Artifact findMatchingArtifact( MavenProject project, Artifact requestedA
return mainArtifact;
}

for ( Artifact attachedArtifact : RepositoryUtils.toArtifacts( project.getAttachedArtifacts() ) )
{
if ( attachedArtifactComparison( requestedArtifact, attachedArtifact ) )
{
return attachedArtifact;
}
}

return null;
return RepositoryUtils.toArtifacts( project.getAttachedArtifacts() ).stream()
.filter( isRequestedArtifact( requestedArtifact ) )
.findFirst()
.orElse( null );
}

private boolean attachedArtifactComparison( Artifact requested, Artifact attached )
/**
* We are taking as much as we can from the DefaultArtifact.equals(). The requested artifact has no file, so we want
* to remove that from the comparison.
*
* @param requestArtifact checked against the given artifact.
* @return true if equals, false otherwise.
*/
private Predicate<Artifact> isRequestedArtifact( Artifact requestArtifact )
{
//
// We are taking as much as we can from the DefaultArtifact.equals(). The requested artifact has no file so
// we want to remove that from the comparison.
//
return requested.getArtifactId().equals( attached.getArtifactId() )
&& requested.getGroupId().equals( attached.getGroupId() )
&& requested.getVersion().equals( attached.getVersion() )
&& requested.getExtension().equals( attached.getExtension() )
&& requested.getClassifier().equals( attached.getClassifier() );
return s -> s.getArtifactId().equals( requestArtifact.getArtifactId() )
&& s.getGroupId().equals( requestArtifact.getGroupId() )
&& s.getVersion().equals( requestArtifact.getVersion() )
&& s.getExtension().equals( requestArtifact.getExtension() )
&& s.getClassifier().equals( requestArtifact.getClassifier() );

}

/**
Expand Down
47 changes: 15 additions & 32 deletions maven-core/src/main/java/org/apache/maven/RepositoryUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

import org.apache.maven.artifact.handler.ArtifactHandler;
import org.apache.maven.artifact.handler.DefaultArtifactHandler;
Expand Down Expand Up @@ -170,32 +172,21 @@ public static Dependency toDependency( org.apache.maven.artifact.Artifact artifa

Artifact result = toArtifact( artifact );

List<Exclusion> excl = null;
if ( exclusions != null )
{
excl = new ArrayList<>( exclusions.size() );
for ( org.apache.maven.model.Exclusion exclusion : exclusions )
{
excl.add( toExclusion( exclusion ) );
}
}

List<Exclusion> excl = Optional.ofNullable( exclusions )
.orElse( Collections.emptyList() )
.stream()
.map( RepositoryUtils::toExclusion )
.collect( Collectors.toList() );
return new Dependency( result, artifact.getScope(), artifact.isOptional(), excl );
}

public static List<RemoteRepository> toRepos( List<ArtifactRepository> repos )
{
if ( repos == null )
{
return null;
}

List<RemoteRepository> results = new ArrayList<>( repos.size() );
for ( ArtifactRepository repo : repos )
{
results.add( toRepo( repo ) );
}
return results;
return Optional.ofNullable( repos )
.orElse( Collections.emptyList() )
.stream()
.map( RepositoryUtils::toRepo )
.collect( Collectors.toList() );
}

public static RemoteRepository toRepo( ArtifactRepository repo )
Expand Down Expand Up @@ -318,11 +309,8 @@ public static Dependency toDependency( org.apache.maven.model.Dependency depende
new DefaultArtifact( dependency.getGroupId(), dependency.getArtifactId(), dependency.getClassifier(), null,
dependency.getVersion(), props, stereotype );

List<Exclusion> exclusions = new ArrayList<>( dependency.getExclusions().size() );
for ( org.apache.maven.model.Exclusion exclusion : dependency.getExclusions() )
{
exclusions.add( toExclusion( exclusion ) );
}
List<Exclusion> exclusions =
dependency.getExclusions().stream().map( RepositoryUtils::toExclusion ).collect( Collectors.toList() );

return new Dependency( artifact,
dependency.getScope(),
Expand Down Expand Up @@ -363,12 +351,7 @@ public ArtifactType get( String stereotypeId )

public static Collection<Artifact> toArtifacts( Collection<org.apache.maven.artifact.Artifact> artifactsToConvert )
{
List<Artifact> artifacts = new ArrayList<>();
for ( org.apache.maven.artifact.Artifact a : artifactsToConvert )
{
artifacts.add( toArtifact( a ) );
}
return artifacts;
return artifactsToConvert.stream().map( RepositoryUtils::toArtifact ).collect( Collectors.toList() );
}

public static WorkspaceRepository getWorkspace( RepositorySystemSession session )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@
* under the License.
*/

import java.util.List;
import java.util.function.Predicate;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.model.Exclusion;

import java.util.List;

/**
* Filter to exclude from a list of artifact patterns.
*/
Expand All @@ -38,31 +39,33 @@ public ExclusionArtifactFilter( List<Exclusion> exclusions )
this.exclusions = exclusions;
}

private Predicate<Exclusion> sameArtifactId( Artifact artifact )
{
return exclusion -> exclusion.getArtifactId().equals( artifact.getArtifactId() );
}

private Predicate<Exclusion> sameGroupId( Artifact artifact )
{
return exclusion -> exclusion.getGroupId().equals( artifact.getGroupId() );
}

private Predicate<Exclusion> groupIdIsWildcard = exclusion -> WILDCARD.equals( exclusion.getGroupId() );

private Predicate<Exclusion> artifactIdIsWildcard = exclusion -> WILDCARD.equals( exclusion.getArtifactId() );

private Predicate<Exclusion> groupIdAndArtifactIdIsWildcard = groupIdIsWildcard.and( artifactIdIsWildcard );

private Predicate<Exclusion> exclude( Artifact artifact )
{
return groupIdAndArtifactIdIsWildcard
.or( groupIdIsWildcard.and( sameArtifactId( artifact ) ) )
.or( artifactIdIsWildcard.and( sameGroupId( artifact ) ) )
.or( sameGroupId( artifact ).and( sameArtifactId( artifact ) ) );
}

@Override
public boolean include( Artifact artifact )
{
for ( Exclusion exclusion : exclusions )
{
if ( WILDCARD.equals( exclusion.getGroupId() ) && WILDCARD.equals( exclusion.getArtifactId() ) )
{
return false;
}
if ( WILDCARD.equals( exclusion.getGroupId() )
&& exclusion.getArtifactId().equals( artifact.getArtifactId() ) )
{
return false;
}
if ( WILDCARD.equals( exclusion.getArtifactId() )
&& exclusion.getGroupId().equals( artifact.getGroupId() ) )
{
return false;
}
if ( exclusion.getGroupId().equals( artifact.getGroupId() )
&& exclusion.getArtifactId().equals( artifact.getArtifactId() ) )
{
return false;
}
}
return true;
return !exclusions.stream().anyMatch( exclude( artifact ) );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,9 @@ public void setProjects( List<MavenProject> projects )
if ( !projects.isEmpty() )
{
this.currentProject = projects.get( 0 );
this.topLevelProject = currentProject;
for ( MavenProject project : projects )
{
if ( project.isExecutionRoot() )
{
topLevelProject = project;
break;
}
}
this.topLevelProject =
projects.stream().filter( project -> project.isExecutionRoot() ).findFirst()
.orElse( currentProject );
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@
*/

import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

import org.codehaus.plexus.classworlds.realm.ClassRealm;

import static java.util.function.Function.identity;
import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.toMap;

/**
* Provides information about artifacts (identified by groupId:artifactId string key) and classpath elements exported by
* Maven core itself and loaded Maven core extensions.
Expand All @@ -47,13 +49,9 @@ public CoreExports( CoreExtensionEntry entry )

public CoreExports( ClassRealm realm, Set<String> exportedArtifacts, Set<String> exportedPackages )
{
Map<String, ClassLoader> packages = new LinkedHashMap<>();
for ( String pkg : exportedPackages )
{
packages.put( pkg, realm );
}
this.artifacts = Collections.unmodifiableSet( new HashSet<>( exportedArtifacts ) );
this.packages = Collections.unmodifiableMap( new HashMap<>( packages ) );
this.packages = exportedPackages.stream().collect(
collectingAndThen( toMap( identity(), v -> realm ), Collections::unmodifiableMap ) );
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.List;
import java.util.Map;

import static java.util.function.Function.identity;
import static java.util.stream.Collectors.toMap;

/**
Expand Down Expand Up @@ -57,7 +58,7 @@ public DefaultLifecycleMapping()
public DefaultLifecycleMapping( final List<Lifecycle> lifecycles )
{
this.lifecycleMap = Collections.unmodifiableMap(
lifecycles.stream().collect( toMap( Lifecycle::getId, l -> l ) )
lifecycles.stream().collect( toMap( Lifecycle::getId, identity() ) )
);
}

Expand Down
Loading