Replaced old profile code with new.

git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@747993 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Britton Isbell 2009-02-26 03:14:14 +00:00
parent fac6e8c96d
commit 55faf1270e
6 changed files with 123 additions and 53 deletions

View File

@ -227,6 +227,7 @@ public class PomTransformer
ModelContainerAction action = pluginContainer.containerAction( managementContainer ); ModelContainerAction action = pluginContainer.containerAction( managementContainer );
//Join Execution Containers
if ( action.equals( ModelContainerAction.JOIN ) || action.equals( ModelContainerAction.DELETE ) ) if ( action.equals( ModelContainerAction.JOIN ) || action.equals( ModelContainerAction.DELETE ) )
{ {
ModelDataSource pluginDatasource = new DefaultModelDataSource( pluginContainer.getProperties(), PomTransformer.MODEL_CONTAINER_FACTORIES ); ModelDataSource pluginDatasource = new DefaultModelDataSource( pluginContainer.getProperties(), PomTransformer.MODEL_CONTAINER_FACTORIES );

View File

@ -45,7 +45,16 @@ public class OperatingSystemMatcher implements ActiveProfileMatcher {
|| (key.equals("${os.family}") && property.getUri().equals(ProjectUri.Profiles.Profile.Activation.Os.family)) || (key.equals("${os.family}") && property.getUri().equals(ProjectUri.Profiles.Profile.Activation.Os.family))
|| (key.equals("${os.name}") && property.getUri().equals(ProjectUri.Profiles.Profile.Activation.Os.name)) ) || (key.equals("${os.name}") && property.getUri().equals(ProjectUri.Profiles.Profile.Activation.Os.name)) )
{ {
return interpolatorProperty.getValue().equals(property.getResolvedValue());
if(property.getResolvedValue().startsWith("!"))
{
return !interpolatorProperty.getValue().equals(property.getResolvedValue());
}
else
{
return interpolatorProperty.getValue().equals(property.getResolvedValue());
}
} }
} }
return true; return true;

View File

