mirror of https://github.com/apache/maven.git
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:
parent
fac6e8c96d
commit
55faf1270e
|
@ -227,6 +227,7 @@ public class PomTransformer
|
|||
|
||||
ModelContainerAction action = pluginContainer.containerAction( managementContainer );
|
||||
|
||||
//Join Execution Containers
|
||||
if ( action.equals( ModelContainerAction.JOIN ) || action.equals( ModelContainerAction.DELETE ) )
|
||||
{
|
||||
ModelDataSource pluginDatasource = new DefaultModelDataSource( pluginContainer.getProperties(), PomTransformer.MODEL_CONTAINER_FACTORIES );
|
||||
|
|
|
@ -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.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;
|
||||
|
|
|
@ -25,10 +25,16 @@ public class PropertyMatcher implements ActiveProfileMatcher {
|
|||
}
|
||||
}
|
||||
|
||||
if(name == null || value == null) {
|
||||
if(name == null )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if(value == null)
|
||||
{
|
||||
return !name.startsWith("!");
|
||||
}
|
||||
|
||||
for(InterpolatorProperty ip : properties) {
|
||||
if(ip.getKey().equals("${" + name + "}")) {
|
||||
return ip.getValue().equals(value);
|
||||
|
|
|
@ -23,21 +23,29 @@ import org.apache.maven.model.Activation;
|
|||
import org.apache.maven.model.Model;
|
||||
import org.apache.maven.model.Profile;
|
||||
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.ProfileActivationContext;
|
||||
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.MutablePlexusContainer;
|
||||
import org.codehaus.plexus.component.repository.exception.ComponentLifecycleException;
|
||||
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
|
||||
import org.codehaus.plexus.util.xml.pull.XmlSerializer;
|
||||
import org.codehaus.plexus.util.xml.pull.MXSerializer;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
import java.util.Map.Entry;
|
||||
import java.io.*;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
|
||||
public class DefaultProfileManager
|
||||
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 )
|
||||
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
|
||||
{
|
||||
activators = container.lookupList( ProfileActivator.class );
|
||||
|
||||
for ( ProfileActivator activator : activators )
|
||||
{
|
||||
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;
|
||||
serializer.setOutput( writer );
|
||||
serializer.startDocument("UTF-8", null );
|
||||
} catch (IOException e) {
|
||||
|
||||
}
|
||||
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 );
|
||||
if ( activators != null )
|
||||
p2.add(new ModelProperty(mp.getUri().replaceFirst("profile/", ""), mp.getResolvedValue()));
|
||||
}
|
||||
|
||||
ModelContainer mc = new IdModelContainerFactory(ProjectUri.Profiles.Profile.xUri).create(p2);
|
||||
for(ActiveProfileMatcher matcher : matchers)
|
||||
{
|
||||
if(matcher.isMatch(mc, interpolatorProperties))
|
||||
{
|
||||
try
|
||||
{
|
||||
container.releaseAll( activators );
|
||||
}
|
||||
catch ( ComponentLifecycleException e )
|
||||
{
|
||||
container.getLogger().debug( "Error releasing profile activators - ignoring.", e );
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
|
@ -341,18 +379,6 @@ public class DefaultProfileManager
|
|||
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 )
|
||||
{
|
||||
|
|
|
@ -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
|
||||
*
|
||||
|
|
|
@ -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>
|
Loading…
Reference in New Issue