[MNG-7447] - Several Improvements by using Stream API

This commit is contained in:
Karl Heinz Marbaise 2021-09-30 19:10:44 +02:00
parent f7b073f3eb
commit c604db3c3a
No known key found for this signature in database
GPG Key ID: BF1518E0160788A2
7 changed files with 150 additions and 155 deletions

View File

@ -20,17 +20,17 @@ package org.apache.maven;
*/ */
import java.io.File; import java.io.File;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import javax.inject.Inject; import java.util.Optional;
import javax.inject.Named; import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import org.apache.maven.artifact.ArtifactUtils; import org.apache.maven.artifact.ArtifactUtils;
import org.apache.maven.execution.MavenSession; import org.apache.maven.execution.MavenSession;
@ -41,6 +41,13 @@ import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.repository.WorkspaceRepository; import org.eclipse.aether.repository.WorkspaceRepository;
import org.eclipse.aether.util.artifact.ArtifactIdUtils; import org.eclipse.aether.util.artifact.ArtifactIdUtils;
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 * 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. * jar if it has been built, or only compile output directory if packaging hasn't happened yet.
@ -57,32 +64,27 @@ class ReactorReader
private static final Collection<String> COMPILE_PHASE_TYPES = private static final Collection<String> COMPILE_PHASE_TYPES =
Arrays.asList( "jar", "ejb-client", "war", "rar", "ejb3", "par", "sar", "wsr", "har", "app-client" ); Arrays.asList( "jar", "ejb-client", "war", "rar", "ejb3", "par", "sar", "wsr", "har", "app-client" );
private Map<String, MavenProject> projectsByGAV; private final MavenSession session;
private final Map<String, MavenProject> projectsByGAV;
private final Map<String, List<MavenProject>> projectsByGA;
private final WorkspaceRepository repository;
private Map<String, List<MavenProject>> projectsByGA; private Function<MavenProject, String> projectIntoKey =
s -> ArtifactUtils.key( s.getGroupId(), s.getArtifactId(), s.getVersion() );
private WorkspaceRepository repository; private Function<MavenProject, String> projectIntoVersionlessKey =
s -> ArtifactUtils.versionlessKey( s.getGroupId(), s.getArtifactId() );
@Inject @Inject
ReactorReader( MavenSession session ) ReactorReader( MavenSession session )
{ {
projectsByGAV = session.getProjectMap(); this.session = session;
this.projectsByGAV =
session.getAllProjects().stream()
.collect( toMap( projectIntoKey, identity() ) );
projectsByGA = new HashMap<>( projectsByGAV.size() * 2 ); this.projectsByGA = projectsByGAV.values().stream()
for ( MavenProject project : projectsByGAV.values() ) .collect( groupingBy( projectIntoVersionlessKey ) );
{
String key = ArtifactUtils.versionlessKey( project.getGroupId(), project.getArtifactId() );
List<MavenProject> projects = projectsByGA.get( key );
if ( projects == null )
{
projects = new ArrayList<>( 1 );
projectsByGA.put( key, projects );
}
projects.add( project );
}
repository = new WorkspaceRepository( "reactor", new HashSet<>( projectsByGAV.keySet() ) ); repository = new WorkspaceRepository( "reactor", new HashSet<>( projectsByGAV.keySet() ) );
} }
@ -119,23 +121,11 @@ class ReactorReader
{ {
String key = ArtifactUtils.versionlessKey( artifact.getGroupId(), artifact.getArtifactId() ); String key = ArtifactUtils.versionlessKey( artifact.getGroupId(), artifact.getArtifactId() );
List<MavenProject> projects = projectsByGA.get( key ); return Optional.ofNullable( projectsByGA.get( key ) )
if ( projects == null || projects.isEmpty() ) .orElse( Collections.emptyList() ).stream()
{ .filter( s -> Objects.nonNull( find( s, artifact ) ) )
return Collections.emptyList(); .map( MavenProject::getVersion )
} .collect( Collectors.collectingAndThen( Collectors.toList(), Collections::unmodifiableList ) );
List<String> versions = new ArrayList<>();
for ( MavenProject project : projects )
{
if ( find( project, artifact ) != null )
{
versions.add( project.getVersion() );
}
}
return Collections.unmodifiableList( versions );
} }
@Override @Override
@ -190,6 +180,17 @@ class ReactorReader
return null; return null;
} }
private File determinePreviouslyPackagedArtifactFile( MavenProject project, Artifact artifact )
{
if ( artifact == null )
{
return null;
}
String fileName = String.format( "%s.%s", project.getBuild().getFinalName(), artifact.getExtension() );
return new File( project.getBuild().getDirectory(), fileName );
}
private boolean hasArtifactFileFromPackagePhase( Artifact projectArtifact ) private boolean hasArtifactFileFromPackagePhase( Artifact projectArtifact )
{ {
return projectArtifact != null && projectArtifact.getFile() != null && projectArtifact.getFile().exists(); return projectArtifact != null && projectArtifact.getFile() != null && projectArtifact.getFile().exists();
@ -218,28 +219,27 @@ class ReactorReader
return mainArtifact; return mainArtifact;
} }
for ( Artifact attachedArtifact : RepositoryUtils.toArtifacts( project.getAttachedArtifacts() ) ) return RepositoryUtils.toArtifacts( project.getAttachedArtifacts() ).stream()
{ .filter( isRequestedArtifact( requestedArtifact ) )
if ( attachedArtifactComparison( requestedArtifact, attachedArtifact ) ) .findFirst()
{ .orElse( null );
return attachedArtifact;
}
}
return 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 )
{ {
// return s -> s.getArtifactId().equals( requestArtifact.getArtifactId() )
// We are taking as much as we can from the DefaultArtifact.equals(). The requested artifact has no file so && s.getGroupId().equals( requestArtifact.getGroupId() )
// we want to remove that from the comparison. && s.getVersion().equals( requestArtifact.getVersion() )
// && s.getExtension().equals( requestArtifact.getExtension() )
return requested.getArtifactId().equals( attached.getArtifactId() ) && s.getClassifier().equals( requestArtifact.getClassifier() );
&& requested.getGroupId().equals( attached.getGroupId() )
&& requested.getVersion().equals( attached.getVersion() )
&& requested.getExtension().equals( attached.getExtension() )
&& requested.getClassifier().equals( attached.getClassifier() );
} }
/** /**

View File

@ -26,6 +26,8 @@ import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects; 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.ArtifactHandler;
import org.apache.maven.artifact.handler.DefaultArtifactHandler; import org.apache.maven.artifact.handler.DefaultArtifactHandler;
@ -170,32 +172,21 @@ public class RepositoryUtils
Artifact result = toArtifact( artifact ); Artifact result = toArtifact( artifact );
List<Exclusion> excl = null; List<Exclusion> excl = Optional.ofNullable( exclusions )
if ( exclusions != null ) .orElse( Collections.emptyList() )
{ .stream()
excl = new ArrayList<>( exclusions.size() ); .map( RepositoryUtils::toExclusion )
for ( org.apache.maven.model.Exclusion exclusion : exclusions ) .collect( Collectors.toList() );
{
excl.add( toExclusion( exclusion ) );
}
}
return new Dependency( result, artifact.getScope(), artifact.isOptional(), excl ); return new Dependency( result, artifact.getScope(), artifact.isOptional(), excl );
} }
public static List<RemoteRepository> toRepos( List<ArtifactRepository> repos ) public static List<RemoteRepository> toRepos( List<ArtifactRepository> repos )
{ {
if ( repos == null ) return Optional.ofNullable( repos )
{ .orElse( Collections.emptyList() )
return null; .stream()
} .map( RepositoryUtils::toRepo )
.collect( Collectors.toList() );
List<RemoteRepository> results = new ArrayList<>( repos.size() );
for ( ArtifactRepository repo : repos )
{
results.add( toRepo( repo ) );
}
return results;
} }
public static RemoteRepository toRepo( ArtifactRepository repo ) public static RemoteRepository toRepo( ArtifactRepository repo )
@ -316,11 +307,8 @@ public class RepositoryUtils
new DefaultArtifact( dependency.getGroupId(), dependency.getArtifactId(), dependency.getClassifier(), null, new DefaultArtifact( dependency.getGroupId(), dependency.getArtifactId(), dependency.getClassifier(), null,
dependency.getVersion(), props, stereotype ); dependency.getVersion(), props, stereotype );
List<Exclusion> exclusions = new ArrayList<>( dependency.getExclusions().size() ); List<Exclusion> exclusions =
for ( org.apache.maven.model.Exclusion exclusion : dependency.getExclusions() ) dependency.getExclusions().stream().map( RepositoryUtils::toExclusion ).collect( Collectors.toList() );
{
exclusions.add( toExclusion( exclusion ) );
}
Dependency result = new Dependency( artifact, Dependency result = new Dependency( artifact,
dependency.getScope(), dependency.getScope(),
@ -363,12 +351,7 @@ public class RepositoryUtils
public static Collection<Artifact> toArtifacts( Collection<org.apache.maven.artifact.Artifact> artifactsToConvert ) public static Collection<Artifact> toArtifacts( Collection<org.apache.maven.artifact.Artifact> artifactsToConvert )
{ {
List<Artifact> artifacts = new ArrayList<>(); return artifactsToConvert.stream().map( RepositoryUtils::toArtifact ).collect( Collectors.toList() );
for ( org.apache.maven.artifact.Artifact a : artifactsToConvert )
{
artifacts.add( toArtifact( a ) );
}
return artifacts;
} }
public static WorkspaceRepository getWorkspace( RepositorySystemSession session ) public static WorkspaceRepository getWorkspace( RepositorySystemSession session )

View File

@ -19,11 +19,12 @@ package org.apache.maven.artifact.resolver.filter;
* under the License. * under the License.
*/ */
import java.util.List;
import java.util.function.Predicate;
import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.Artifact;
import org.apache.maven.model.Exclusion; import org.apache.maven.model.Exclusion;
import java.util.List;
/** /**
* Filter to exclude from a list of artifact patterns. * Filter to exclude from a list of artifact patterns.
*/ */
@ -38,31 +39,33 @@ public class ExclusionArtifactFilter implements ArtifactFilter
this.exclusions = 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 @Override
public boolean include( Artifact artifact ) public boolean include( Artifact artifact )
{ {
for ( Exclusion exclusion : exclusions ) return !exclusions.stream().anyMatch( exclude( artifact ) );
{
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;
} }
} }

