You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by mi...@apache.org on 2022/02/12 20:13:03 UTC
[maven] branch MNG-7400 created (now c1b0207)
This is an automated email from the ASF dual-hosted git repository.
michaelo pushed a change to branch MNG-7400
in repository https://gitbox.apache.org/repos/asf/maven.git.
at c1b0207 [MNG-7400] Allow more WorkspaceReaders to participate
This branch includes the following new commits:
new c1b0207 [MNG-7400] Allow more WorkspaceReaders to participate
The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
[maven] 01/01: [MNG-7400] Allow more WorkspaceReaders to participate
Posted by mi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
michaelo pushed a commit to branch MNG-7400
in repository https://gitbox.apache.org/repos/asf/maven.git
commit c1b020762aeab7d04508c84adf4b1f4759148cb1
Author: Christoph Läubrich <ch...@laeubi-soft.de>
AuthorDate: Sat Jan 29 17:03:00 2022 +0100
[MNG-7400] Allow more WorkspaceReaders to participate
This closes #667
---
.../main/java/org/apache/maven/DefaultMaven.java | 103 ++++++++++++++-------
1 file changed, 67 insertions(+), 36 deletions(-)
diff --git a/maven-core/src/main/java/org/apache/maven/DefaultMaven.java b/maven-core/src/main/java/org/apache/maven/DefaultMaven.java
index 142dc13..01cd4e2 100644
--- a/maven-core/src/main/java/org/apache/maven/DefaultMaven.java
+++ b/maven-core/src/main/java/org/apache/maven/DefaultMaven.java
@@ -266,28 +266,15 @@ public class DefaultMaven
return addExceptionToResult( result, e );
}
- WorkspaceReader reactorWorkspace;
try
{
- reactorWorkspace = container.lookup( WorkspaceReader.class, ReactorReader.HINT );
+ setupWorkspaceReader( session, repoSession );
}
catch ( ComponentLookupException 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();
-
try
{
afterProjectsRead( session );
@@ -367,11 +354,40 @@ public class DefaultMaven
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 afterSessionStart( MavenSession session )
throws MavenExecutionException
{
// CHECKSTYLE_OFF: LineLength
- for ( AbstractMavenLifecycleParticipant listener : getLifecycleParticipants( Collections.emptyList() ) )
+ for ( AbstractMavenLifecycleParticipant listener : getExtensionComponents( Collections.emptyList(),
+ AbstractMavenLifecycleParticipant.class ) )
// CHECKSTYLE_ON: LineLength
{
listener.afterSessionStart( session );
@@ -384,7 +400,10 @@ public class DefaultMaven
ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();
try
{
- for ( AbstractMavenLifecycleParticipant listener : getLifecycleParticipants( session.getProjects() ) )
+ // CHECKSTYLE_OFF: LineLength
+ for ( AbstractMavenLifecycleParticipant listener : getExtensionComponents( session.getProjects(),
+ AbstractMavenLifecycleParticipant.class ) )
+ // CHECKSTYLE_ON: LineLength
{
Thread.currentThread().setContextClassLoader( listener.getClass().getClassLoader() );
@@ -403,7 +422,10 @@ public class DefaultMaven
ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();
try
{
- for ( AbstractMavenLifecycleParticipant listener : getLifecycleParticipants( projects ) )
+ // CHECKSTYLE_OFF: LineLength
+ for ( AbstractMavenLifecycleParticipant listener : getExtensionComponents( projects,
+ AbstractMavenLifecycleParticipant.class ) )
+ // CHECKSTYLE_ON: LineLength
{
Thread.currentThread().setContextClassLoader( listener.getClass().getClassLoader() );
@@ -463,51 +485,60 @@ public class DefaultMaven
}
}
- private Collection<AbstractMavenLifecycleParticipant> getLifecycleParticipants( Collection<MavenProject> projects )
+ private <T> Collection<T> getExtensionComponents( Collection<MavenProject> projects, Class<T> role )
{
- Collection<AbstractMavenLifecycleParticipant> lifecycleListeners = new LinkedHashSet<>();
+ Collection<T> foundComponents = new LinkedHashSet<>();
- ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();
try
{
- 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() );
- }
+ foundComponents.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() );
+ }
- Collection<ClassLoader> scannedRealms = new HashSet<>();
+ foundComponents.addAll( getProjectScopedExtensionComponents( projects, role ) );
+ return foundComponents;
+ }
+
+ protected <T> Collection<T> getProjectScopedExtensionComponents( Collection<MavenProject> projects, Class<T> role )
+ {
+
+ Collection<T> foundComponents = new LinkedHashSet<>();
+ Collection<ClassLoader> scannedRealms = new HashSet<>();
+
+ Thread currentThread = Thread.currentThread();
+ ClassLoader originalContextClassLoader = currentThread.getContextClassLoader();
+ try
+ {
for ( MavenProject project : projects )
{
ClassLoader projectRealm = project.getClassRealm();
if ( projectRealm != null && scannedRealms.add( projectRealm ) )
{
- Thread.currentThread().setContextClassLoader( projectRealm );
+ currentThread.setContextClassLoader( projectRealm );
try
{
- lifecycleListeners.addAll( container.lookupList( AbstractMavenLifecycleParticipant.class ) );
+ foundComponents.addAll( container.lookupList( role ) );
}
catch ( ComponentLookupException e )
{
// 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
{
- Thread.currentThread().setContextClassLoader( originalClassLoader );
+ currentThread.setContextClassLoader( originalContextClassLoader );
}
-
- return lifecycleListeners;
}
private MavenExecutionResult addExceptionToResult( MavenExecutionResult result, Throwable e )