[MNG-7400] Allow more WorkspaceReaders to participate

This closes #668
This commit is contained in:
Christoph Läubrich 2022-01-29 18:09:47 +01:00 committed by Michael Osipov
parent 0a118d6e24
commit 6f14196846
1 changed files with 56 additions and 29 deletions

View File

@ -232,26 +232,15 @@ public class DefaultMaven
return addExceptionToResult( result, e ); return addExceptionToResult( result, e );
} }
WorkspaceReader reactorWorkspace;
try try
{ {
reactorWorkspace = container.lookup( WorkspaceReader.class, ReactorReader.HINT ); setupWorkspaceReader( session, repoSession );
} }
catch ( ComponentLookupException e ) catch ( ComponentLookupException e )
{ {
return addExceptionToResult( result, e ); return addExceptionToResult( result, e );
} }
//
// Desired order of precedence for local artifact repositories
//
// Reactor
// Workspace
// User Local Repository
//
repoSession.setWorkspaceReader( ChainedWorkspaceReader.newInstance( reactorWorkspace,
repoSession.getWorkspaceReader() ) );
repoSession.setReadOnly(); repoSession.setReadOnly();
ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader(); ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();
@ -326,6 +315,34 @@ public class DefaultMaven
return result; return result;
} }
private void setupWorkspaceReader( MavenSession session, DefaultRepositorySystemSession repoSession )
throws ComponentLookupException
{
// Desired order of precedence for workspace readers before querying the local artifact repositories
List<WorkspaceReader> workspaceReaders = new ArrayList<WorkspaceReader>();
// 1) Reactor workspace reader
workspaceReaders.add( container.lookup( WorkspaceReader.class, ReactorReader.HINT ) );
// 2) Repository system session-scoped workspace reader
WorkspaceReader repoWorkspaceReader = repoSession.getWorkspaceReader();
if ( repoWorkspaceReader != null )
{
workspaceReaders.add( repoWorkspaceReader );
}
// 3) .. n) Project-scoped workspace readers
for ( WorkspaceReader workspaceReader : getProjectScopedExtensionComponents( session.getProjects(),
WorkspaceReader.class ) )
{
if ( workspaceReaders.contains( workspaceReader ) )
{
continue;
}
workspaceReaders.add( workspaceReader );
}
WorkspaceReader[] readers = workspaceReaders.toArray( new WorkspaceReader[0] );
repoSession.setWorkspaceReader( new ChainedWorkspaceReader( readers ) );
}
private void afterSessionEnd( Collection<MavenProject> projects, MavenSession session ) private void afterSessionEnd( Collection<MavenProject> projects, MavenSession session )
throws MavenExecutionException throws MavenExecutionException
{ {
@ -369,9 +386,6 @@ public class DefaultMaven
{ {
Collection<AbstractMavenLifecycleParticipant> lifecycleListeners = new LinkedHashSet<>(); Collection<AbstractMavenLifecycleParticipant> lifecycleListeners = new LinkedHashSet<>();
ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();
try
{
try try
{ {
lifecycleListeners.addAll( container.lookupList( AbstractMavenLifecycleParticipant.class ) ); lifecycleListeners.addAll( container.lookupList( AbstractMavenLifecycleParticipant.class ) );
@ -382,34 +396,47 @@ public class DefaultMaven
logger.warn( "Failed to lookup lifecycle participants: " + e.getMessage() ); logger.warn( "Failed to lookup lifecycle participants: " + e.getMessage() );
} }
lifecycleListeners.addAll( getProjectScopedExtensionComponents( projects,
AbstractMavenLifecycleParticipant.class ) );
return lifecycleListeners;
}
protected <T> Collection<T> getProjectScopedExtensionComponents( Collection<MavenProject> projects, Class<T> role )
{
Collection<T> foundComponents = new LinkedHashSet<>();
Collection<ClassLoader> scannedRealms = new HashSet<>(); Collection<ClassLoader> scannedRealms = new HashSet<>();
Thread currentThread = Thread.currentThread();
ClassLoader originalContextClassLoader = currentThread.getContextClassLoader();
try
{
for ( MavenProject project : projects ) for ( MavenProject project : projects )
{ {
ClassLoader projectRealm = project.getClassRealm(); ClassLoader projectRealm = project.getClassRealm();
if ( projectRealm != null && scannedRealms.add( projectRealm ) ) if ( projectRealm != null && scannedRealms.add( projectRealm ) )
{ {
Thread.currentThread().setContextClassLoader( projectRealm ); currentThread.setContextClassLoader( projectRealm );
try try
{ {
lifecycleListeners.addAll( container.lookupList( AbstractMavenLifecycleParticipant.class ) ); foundComponents.addAll( container.lookupList( role ) );
} }
catch ( ComponentLookupException e ) catch ( ComponentLookupException e )
{ {
// this is just silly, lookupList should return an empty list! // this is just silly, lookupList should return an empty list!
logger.warn( "Failed to lookup lifecycle participants: " + e.getMessage() ); logger.warn( "Failed to lookup " + role + ": " + e.getMessage() );
} }
} }
} }
return foundComponents;
} }
finally finally
{ {
Thread.currentThread().setContextClassLoader( originalClassLoader ); currentThread.setContextClassLoader( originalContextClassLoader );
} }
return lifecycleListeners;
} }
private MavenExecutionResult addExceptionToResult( MavenExecutionResult result, Throwable e ) private MavenExecutionResult addExceptionToResult( MavenExecutionResult result, Throwable e )