MNG-2364 make sure the system properties are passed from the execution/embed requests, to make the on-property profile activation work in embedded environment

git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@415000 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Milos Kleint 2006-06-17 06:46:58 +00:00
parent 02d6cd1968
commit d7a1ba698d
9 changed files with 111 additions and 16 deletions

View File

@ -306,7 +306,7 @@ public class DefaultMaven
throw new MavenExecutionException( "Unable to configure Maven for execution", e );
}
ProfileManager globalProfileManager = new DefaultProfileManager( container );
ProfileManager globalProfileManager = new DefaultProfileManager( container, request.getProperties() );
globalProfileManager.loadSettingsProfiles( request.getSettings() );
@ -372,7 +372,8 @@ public class DefaultMaven
MavenProject superProject;
try
{
superProject = projectBuilder.buildStandaloneSuperProject( request.getLocalRepository() );
superProject = projectBuilder.buildStandaloneSuperProject( request.getLocalRepository(),
new DefaultProfileManager( container, request.getProperties()) );
}
catch ( ProjectBuildingException e )

View File

@ -19,6 +19,7 @@ import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import org.apache.maven.settings.Settings;
@ -43,6 +44,8 @@ public class DefaultMavenEmbedRequest
private ContainerCustomizer customizer;
private Properties systemProperties;
/**
* Creates a new instance of DefaultMavenEmbedRequest
*/
@ -125,4 +128,15 @@ public class DefaultMavenEmbedRequest
return customizer;
}
public MavenEmbedRequest setSystemProperties(Properties properties)
{
systemProperties = properties;
return this;
}
public Properties getSystemProperties()
{
return systemProperties != null ? systemProperties : System.getProperties();
}
}

View File

@ -18,6 +18,7 @@ package org.apache.maven.embedder;
import java.io.File;
import java.net.URL;
import java.util.List;
import java.util.Properties;
import org.apache.maven.settings.Settings;
/**
@ -60,6 +61,11 @@ public interface MavenEmbedRequest {
*/
MavenEmbedRequest setConfigurationCustomizer(ContainerCustomizer customizer);
/**
* set the system properties to be used during the lifecycle of the embedder. Excluding the time when executing the project, then the properties from MavenExecutionRequestare used.
*/
MavenEmbedRequest setSystemProperties( Properties properties );
List getActiveProfiles();
List getInactiveProfiles();
@ -70,4 +76,7 @@ public interface MavenEmbedRequest {
ContainerCustomizer getContainerCustomizer();
Properties getSystemProperties();
}

View File

@ -602,7 +602,7 @@ public class MavenEmbedder
pluginDescriptorBuilder = new PluginDescriptorBuilder();
profileManager = new DefaultProfileManager( embedder.getContainer() );
profileManager = new DefaultProfileManager( embedder.getContainer(), req.getSystemProperties() );
profileManager.explicitlyActivate(req.getActiveProfiles());

View File

@ -1,5 +1,6 @@
package org.apache.maven.profiles;
import java.util.Properties;
import org.apache.maven.model.Activation;
import org.apache.maven.model.Profile;
import org.apache.maven.profiles.activation.ProfileActivationException;
@ -45,18 +46,43 @@ public class DefaultProfileManager
private List defaultIds = new ArrayList();
private Map profilesById = new LinkedHashMap();
// default fallback..
private Properties systemProperties = System.getProperties();
/**
* @deprecated without passing in the system properties, the SystemPropertiesProfileActivator will not work correctly
* in embedded envirnments.
*/
public DefaultProfileManager( PlexusContainer container )
{
this( container, null );
this( container, (Settings)null);
}
/**
* the properties passed to the profile manager are the props that
* are passed to maven, possibly containing profile activator properties
*
*/
public DefaultProfileManager( PlexusContainer container, Properties props )
{
this( container, (Settings)null );
if (props != null) {
systemProperties = props;
}
}
public DefaultProfileManager( PlexusContainer container, Settings settings )
private DefaultProfileManager( PlexusContainer container, Settings settings )
{
this.container = container;
loadSettingsProfiles( settings );
}
public Properties getSystemProperties() {
return systemProperties;
}
public Map getProfilesById()
{
@ -202,6 +228,7 @@ public class DefaultProfileManager
throws ProfileActivationException
{
List activators = null;
container.addContextValue("SystemProperties", systemProperties);
try
{
activators = container.lookupList( ProfileActivator.ROLE );
@ -224,13 +251,17 @@ public class DefaultProfileManager
}
finally
{
try
container.getContext().put("SystemProperties", null);
if ( activators != null )
{
container.releaseAll( activators );
}
catch ( ComponentLifecycleException e )
{
container.getLogger().debug( "Error releasing profile activators - ignoring.", e );
try
{
container.releaseAll( activators );
}
catch ( ComponentLifecycleException e )
{
container.getLogger().debug( "Error releasing profile activators - ignoring.", e );
}
}
}
}

View File

@ -1,5 +1,6 @@
package org.apache.maven.profiles;
import java.util.Properties;
import org.apache.maven.model.Profile;
import org.apache.maven.profiles.activation.ProfileActivationException;
import org.apache.maven.settings.Settings;
@ -36,4 +37,5 @@ public interface ProfileManager
List getIdsActivatedByDefault();
void loadSettingsProfiles( Settings settings );
}

