Adding custom profile activator support, using the <custom/> tag in the <activation/> section of a profile.

git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@499911 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
John Dennis Casey 2007-01-25 19:02:16 +00:00
parent e5644518bd
commit 08825262be
5 changed files with 186 additions and 62 deletions

View File

@ -17,6 +17,7 @@
*/ */
import org.apache.maven.artifact.ArtifactUtils;
import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.resolver.ArtifactNotFoundException; import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
import org.apache.maven.artifact.resolver.ArtifactResolutionException; import org.apache.maven.artifact.resolver.ArtifactResolutionException;
@ -41,6 +42,7 @@
import org.apache.maven.monitor.event.MavenEvents; import org.apache.maven.monitor.event.MavenEvents;
import org.apache.maven.profiles.DefaultProfileManager; import org.apache.maven.profiles.DefaultProfileManager;
import org.apache.maven.profiles.ProfileManager; import org.apache.maven.profiles.ProfileManager;
import org.apache.maven.profiles.activation.CustomActivatorAdvice;
import org.apache.maven.profiles.activation.ProfileActivationException; import org.apache.maven.profiles.activation.ProfileActivationException;
import org.apache.maven.project.DuplicateProjectException; import org.apache.maven.project.DuplicateProjectException;
import org.apache.maven.project.MavenProject; import org.apache.maven.project.MavenProject;
@ -381,83 +383,101 @@ private void scanProjectsForExtensions( List files, MavenExecutionRequest reques
ProfileManager globalProfileManager ) ProfileManager globalProfileManager )
throws MavenExecutionException throws MavenExecutionException
{ {
MavenProject superProject; // setup the CustomActivatorAdvice to fail quietly while we discover extensions...then, we'll
// reset it.
CustomActivatorAdvice activatorAdvice = CustomActivatorAdvice.getCustomActivatorAdvice( buildContextManager );
activatorAdvice.setFailQuietly( true );
activatorAdvice.store( buildContextManager );
try try
{ {
superProject = projectBuilder.buildStandaloneSuperProject( request.getLocalRepository(), MavenProject superProject;
globalProfileManager );
}
catch ( ProjectBuildingException e )
{
throw new MavenExecutionException( "Error building super-POM for retrieving the default remote repository list: " + e.getMessage(), e );
}
List originalRemoteRepositories = superProject.getRemoteArtifactRepositories();
Map cache = new HashMap();
for ( Iterator it = files.iterator(); it.hasNext(); )
{
File pom = (File) it.next();
ModelLineage lineage;
try try
{ {
lineage = modelLineageBuilder.buildModelLineage( pom, request.getLocalRepository(), originalRemoteRepositories, globalProfileManager, cache ); superProject = projectBuilder.buildStandaloneSuperProject( request.getLocalRepository(),
globalProfileManager );
} }
catch ( ProjectBuildingException e ) catch ( ProjectBuildingException e )
{ {
throw new MavenExecutionException( "Error building model lineage in order to pre-scan for extensions: " + e.getMessage(), e ); throw new MavenExecutionException( "Error building super-POM for retrieving the default remote repository list: " + e.getMessage(), e );
} }
for ( ModelLineageIterator lineageIterator = lineage.lineageIterator(); lineageIterator.hasNext(); ) List originalRemoteRepositories = superProject.getRemoteArtifactRepositories();
Map cache = new HashMap();
for ( Iterator it = files.iterator(); it.hasNext(); )
{ {
Model model = (Model) lineageIterator.next(); File pom = (File) it.next();
Build build = model.getBuild(); ModelLineage lineage;
try
if ( build != null )
{ {
List extensions = build.getExtensions(); getLogger().debug( "Building model-lineage for: " + pom + " to pre-scan for extensions." );
if ( extensions != null && !extensions.isEmpty() ) lineage = modelLineageBuilder.buildModelLineage( pom, request.getLocalRepository(), originalRemoteRepositories, globalProfileManager, cache );
}
catch ( ProjectBuildingException e )
{
throw new MavenExecutionException( "Error building model lineage in order to pre-scan for extensions: " + e.getMessage(), e );
}
for ( ModelLineageIterator lineageIterator = lineage.lineageIterator(); lineageIterator.hasNext(); )
{
Model model = (Model) lineageIterator.next();
Build build = model.getBuild();
if ( build != null )
{ {
List remoteRepositories = lineageIterator.getArtifactRepositories(); List extensions = build.getExtensions();
// thankfully, we don't have to deal with dependencyManagement here, yet. if ( extensions != null && !extensions.isEmpty() )
// TODO Revisit if/when extensions are made to use the info in dependencyManagement
for ( Iterator extensionIterator = extensions.iterator(); extensionIterator.hasNext(); )
{ {
Extension extension = (Extension) extensionIterator.next(); List remoteRepositories = lineageIterator.getArtifactRepositories();
try // thankfully, we don't have to deal with dependencyManagement here, yet.
// TODO Revisit if/when extensions are made to use the info in dependencyManagement
for ( Iterator extensionIterator = extensions.iterator(); extensionIterator.hasNext(); )
{ {
extensionManager.addExtension( extension, model, remoteRepositories, request.getLocalRepository() ); Extension extension = (Extension) extensionIterator.next();
}
catch ( ArtifactResolutionException e ) getLogger().debug( "Adding extension: " + ArtifactUtils.versionlessKey( extension.getGroupId(), extension.getArtifactId() ) + " from model: " + model.getId() );
{
throw new MavenExecutionException( "Cannot resolve pre-scanned extension artifact: " try
+ extension.getGroupId() + ":" + extension.getArtifactId() + ": " + e.getMessage(), {
e ); extensionManager.addExtension( extension, model, remoteRepositories, request.getLocalRepository() );
} }
catch ( ArtifactNotFoundException e ) catch ( ArtifactResolutionException e )
{ {
throw new MavenExecutionException( "Cannot find pre-scanned extension artifact: " throw new MavenExecutionException( "Cannot resolve pre-scanned extension artifact: "
+ extension.getGroupId() + ":" + extension.getArtifactId() + ": " + e.getMessage(), + extension.getGroupId() + ":" + extension.getArtifactId() + ": " + e.getMessage(),
e ); e );
} }
catch ( PlexusContainerException e ) catch ( ArtifactNotFoundException e )
{ {
throw new MavenExecutionException( "Failed to add pre-scanned extension: " throw new MavenExecutionException( "Cannot find pre-scanned extension artifact: "
+ extension.getGroupId() + ":" + extension.getArtifactId() + ": " + e.getMessage(), + extension.getGroupId() + ":" + extension.getArtifactId() + ": " + e.getMessage(),
e ); e );
}
catch ( PlexusContainerException e )
{
throw new MavenExecutionException( "Failed to add pre-scanned extension: "
+ extension.getGroupId() + ":" + extension.getArtifactId() + ": " + e.getMessage(),
e );
}
} }
} }
} }
} }
} }
extensionManager.registerWagons();
}
finally
{
activatorAdvice.reset();
activatorAdvice.store( buildContextManager );
} }
extensionManager.registerWagons();
} }
private void logReactorSummaryLine( String name, private void logReactorSummaryLine( String name,

View File

@ -1,5 +1,6 @@
package org.apache.maven.profiles.activation; package org.apache.maven.profiles.activation;
import org.apache.maven.context.BuildContextManager;
import org.apache.maven.model.Activation; import org.apache.maven.model.Activation;
import org.apache.maven.model.ActivationCustom; import org.apache.maven.model.ActivationCustom;
import org.apache.maven.model.Profile; import org.apache.maven.model.Profile;
@ -49,7 +50,10 @@ public class CustomActivator
private Logger logger; private Logger logger;
private BuildContextManager buildContextManager;
public boolean canDetermineActivation( Profile profile ) public boolean canDetermineActivation( Profile profile )
throws ProfileActivationException
{ {
Activation activation = profile.getActivation(); Activation activation = profile.getActivation();
@ -60,8 +64,11 @@ public boolean canDetermineActivation( Profile profile )
if ( custom != null ) if ( custom != null )
{ {
ProfileActivator activator = loadProfileActivator( custom ); ProfileActivator activator = loadProfileActivator( custom );
return activator.canDetermineActivation( profile ); if ( activator != null )
{
return activator.canDetermineActivation( profile );
}
} }
} }
@ -69,10 +76,14 @@ public boolean canDetermineActivation( Profile profile )
} }
private ProfileActivator loadProfileActivator( ActivationCustom custom ) private ProfileActivator loadProfileActivator( ActivationCustom custom )
throws ProfileActivationException
{ {
CustomActivatorAdvice advice = CustomActivatorAdvice.getCustomActivatorAdvice( buildContextManager );
String type = custom.getType(); String type = custom.getType();
ProfileActivator activator; ProfileActivator activator = null;
try try
{ {
activator = (ProfileActivator) container.lookup( ProfileActivator.ROLE, type ); activator = (ProfileActivator) container.lookup( ProfileActivator.ROLE, type );
@ -81,8 +92,11 @@ private ProfileActivator loadProfileActivator( ActivationCustom custom )
{ {
getLogger().debug( "Failed to lookup ProfileActivator \'" + type + "\'", e ); getLogger().debug( "Failed to lookup ProfileActivator \'" + type + "\'", e );
throw new IllegalArgumentException( "Cannot find ProfileActivator with role-hint: " + type if ( !advice.failQuietly() )
+ ". \nPerhaps you're missing a build extension? \nSee debug output for more information." ); {
throw new ProfileActivationException( "Cannot find ProfileActivator with role-hint: " + type
+ ". \nPerhaps you're missing a build extension?", e );
}
} }
PlexusConfiguration configuration = new XmlPlexusConfiguration( (Xpp3Dom) custom.getConfiguration() ); PlexusConfiguration configuration = new XmlPlexusConfiguration( (Xpp3Dom) custom.getConfiguration() );
@ -97,14 +111,18 @@ private ProfileActivator loadProfileActivator( ActivationCustom custom )
{ {
getLogger().debug( "Failed to configure ProfileActivator \'" + type + "\'", e ); getLogger().debug( "Failed to configure ProfileActivator \'" + type + "\'", e );
throw new IllegalArgumentException( "Failed to configure ProfileActivator with role-hint: " + type if ( !advice.failQuietly() )
+ ". Turn on debug mode for more information." ); {
throw new ProfileActivationException( "Failed to configure ProfileActivator with role-hint: " + type
+ ".", e );
}
} }
return activator; return activator;
} }
public boolean isActive( Profile profile ) public boolean isActive( Profile profile )
throws ProfileActivationException
{ {
ActivationCustom custom = profile.getActivation().getCustom(); ActivationCustom custom = profile.getActivation().getCustom();

View File

@ -0,0 +1,80 @@
package org.apache.maven.profiles.activation;
import org.apache.maven.context.BuildContext;
import org.apache.maven.context.BuildContextManager;
import org.apache.maven.context.ManagedBuildData;
/**
* Advice for the custom profile activator, which tells how to handle cases where custom activators
* cannot be found or configured. This is used to suppress missing activators when pre-scanning for
* build extensions (which may contain the custom activator).
*
* @author jdcasey
*/
public class CustomActivatorAdvice
implements ManagedBuildData
{
public static final String BUILD_CONTEXT_KEY = CustomActivatorAdvice.class.getName();
private static final boolean DEFAULT_FAIL_QUIETLY = true;
/**
* If set to false, this tells the CustomProfileActivator to fail quietly when the specified
* custom profile activator cannot be found or configured correctly. Default behavior is to throw
* a new ProfileActivationException.
*/
private boolean failQuietly = DEFAULT_FAIL_QUIETLY;
public void reset()
{
failQuietly = DEFAULT_FAIL_QUIETLY;
}
public void setFailQuietly( boolean ignoreMissingActivator )
{
this.failQuietly = ignoreMissingActivator;
}
public boolean failQuietly()
{
return failQuietly;
}
public String getStorageKey()
{
return BUILD_CONTEXT_KEY;
}
/**
* Read the custom profile activator advice from the build context. If missing or the build
* context has not been initialized, create a new instance of the advice and return that.
*/
public static CustomActivatorAdvice getCustomActivatorAdvice( BuildContextManager buildContextManager )
{
BuildContext buildContext = buildContextManager.readBuildContext( false );
CustomActivatorAdvice advice = null;
if ( buildContext != null )
{
advice = (CustomActivatorAdvice) buildContext.get( BUILD_CONTEXT_KEY );
}
if ( advice == null )
{
advice = new CustomActivatorAdvice();
}
return advice;
}
public void store( BuildContextManager buildContextManager )
{
BuildContext buildContext = buildContextManager.readBuildContext( true );
buildContext.put( this );
buildContextManager.storeBuildContext( buildContext );
}
}

View File

@ -22,8 +22,8 @@ public interface ProfileActivator
{ {
static final String ROLE = ProfileActivator.class.getName(); static final String ROLE = ProfileActivator.class.getName();
boolean canDetermineActivation( Profile profile ); boolean canDetermineActivation( Profile profile ) throws ProfileActivationException;
boolean isActive( Profile profile ); boolean isActive( Profile profile ) throws ProfileActivationException;
} }

View File

@ -205,6 +205,12 @@
<role>org.apache.maven.profiles.activation.ProfileActivator</role> <role>org.apache.maven.profiles.activation.ProfileActivator</role>
<role-hint>custom</role-hint> <role-hint>custom</role-hint>
<implementation>org.apache.maven.profiles.activation.CustomActivator</implementation> <implementation>org.apache.maven.profiles.activation.CustomActivator</implementation>
<requirements>
<requirement>
<role>org.apache.maven.context.BuildContextManager</role>
<role-hint>default</role-hint>
</requirement>
</requirements>
</component> </component>
<!-- <!--
| |