@ -25,10 +25,16 @@ public class PropertyMatcher implements ActiveProfileMatcher {
} }
} }
if(name == null || value == null) { if(name == null )
{
return false; return false;
} }
if(value == null)
{
return !name.startsWith("!");
}
for(InterpolatorProperty ip : properties) { for(InterpolatorProperty ip : properties) {
if(ip.getKey().equals("${" + name + "}")) { if(ip.getKey().equals("${" + name + "}")) {
return ip.getValue().equals(value); return ip.getValue().equals(value);

View File

@ -23,21 +23,29 @@ import org.apache.maven.model.Activation;
import org.apache.maven.model.Model; import org.apache.maven.model.Model;
import org.apache.maven.model.Profile; import org.apache.maven.model.Profile;
import org.apache.maven.model.Parent; import org.apache.maven.model.Parent;
import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
import org.apache.maven.profiles.activation.DefaultProfileActivationContext; import org.apache.maven.profiles.activation.DefaultProfileActivationContext;
import org.apache.maven.profiles.activation.ProfileActivationContext; import org.apache.maven.profiles.activation.ProfileActivationContext;
import org.apache.maven.profiles.activation.ProfileActivationException; import org.apache.maven.profiles.activation.ProfileActivationException;
import org.apache.maven.profiles.activation.ProfileActivator; import org.apache.maven.shared.model.ModelContainer;
import org.apache.maven.shared.model.ModelProperty;
import org.apache.maven.shared.model.ModelMarshaller;
import org.apache.maven.shared.model.InterpolatorProperty;
import org.apache.maven.project.builder.factories.IdModelContainerFactory;
import org.apache.maven.project.builder.ProjectUri;
import org.apache.maven.project.builder.PomTransformer;
import org.apache.maven.project.builder.PomInterpolatorTag;
import org.apache.maven.project.builder.profile.*;
import org.codehaus.plexus.PlexusContainer; import org.codehaus.plexus.PlexusContainer;
import org.codehaus.plexus.MutablePlexusContainer; import org.codehaus.plexus.MutablePlexusContainer;
import org.codehaus.plexus.component.repository.exception.ComponentLifecycleException; import org.codehaus.plexus.util.xml.pull.XmlSerializer;
import org.codehaus.plexus.component.repository.exception.ComponentLookupException; import org.codehaus.plexus.util.xml.pull.MXSerializer;
import java.util.ArrayList; import java.util.*;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.io.*;
import java.lang.reflect.Method;
public class DefaultProfileManager public class DefaultProfileManager
implements ProfileManager implements ProfileManager
@ -258,49 +266,79 @@ public class DefaultProfileManager
} }
} }
private static List<ActiveProfileMatcher> matchers = Arrays.asList(new FileMatcher(),
new JdkMatcher(), new OperatingSystemMatcher(), new PropertyMatcher());
private boolean isActive( Profile profile, ProfileActivationContext context ) private boolean isActive( Profile profile, ProfileActivationContext context )
throws ProfileActivationException throws ProfileActivationException
{ {
List<ProfileActivator> activators = null; //TODO: Using reflection now. Need to replace with custom mapper
StringWriter writer = new StringWriter();
XmlSerializer serializer = new MXSerializer();
serializer.setProperty( "http://xmlpull.org/v1/doc/properties.html#serializer-indentation", " " );
serializer.setProperty( "http://xmlpull.org/v1/doc/properties.html#serializer-line-separator", "\n" );
try try
{ {
activators = container.lookupList( ProfileActivator.class ); serializer.setOutput( writer );
serializer.startDocument("UTF-8", null );
for ( ProfileActivator activator : activators ) } catch (IOException e) {
{
if ( activator.canDetermineActivation( profile, context ) )
{
if ( activator.isActive( profile, context ) )
{
container.getLogger().debug(
"Profile: " + profile.getId() + " is active. (source: " + profile.getSource() + ")" );
return true;
}
}
}
return false;
} }
catch ( ComponentLookupException e )
try {
MavenXpp3Writer w = new MavenXpp3Writer();
Class c = Class.forName("org.apache.maven.model.io.xpp3.MavenXpp3Writer");
Class partypes[] = new Class[3];
partypes[0] = Profile.class;
partypes[1] = String.class;
partypes[2] = XmlSerializer.class;
Method meth = c.getDeclaredMethod(
"writeProfile", partypes);
meth.setAccessible(true);
Object arglist[] = new Object[3];
arglist[0] = profile;
arglist[1] = "profile";
arglist[2] = serializer;
meth.invoke(w, arglist);
serializer.endDocument();
} catch (Exception e)
{ {
throw new ProfileActivationException( "Cannot retrieve list of profile activators.", e ); throw new ProfileActivationException(e.getMessage(), e);
} }
finally
List<InterpolatorProperty> interpolatorProperties = new ArrayList<InterpolatorProperty>();
interpolatorProperties.addAll(InterpolatorProperty.toInterpolatorProperties(
context.getExecutionProperties(),
PomInterpolatorTag.EXECUTION_PROPERTIES.name()));
List<ModelProperty> p;
try
{
p = ModelMarshaller.marshallXmlToModelProperties(new ByteArrayInputStream(writer.getBuffer().toString().getBytes()),
ProjectUri.Profiles.Profile.xUri, PomTransformer.URIS);
} catch (IOException e) {
throw new ProfileActivationException(e.getMessage());
}
//Serializer adds in extra node, strip it out
List<ModelProperty> p2 = new ArrayList<ModelProperty>();
for(ModelProperty mp : p)
{ {
container.getContext().put( "SystemProperties", null ); p2.add(new ModelProperty(mp.getUri().replaceFirst("profile/", ""), mp.getResolvedValue()));
if ( activators != null ) }
ModelContainer mc = new IdModelContainerFactory(ProjectUri.Profiles.Profile.xUri).create(p2);
for(ActiveProfileMatcher matcher : matchers)
{
if(matcher.isMatch(mc, interpolatorProperties))
{ {
try return true;
{
container.releaseAll( activators );
}
catch ( ComponentLifecycleException e )
{
container.getLogger().debug( "Error releasing profile activators - ignoring.", e );
}
} }
} }
return false;
} }
/* (non-Javadoc) /* (non-Javadoc)
@ -341,18 +379,6 @@ public class DefaultProfileManager
return profileActivationContext.getActiveByDefaultProfileIds(); return profileActivationContext.getActiveByDefaultProfileIds();
} }
private static String getVersion( Model model )
{
Parent parent = model.getParent();
String version = model.getVersion();
if ( ( parent != null ) && ( version == null ) )
{
version = parent.getVersion();
}
return version;
}
public static String getGroupId( Model model ) public static String getGroupId( Model model )
{ {

View File

@ -80,6 +80,19 @@ public class PomConstructionTest
}; };
} }
/**
* Will throw exception if url is empty. MNG-4050
*
* @throws Exception
*/
/*
public void testEmptyUrl()
throws Exception
{
buildPomFromMavenProject( "empty-distMng-repo-url", null );
}
*/
/** /**
* Will throw exception if doesn't find parent(s) in build * Will throw exception if doesn't find parent(s) in build
* *

View File

@ -0,0 +1,15 @@
<project >
<modelVersion>4.0.0</modelVersion>
<groupId>org.apache.commons</groupId>
<artifactId>commons-parent</artifactId>
<packaging>pom</packaging>
<version>11</version>
<distributionManagement>
<repository>
<id>dummy</id>
<name>Dummy to avoid accidental deploys</name>
<url />
</repository>
</distributionManagement>
</project>