View File

@ -84,15 +84,9 @@ public class MavenSession
if ( !projects.isEmpty() ) if ( !projects.isEmpty() )
{ {
this.currentProject = projects.get( 0 ); this.currentProject = projects.get( 0 );
this.topLevelProject = currentProject; this.topLevelProject =
for ( MavenProject project : projects ) projects.stream().filter( project -> project.isExecutionRoot() ).findFirst()
{ .orElse( currentProject );
if ( project.isExecutionRoot() )
{
topLevelProject = project;
break;
}
}
} }
else else
{ {

View File

@ -20,14 +20,16 @@ package org.apache.maven.extension.internal;
*/ */
import java.util.Collections; import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import org.codehaus.plexus.classworlds.realm.ClassRealm; 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 * Provides information about artifacts (identified by groupId:artifactId string key) and classpath elements exported by
* Maven core itself and loaded Maven core extensions. * Maven core itself and loaded Maven core extensions.
@ -47,13 +49,9 @@ public class CoreExports
public CoreExports( ClassRealm realm, Set<String> exportedArtifacts, Set<String> exportedPackages ) 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.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 ) );
} }
/** /**

View File

@ -19,10 +19,14 @@ package org.apache.maven.lifecycle.mapping;
* under the License. * under the License.
*/ */
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import static java.util.function.Function.identity;
import static java.util.stream.Collectors.toMap;
/** /**
* DefaultLifecycleMapping * DefaultLifecycleMapping
*/ */
@ -38,7 +42,28 @@ public class DefaultLifecycleMapping
private Map<String, LifecyclePhase> phases; private Map<String, LifecyclePhase> phases;
/** /**
* Populates the lifecycle map from the injected list of lifecycle mappings (if not already done). * Default ctor for plexus compatibility: lifecycles are most commonly defined in Plexus XML, that does field
* injection. Still, for Plexus to be able to instantiate this class, default ctor is needed.
*
* @deprecated Should not be used in Java code.
*/
@Deprecated
public DefaultLifecycleMapping()
{
}
/**
* Ctor to be used in Java code/providers.
*/
public DefaultLifecycleMapping( final List<Lifecycle> lifecycles )
{
this.lifecycleMap = Collections.unmodifiableMap(
lifecycles.stream().collect( toMap( Lifecycle::getId, identity() ) )
);
}
/**
* Plexus: Populates the lifecycle map from the injected list of lifecycle mappings (if not already done).
*/ */
private void initLifecycleMap() private void initLifecycleMap()
{ {
@ -79,6 +104,7 @@ public class DefaultLifecycleMapping
} }
} }
@Override
public Map<String, Lifecycle> getLifecycles() public Map<String, Lifecycle> getLifecycles()
{ {
initLifecycleMap(); initLifecycleMap();
@ -86,6 +112,8 @@ public class DefaultLifecycleMapping
return lifecycleMap; return lifecycleMap;
} }
@Deprecated
@Override
public List<String> getOptionalMojos( String lifecycle ) public List<String> getOptionalMojos( String lifecycle )
{ {
return null; return null;
@ -110,7 +138,7 @@ public class DefaultLifecycleMapping
return null; return null;
} }
} }
@Deprecated @Deprecated
public Map<String, String> getPhases( String lifecycle ) public Map<String, String> getPhases( String lifecycle )
{ {

View File

@ -21,12 +21,13 @@ package org.apache.maven.model.profile;
import java.io.File; import java.io.File;
import java.util.Collections; import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.toMap;
/** /**
* Describes the environmental context used to determine the activation status of profiles. * Describes the environmental context used to determine the activation status of profiles.
* *
@ -230,8 +231,11 @@ public class DefaultProfileActivationContext
{ {
if ( projectProperties != null ) if ( projectProperties != null )
{ {
this.projectProperties = projectProperties.entrySet().stream()
this.projectProperties = Collections.unmodifiableMap( toMap( projectProperties ) ); .collect(
collectingAndThen(
toMap( k -> String.valueOf( k.getKey() ), v -> String.valueOf( v ) ),
Collections::unmodifiableMap ) );
} }
else else
{ {
@ -241,19 +245,4 @@ public class DefaultProfileActivationContext
return this; return this;
} }
private Map<String, String> toMap( Properties properties )
{
if ( properties == null )
{
return Collections.emptyMap();
}
Map<String, String> map = new HashMap<>();
Enumeration keys = properties.keys();
while ( keys.hasMoreElements() )
{
String key = (String) keys.nextElement();
map.put( key, properties.getProperty( key ) );
}
return map;
}
} }