o Allowed build extensions to contribute lifecycle participants

git-svn-id: https://svn.apache.org/repos/asf/maven/maven-3/trunk@820374 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Benjamin Bentmann 2009-09-30 18:30:51 +00:00
parent 293dfbcb30
commit 138d7bc9c2
2 changed files with 51 additions and 9 deletions

View File

@ -46,6 +46,7 @@ public abstract class AbstractMavenLifecycleParticipant
* activate profiles and perform similar tasks that affect MavenProject
* instance construction.
*/
// TODO: This is too early for build extensions, so maybe just remove it?
public void afterSessionStart( MavenSession session ) throws MavenExecutionException
{
// do nothing

View File

@ -20,7 +20,9 @@ import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
@ -108,7 +110,7 @@ public class DefaultMaven
try
{
for ( AbstractMavenLifecycleParticipant listener : getLifecycleParticipants() )
for ( AbstractMavenLifecycleParticipant listener : getLifecycleParticipants( Collections.<MavenProject> emptyList() ) )
{
listener.afterSessionStart( session );
}
@ -138,10 +140,13 @@ public class DefaultMaven
session.setProjects( projects );
ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();
try
{
for ( AbstractMavenLifecycleParticipant listener : getLifecycleParticipants() )
for ( AbstractMavenLifecycleParticipant listener : getLifecycleParticipants( projects ) )
{
Thread.currentThread().setContextClassLoader( listener.getClass().getClassLoader() );
listener.afterProjectsRead( session );
}
}
@ -149,6 +154,10 @@ public class DefaultMaven
{
return processResult( result, e );
}
finally
{
Thread.currentThread().setContextClassLoader( originalClassLoader );
}
try
{
@ -212,19 +221,51 @@ public class DefaultMaven
return result;
}
private List<AbstractMavenLifecycleParticipant> getLifecycleParticipants()
private Collection<AbstractMavenLifecycleParticipant> getLifecycleParticipants( Collection<MavenProject> projects )
{
// TODO injection of component lists does not work
List<AbstractMavenLifecycleParticipant> lifecycleListeners;
Collection<AbstractMavenLifecycleParticipant> lifecycleListeners =
new LinkedHashSet<AbstractMavenLifecycleParticipant>();
ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();
try
{
lifecycleListeners = container.lookupList( AbstractMavenLifecycleParticipant.class );
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() );
}
Collection<ClassLoader> scannedRealms = new HashSet<ClassLoader>();
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() );
}
}
}
}
catch ( ComponentLookupException e1 )
finally
{
// this is just silly, lookupList should return an empty list!
lifecycleListeners = new ArrayList<AbstractMavenLifecycleParticipant>();
Thread.currentThread().setContextClassLoader( originalClassLoader );
}
return lifecycleListeners;
}