View File

@ -1,8 +1,12 @@
package org.apache.maven.profiles.activation;
import java.util.Properties;
import org.apache.maven.model.Activation;
import org.apache.maven.model.ActivationProperty;
import org.apache.maven.model.Profile;
import org.codehaus.plexus.context.Context;
import org.codehaus.plexus.context.ContextException;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
import org.codehaus.plexus.util.StringUtils;
/*
@ -22,8 +26,15 @@ import org.codehaus.plexus.util.StringUtils;
*/
public class SystemPropertyProfileActivator
extends DetectedProfileActivator
extends DetectedProfileActivator implements Contextualizable
{
private Properties properties;
public void contextualize(Context context) throws ContextException
{
properties = (Properties)context.get("SystemProperties");
}
protected boolean canDetectActivation( Profile profile )
{
return profile.getActivation() != null && profile.getActivation().getProperty() != null;
@ -46,7 +57,7 @@ public class SystemPropertyProfileActivator
name = name.substring( 1 );
}
String sysValue = System.getProperty( name );
String sysValue = properties.getProperty( name );
String propValue = property.getValue();
if ( StringUtils.isNotEmpty( propValue ) )

View File

@ -240,6 +240,14 @@ public class DefaultMavenProjectBuilder
// what is using this externally? jvz.
public MavenProject buildStandaloneSuperProject( ArtifactRepository localRepository )
throws ProjectBuildingException
{
//TODO mkleint - use the (Container, Properties) constructor to make system properties embeddable
ProfileManager profileManager = new DefaultProfileManager( container );
return buildStandaloneSuperProject( localRepository, profileManager );
}
public MavenProject buildStandaloneSuperProject( ArtifactRepository localRepository, ProfileManager profileManager )
throws ProjectBuildingException
{
Model superModel = getSuperModel();
@ -249,7 +257,6 @@ public class DefaultMavenProjectBuilder
superModel.setVersion( STANDALONE_SUPERPOM_VERSION );
ProfileManager profileManager = new DefaultProfileManager( container );
List activeProfiles;
@ -606,7 +613,15 @@ public class DefaultMavenProjectBuilder
Model superModel = getSuperModel();
ProfileManager superProjectProfileManager = new DefaultProfileManager( container );
//TODO mkleint - use the (Container, Properties) constructor to make system properties embeddable
// shall the ProfileManager intefrace expose the properties?
ProfileManager superProjectProfileManager;
if (externalProfileManager instanceof DefaultProfileManager) {
superProjectProfileManager = new DefaultProfileManager( container, ((DefaultProfileManager) externalProfileManager).getSystemProperties() );
} else {
superProjectProfileManager = new DefaultProfileManager( container );
}
List activeProfiles;
@ -965,7 +980,13 @@ public class DefaultMavenProjectBuilder
}
}
ProfileManager profileManager = new DefaultProfileManager( container );
//TODO mkleint - use the (Container, Properties constructor to make system properties embeddable
ProfileManager profileManager;
if (externalProfileManager != null && externalProfileManager instanceof DefaultProfileManager ) {
profileManager = new DefaultProfileManager( container, ((DefaultProfileManager)externalProfileManager).getSystemProperties() );
} else {
profileManager = new DefaultProfileManager( container );
}
if ( externalProfileManager != null )
{

View File

@ -90,4 +90,10 @@ public interface MavenProjectBuilder
MavenProject buildStandaloneSuperProject( ArtifactRepository localRepository )
throws ProjectBuildingException;
/**
* need to pass a profilemanager with correct context (eg. with execution properties)
*/
MavenProject buildStandaloneSuperProject( ArtifactRepository localRepository, ProfileManager profileManager )
throws ProjectBuildingException;
}