Skip to content

Commit

Permalink
Backport of MNG-7400 - Allow more WorkspaceReader's to participate
Browse files Browse the repository at this point in the history
  • Loading branch information
Christoph Läubrich committed Jan 29, 2022
1 parent 83257bf commit ca599fe
Showing 1 changed file with 35 additions and 21 deletions.
56 changes: 35 additions & 21 deletions maven-core/src/main/java/org/apache/maven/DefaultMaven.java
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,13 @@ private MavenExecutionResult doExecute( MavenExecutionRequest request, MavenSess
return addExceptionToResult( result, e );
}

for ( WorkspaceReader workspaceReader : getProjectScopedExtensions( session.getProjects(),
WorkspaceReader.class ) )
{
repoSession.setWorkspaceReader( ChainedWorkspaceReader.newInstance( repoSession.getWorkspaceReader(),
workspaceReader ) );
}

WorkspaceReader reactorWorkspace;
try
{
Expand Down Expand Up @@ -382,27 +389,8 @@ private Collection<AbstractMavenLifecycleParticipant> getLifecycleParticipants(
logger.warn( "Failed to lookup lifecycle participants: " + e.getMessage() );
}

Collection<ClassLoader> scannedRealms = new HashSet<>();

for ( MavenProject project : projects )
{
ClassLoader projectRealm = project.getClassRealm();

if ( projectRealm != null && scannedRealms.add( projectRealm ) )
{
Thread.currentThread().setContextClassLoader( projectRealm );

try
{
lifecycleListeners.addAll( container.lookupList( AbstractMavenLifecycleParticipant.class ) );
}
catch ( ComponentLookupException e )
{
// this is just silly, lookupList should return an empty list!
logger.warn( "Failed to lookup lifecycle participants: " + e.getMessage() );
}
}
}
lifecycleListeners.addAll( getProjectScopedExtensions( projects,
AbstractMavenLifecycleParticipant.class ) );
}
finally
{
Expand All @@ -412,6 +400,32 @@ private Collection<AbstractMavenLifecycleParticipant> getLifecycleParticipants(
return lifecycleListeners;
}

protected <T> Collection<T> getProjectScopedExtensions( Collection<MavenProject> projects, Class<T> role )
{
Collection<ClassLoader> scannedRealms = new HashSet<>();
Collection<T> extensions = new LinkedHashSet<>();
for ( MavenProject project : projects )
{
ClassLoader projectRealm = project.getClassRealm();

if ( projectRealm != null && scannedRealms.add( projectRealm ) )
{
Thread.currentThread().setContextClassLoader( projectRealm );

try
{
extensions.addAll( container.lookupList( role ) );
}
catch ( ComponentLookupException e )
{
// this is just silly, lookupList should return an empty list!
logger.warn( "Failed to lookup " + role + ": " + e.getMessage() );
}
}
}
return extensions;
}

private MavenExecutionResult addExceptionToResult( MavenExecutionResult result, Throwable e )
{
if ( !result.getExceptions().contains( e ) )
Expand Down

0 comments on commit ca599fe

Please sign in to comment.