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.resolver.ArtifactNotFoundException;
|
||||
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.profiles.DefaultProfileManager;
|
||||
import org.apache.maven.profiles.ProfileManager;
|
||||
import org.apache.maven.profiles.activation.CustomActivatorAdvice;
|
||||
import org.apache.maven.profiles.activation.ProfileActivationException;
|
||||
import org.apache.maven.project.DuplicateProjectException;
|
||||
import org.apache.maven.project.MavenProject;
|
||||
|
@ -381,83 +383,101 @@ public class DefaultMaven
|
|||
ProfileManager globalProfileManager )
|
||||
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
|
||||
{
|
||||
superProject = projectBuilder.buildStandaloneSuperProject( request.getLocalRepository(),
|
||||
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;
|
||||
MavenProject superProject;
|
||||
try
|
||||
{
|
||||
lineage = modelLineageBuilder.buildModelLineage( pom, request.getLocalRepository(), originalRemoteRepositories, globalProfileManager, cache );
|
||||
superProject = projectBuilder.buildStandaloneSuperProject( request.getLocalRepository(),
|
||||
globalProfileManager );
|
||||
}
|
||||
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();
|
||||
|
||||
if ( build != null )
|
||||
ModelLineage lineage;
|
||||
try
|
||||
{
|
||||
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.
|
||||
// TODO Revisit if/when extensions are made to use the info in dependencyManagement
|
||||
for ( Iterator extensionIterator = extensions.iterator(); extensionIterator.hasNext(); )
|
||||
if ( extensions != null && !extensions.isEmpty() )
|
||||
{
|
||||
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() );
|
||||
}
|
||||
catch ( ArtifactResolutionException e )
|
||||
{
|
||||
throw new MavenExecutionException( "Cannot resolve pre-scanned extension artifact: "
|
||||
+ extension.getGroupId() + ":" + extension.getArtifactId() + ": " + e.getMessage(),
|
||||
e );
|
||||
}
|
||||
catch ( ArtifactNotFoundException e )
|
||||
{
|
||||
throw new MavenExecutionException( "Cannot find pre-scanned extension artifact: "
|
||||
+ extension.getGroupId() + ":" + extension.getArtifactId() + ": " + e.getMessage(),
|
||||
e );
|
||||
}
|
||||
catch ( PlexusContainerException e )
|
||||
{
|
||||
throw new MavenExecutionException( "Failed to add pre-scanned extension: "
|
||||
+ extension.getGroupId() + ":" + extension.getArtifactId() + ": " + e.getMessage(),
|
||||
e );
|
||||
Extension extension = (Extension) extensionIterator.next();
|
||||
|
||||
getLogger().debug( "Adding extension: " + ArtifactUtils.versionlessKey( extension.getGroupId(), extension.getArtifactId() ) + " from model: " + model.getId() );
|
||||
|
||||
try
|
||||
{
|
||||
extensionManager.addExtension( extension, model, remoteRepositories, request.getLocalRepository() );
|
||||
}
|
||||
catch ( ArtifactResolutionException e )
|
||||
{
|
||||
throw new MavenExecutionException( "Cannot resolve pre-scanned extension artifact: "
|
||||
+ extension.getGroupId() + ":" + extension.getArtifactId() + ": " + e.getMessage(),
|
||||
e );
|
||||
}
|
||||
catch ( ArtifactNotFoundException e )
|
||||
{
|
||||
throw new MavenExecutionException( "Cannot find pre-scanned extension artifact: "
|
||||
+ extension.getGroupId() + ":" + extension.getArtifactId() + ": " + e.getMessage(),
|
||||
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,
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package org.apache.maven.profiles.activation;
|
||||
|
||||
import org.apache.maven.context.BuildContextManager;
|
||||
import org.apache.maven.model.Activation;
|
||||
import org.apache.maven.model.ActivationCustom;
|
||||
import org.apache.maven.model.Profile;
|
||||
|
@ -49,7 +50,10 @@ public class CustomActivator
|
|||
|
||||
private Logger logger;
|
||||
|
||||
private BuildContextManager buildContextManager;
|
||||
|
||||
public boolean canDetermineActivation( Profile profile )
|
||||
throws ProfileActivationException
|
||||
{
|
||||
Activation activation = profile.getActivation();
|
||||
|
||||
|
@ -60,8 +64,11 @@ public class CustomActivator
|
|||
if ( custom != null )
|
||||
{
|
||||
ProfileActivator activator = loadProfileActivator( custom );
|
||||
|
||||
return activator.canDetermineActivation( profile );
|
||||
|
||||
if ( activator != null )
|
||||
{
|
||||
return activator.canDetermineActivation( profile );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -69,10 +76,14 @@ public class CustomActivator
|
|||
}
|
||||
|
||||
private ProfileActivator loadProfileActivator( ActivationCustom custom )
|
||||
throws ProfileActivationException
|
||||
{
|
||||
CustomActivatorAdvice advice = CustomActivatorAdvice.getCustomActivatorAdvice( buildContextManager );
|
||||
|
||||
String type = custom.getType();
|
||||
|
||||
ProfileActivator activator;
|
||||
ProfileActivator activator = null;
|
||||
|
||||
try
|
||||
{
|
||||
activator = (ProfileActivator) container.lookup( ProfileActivator.ROLE, type );
|
||||
|
@ -81,8 +92,11 @@ public class CustomActivator
|
|||
{
|
||||
getLogger().debug( "Failed to lookup ProfileActivator \'" + type + "\'", e );
|
||||
|
||||
throw new IllegalArgumentException( "Cannot find ProfileActivator with role-hint: " + type
|
||||
+ ". \nPerhaps you're missing a build extension? \nSee debug output for more information." );
|
||||
if ( !advice.failQuietly() )
|
||||
{
|
||||
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() );
|
||||
|
@ -97,14 +111,18 @@ public class CustomActivator
|
|||
{
|
||||
getLogger().debug( "Failed to configure ProfileActivator \'" + type + "\'", e );
|
||||
|
||||
throw new IllegalArgumentException( "Failed to configure ProfileActivator with role-hint: " + type
|
||||
+ ". Turn on debug mode for more information." );
|
||||
if ( !advice.failQuietly() )
|
||||
{
|
||||
throw new ProfileActivationException( "Failed to configure ProfileActivator with role-hint: " + type
|
||||
+ ".", e );
|
||||
}
|
||||
}
|
||||
|
||||
return activator;
|
||||
}
|
||||
|
||||
public boolean isActive( Profile profile )
|
||||
throws ProfileActivationException
|
||||
{
|
||||
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();
|
||||
|
||||
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-hint>custom</role-hint>
|
||||
<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>
|
||||
<!--
|
||||
|
|
||||
|
|
Loading…
Reference in New Issue