mirror of https://github.com/apache/maven.git
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:
parent
e5644518bd
commit
08825262be
|
@ -17,6 +17,7 @@ package org.apache.maven;
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
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.EventDispatcher;
|
||||||
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;
|
||||||
|
@ -380,6 +382,14 @@ public class DefaultMaven
|
||||||
private void scanProjectsForExtensions( List files, MavenExecutionRequest request,
|
private void scanProjectsForExtensions( List files, MavenExecutionRequest request,
|
||||||
ProfileManager globalProfileManager )
|
ProfileManager globalProfileManager )
|
||||||
throws MavenExecutionException
|
throws MavenExecutionException
|
||||||
|
{
|
||||||
|
// 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
|
||||||
{
|
{
|
||||||
MavenProject superProject;
|
MavenProject superProject;
|
||||||
try
|
try
|
||||||
|
@ -402,6 +412,8 @@ public class DefaultMaven
|
||||||
ModelLineage lineage;
|
ModelLineage lineage;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
getLogger().debug( "Building model-lineage for: " + pom + " to pre-scan for extensions." );
|
||||||
|
|
||||||
lineage = modelLineageBuilder.buildModelLineage( pom, request.getLocalRepository(), originalRemoteRepositories, globalProfileManager, cache );
|
lineage = modelLineageBuilder.buildModelLineage( pom, request.getLocalRepository(), originalRemoteRepositories, globalProfileManager, cache );
|
||||||
}
|
}
|
||||||
catch ( ProjectBuildingException e )
|
catch ( ProjectBuildingException e )
|
||||||
|
@ -429,6 +441,8 @@ public class DefaultMaven
|
||||||
{
|
{
|
||||||
Extension extension = (Extension) extensionIterator.next();
|
Extension extension = (Extension) extensionIterator.next();
|
||||||
|
|
||||||
|
getLogger().debug( "Adding extension: " + ArtifactUtils.versionlessKey( extension.getGroupId(), extension.getArtifactId() ) + " from model: " + model.getId() );
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
extensionManager.addExtension( extension, model, remoteRepositories, request.getLocalRepository() );
|
extensionManager.addExtension( extension, model, remoteRepositories, request.getLocalRepository() );
|
||||||
|
@ -459,6 +473,12 @@ public class DefaultMaven
|
||||||
|
|
||||||
extensionManager.registerWagons();
|
extensionManager.registerWagons();
|
||||||
}
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
activatorAdvice.reset();
|
||||||
|
activatorAdvice.store( buildContextManager );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void logReactorSummaryLine( String name,
|
private void logReactorSummaryLine( String name,
|
||||||
String status )
|
String status )
|
||||||
|
|
|
@ -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();
|
||||||
|
|
||||||
|
@ -61,18 +65,25 @@ public class CustomActivator
|
||||||
{
|
{
|
||||||
ProfileActivator activator = loadProfileActivator( custom );
|
ProfileActivator activator = loadProfileActivator( custom );
|
||||||
|
|
||||||
|
if ( activator != null )
|
||||||
|
{
|
||||||
return activator.canDetermineActivation( profile );
|
return activator.canDetermineActivation( profile );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
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 @@ public class CustomActivator
|
||||||
{
|
{
|
||||||
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 @@ public class CustomActivator
|
||||||
{
|
{
|
||||||
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();
|
||||||
|
|
||||||
|
|
|
@ -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 );
|
||||||
|
}
|
||||||
|
}
|
|
@ -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;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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>
|
||||||
<!--
|
<!--
|
||||||
|
|
|
|
||||||
|
|
Loading…
Reference in New Issue