o Fixed regressions regarding profile de-/activation

git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@745642 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Benjamin Bentmann 2009-02-18 20:47:42 +00:00
parent 4e1fc45f7e
commit c53466f406
5 changed files with 153 additions and 45 deletions

View File

@ -30,7 +30,6 @@ import java.util.List;
import org.apache.maven.mercury.artifact.ArtifactBasicMetadata;
import org.apache.maven.project.builder.ArtifactModelContainerFactory;
import org.apache.maven.project.builder.ExclusionModelContainerFactory;
import org.apache.maven.project.builder.IdModelContainerFactory;
import org.apache.maven.project.builder.PomTransformer;
import org.apache.maven.project.builder.ProjectUri;
import org.apache.maven.project.builder.profile.ProfileContext;
@ -125,7 +124,7 @@ public final class MavenDomainModel
{
ModelDataSource dataSource = new DefaultModelDataSource( modelProperties, PomTransformer.MODEL_CONTAINER_FACTORIES );
return new ProfileContext( dataSource, null, properties ).getActiveProfiles();
return new ProfileContext( dataSource, null, null, properties ).getActiveProfiles();
}
public ArtifactBasicMetadata getParentMetadata()

View File

@ -14,37 +14,64 @@ public class ProfileContext {
private Collection<String> activeProfileIds;
List<ActiveProfileMatcher> matchers = Collections.unmodifiableList( Arrays.asList(new ByDefaultMatcher(),
private Collection<String> inactiveProfileIds;
private ActiveProfileMatcher defaultMatcher = new ByDefaultMatcher();
private List<ActiveProfileMatcher> matchers = Collections.unmodifiableList( Arrays.asList(
new FileMatcher(), new JdkMatcher(), new OperatingSystemMatcher(), new PropertyMatcher()
) );
public ProfileContext(ModelDataSource modelDataSource, Collection<String> activeProfileIds,
List<InterpolatorProperty> properties) {
public ProfileContext( ModelDataSource modelDataSource, Collection<String> activeProfileIds,
Collection<String> inactiveProfileIds, List<InterpolatorProperty> properties )
{
this.modelDataSource = modelDataSource;
this.properties = new ArrayList<InterpolatorProperty>(properties);
this.activeProfileIds = (activeProfileIds != null) ? activeProfileIds : new ArrayList<String>();
this.properties = new ArrayList<InterpolatorProperty>( properties );
this.activeProfileIds = ( activeProfileIds != null ) ? activeProfileIds : new ArrayList<String>();
this.inactiveProfileIds = ( inactiveProfileIds != null ) ? inactiveProfileIds : new ArrayList<String>();
}
public Collection<ModelContainer> getActiveProfiles() throws DataSourceException {
public Collection<ModelContainer> getActiveProfiles()
throws DataSourceException
{
List<ModelContainer> matchedContainers = new ArrayList<ModelContainer>();
List<ModelContainer> defaultContainers = new ArrayList<ModelContainer>();
List<ModelContainer> modelContainers = modelDataSource.queryFor(ProjectUri.Profiles.Profile.xUri);
for(ModelContainer mc : modelContainers) {
for(ActiveProfileMatcher matcher : matchers) {
if(matcher.isMatch(mc, properties)) {
matchedContainers.add(mc);
continue;
}
}
List<ModelContainer> modelContainers = modelDataSource.queryFor( ProjectUri.Profiles.Profile.xUri );
for ( ModelContainer mc : modelContainers )
{
String profileId = getProfileId( mc.getProperties() );
String profileId = getProfileId(mc.getProperties());
if(profileId != null && activeProfileIds.contains(profileId))
if ( !inactiveProfileIds.contains( profileId ) )
{
matchedContainers.add(mc);
if ( activeProfileIds.contains( profileId ) )
{
matchedContainers.add( mc );
}
else if ( defaultMatcher.isMatch( mc, properties ) )
{
defaultContainers.add( mc );
}
else
{
for ( ActiveProfileMatcher matcher : matchers )
{
if ( matcher.isMatch( mc, properties ) )
{
matchedContainers.add( mc );
break;
}
}
}
}
}
return matchedContainers;
if ( matchedContainers.isEmpty() )
{
matchedContainers = defaultContainers;
}
return matchedContainers;
}
private String getProfileId(List<ModelProperty> modelProperties)

View File

@ -3,14 +3,14 @@ package org.apache.maven.project.builder.profile;
import org.apache.maven.project.builder.profile.ProfileContext;
import org.apache.maven.project.builder.PomTransformer;
import org.apache.maven.project.builder.ProjectUri;
import org.apache.maven.project.builder.ArtifactModelContainerFactory;
import org.apache.maven.project.builder.IdModelContainerFactory;
import org.apache.maven.shared.model.DataSourceException;
import org.apache.maven.shared.model.InterpolatorProperty;
import org.apache.maven.shared.model.ModelContainer;
import org.apache.maven.shared.model.ModelProperty;
import org.apache.maven.shared.model.impl.DefaultModelDataSource;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import static org.junit.Assert.*;
import java.util.ArrayList;
import java.util.Arrays;
@ -19,7 +19,7 @@ import java.util.List;
public class ProfileContextTest {
@org.junit.Test
@Test
public void getActiveProfiles() throws DataSourceException {
List<ModelProperty> modelProperties = new ArrayList<ModelProperty>();
modelProperties.add(new ModelProperty(ProjectUri.xUri, null));
@ -35,7 +35,7 @@ public class ProfileContextTest {
List<InterpolatorProperty> interpolatorProperties = new ArrayList<InterpolatorProperty>();
interpolatorProperties.add(new InterpolatorProperty( "${foo}", "bar"));
ProfileContext ctx = new ProfileContext(dataSource, null, interpolatorProperties);
ProfileContext ctx = new ProfileContext(dataSource, null, null, interpolatorProperties);
Collection<ModelContainer> profiles = ctx.getActiveProfiles();
@ -43,7 +43,7 @@ public class ProfileContextTest {
}
@org.junit.Test
@Test
public void getActiveProfilesById() throws DataSourceException {
List<ModelProperty> modelProperties = new ArrayList<ModelProperty>();
modelProperties.add(new ModelProperty(ProjectUri.xUri, null));
@ -55,11 +55,75 @@ public class ProfileContextTest {
List<InterpolatorProperty> interpolatorProperties = new ArrayList<InterpolatorProperty>();
ProfileContext ctx = new ProfileContext(dataSource, Arrays.asList("test"), interpolatorProperties);
ProfileContext ctx = new ProfileContext(dataSource, Arrays.asList("test"), null, interpolatorProperties);
Collection<ModelContainer> profiles = ctx.getActiveProfiles();
assertTrue(profiles.size() == 1);
}
@Test
public void getActiveByDefaultProfilesOnlyActivatedIfNoOtherPomProfilesAreActive()
throws DataSourceException
{
List<ModelProperty> modelProperties = new ArrayList<ModelProperty>();
modelProperties.add( new ModelProperty( ProjectUri.xUri, null ) );
modelProperties.add( new ModelProperty( ProjectUri.Profiles.xUri, null ) );
modelProperties.add( new ModelProperty( ProjectUri.Profiles.Profile.xUri, null ) );
modelProperties.add( new ModelProperty( ProjectUri.Profiles.Profile.id, "default" ) );
modelProperties.add( new ModelProperty( ProjectUri.Profiles.Profile.Activation.xUri, null ) );
modelProperties.add( new ModelProperty( ProjectUri.Profiles.Profile.Activation.activeByDefault, "true" ) );
modelProperties.add( new ModelProperty( ProjectUri.Profiles.Profile.xUri, null ) );
modelProperties.add( new ModelProperty( ProjectUri.Profiles.Profile.id, "explicit" ) );
DefaultModelDataSource dataSource =
new DefaultModelDataSource( modelProperties, PomTransformer.MODEL_CONTAINER_FACTORIES );
List<InterpolatorProperty> interpolatorProperties = new ArrayList<InterpolatorProperty>();
ProfileContext ctx = new ProfileContext( dataSource, Arrays.asList( "explicit" ), null, interpolatorProperties );
Collection<ModelContainer> profiles = ctx.getActiveProfiles();
assertEquals( 1, profiles.size() );
assertProperty( profiles.iterator().next().getProperties(), ProjectUri.Profiles.Profile.id, "explicit" );
}
@Test
public void getDeactivateProfiles()
throws DataSourceException
{
List<ModelProperty> modelProperties = new ArrayList<ModelProperty>();
modelProperties.add( new ModelProperty( ProjectUri.xUri, null ) );
modelProperties.add( new ModelProperty( ProjectUri.Profiles.xUri, null ) );
modelProperties.add( new ModelProperty( ProjectUri.Profiles.Profile.xUri, null ) );
modelProperties.add( new ModelProperty( ProjectUri.Profiles.Profile.id, "default" ) );
modelProperties.add( new ModelProperty( ProjectUri.Profiles.Profile.Activation.xUri, null ) );
modelProperties.add( new ModelProperty( ProjectUri.Profiles.Profile.Activation.activeByDefault, "true" ) );
DefaultModelDataSource dataSource =
new DefaultModelDataSource( modelProperties, PomTransformer.MODEL_CONTAINER_FACTORIES );
List<InterpolatorProperty> interpolatorProperties = new ArrayList<InterpolatorProperty>();
ProfileContext ctx = new ProfileContext( dataSource, null, Arrays.asList( "default" ), interpolatorProperties );
Collection<ModelContainer> profiles = ctx.getActiveProfiles();
assertEquals( 0, profiles.size() );
}
private void assertProperty( Collection<ModelProperty> properties, String uri, String value )
{
for ( ModelProperty property : properties )
{
if ( uri.equals( property.getUri() ) && value.equals( property.getValue() ) )
{
return;
}
}
fail( "missing model property " + uri + " = " + value );
}
}

View File

@ -181,6 +181,7 @@ public class DefaultProfileManager
return getActiveProfiles( null );
}
// TODO: Portions of this logic are duplicated in o.a.m.p.b.p.ProfileContext, something is wrong here
public List getActiveProfiles( Model model )
throws ProfileActivationException
{

View File

@ -26,14 +26,12 @@ import java.io.StringReader;
import java.util.*;
import org.apache.maven.MavenTools;
import org.apache.maven.profiles.activation.ProfileActivationContext;
import org.apache.maven.mercury.PomProcessor;
import org.apache.maven.mercury.PomProcessorException;
import org.apache.maven.mercury.MavenDomainModel;
import org.apache.maven.mercury.MavenDomainModelFactory;
import org.apache.maven.mercury.builder.api.MetadataReader;
import org.apache.maven.mercury.builder.api.MetadataReaderException;
import org.apache.maven.mercury.builder.api.DependencyProcessorException;
import org.apache.maven.mercury.artifact.ArtifactBasicMetadata;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.InvalidRepositoryException;
@ -48,7 +46,6 @@ import org.apache.maven.project.MavenProjectBuilder;
import org.apache.maven.project.builder.*;
import org.apache.maven.project.builder.ProjectUri;
import org.apache.maven.project.builder.profile.ProfileContext;
import org.apache.maven.project.builder.profile.ProfileUri;
import org.apache.maven.shared.model.*;
import org.apache.maven.shared.model.impl.DefaultModelDataSource;
import org.codehaus.plexus.component.annotations.Component;
@ -166,13 +163,13 @@ public class DefaultProjectBuilder
PomArtifactResolver resolver )
throws IOException
{
return buildModel( pom, null, interpolatorProperties, null, resolver );
return buildModel( pom, null, interpolatorProperties, null, null, resolver );
}
private PomClassicDomainModel buildModel( File pom,
List<Model> mixins,
Collection<InterpolatorProperty> interpolatorProperties,
Collection<String> activeProfileIds,
Collection<String> activeProfileIds, Collection<String> inactiveProfileIds,
PomArtifactResolver resolver )
throws IOException
{
@ -201,6 +198,10 @@ public class DefaultProjectBuilder
{
activeProfileIds = new ArrayList<String>();
}
if ( inactiveProfileIds == null )
{
inactiveProfileIds = new ArrayList<String>();
}
List<InterpolatorProperty> properties;
if ( interpolatorProperties == null )
@ -219,7 +220,7 @@ public class DefaultProjectBuilder
//Process Profile on most specialized child model
ProfileContext profileContext = new ProfileContext(new DefaultModelDataSource(domainModel.getModelProperties(),
PomTransformer.MODEL_CONTAINER_FACTORIES), activeProfileIds, properties);
PomTransformer.MODEL_CONTAINER_FACTORIES), activeProfileIds, inactiveProfileIds, properties);
Collection<ModelContainer> profileContainers = profileContext.getActiveProfiles();
@ -246,11 +247,15 @@ public class DefaultProjectBuilder
List<DomainModel> mavenParents;
if ( isParentLocal( domainModel.getModel().getParent(), pom.getParentFile() ) )
{
mavenParents = getDomainModelParentsFromLocalPath( domainModel, resolver, pom.getParentFile(), properties, activeProfileIds );
mavenParents =
getDomainModelParentsFromLocalPath( domainModel, resolver, pom.getParentFile(), properties,
activeProfileIds, inactiveProfileIds );
}
else
{
mavenParents = getDomainModelParentsFromRepository( domainModel, resolver, properties, activeProfileIds );
mavenParents =
getDomainModelParentsFromRepository( domainModel, resolver, properties, activeProfileIds,
inactiveProfileIds );
}
if ( mavenParents.size() > 0 )
@ -295,16 +300,20 @@ public class DefaultProjectBuilder
throws IOException
{
List<String> profileIds = (projectBuilderConfiguration != null &&
List<String> activeProfileIds = (projectBuilderConfiguration != null &&
projectBuilderConfiguration.getGlobalProfileManager() != null &&
projectBuilderConfiguration.getGlobalProfileManager().getProfileActivationContext() != null) ?
projectBuilderConfiguration.getGlobalProfileManager().getProfileActivationContext().getExplicitlyActiveProfileIds() : new ArrayList<String>();
List<String> inactiveProfileIds =
( projectBuilderConfiguration != null && projectBuilderConfiguration.getGlobalProfileManager() != null &&
projectBuilderConfiguration.getGlobalProfileManager().getProfileActivationContext() != null ) ?
projectBuilderConfiguration.getGlobalProfileManager().getProfileActivationContext().getExplicitlyInactiveProfileIds() : new ArrayList<String>();
PomClassicDomainModel domainModel = buildModel( pom,
mixins,
interpolatorProperties,
profileIds,
activeProfileIds, inactiveProfileIds,
resolver );
try
@ -354,7 +363,8 @@ public class DefaultProjectBuilder
private List<DomainModel> getDomainModelParentsFromRepository( PomClassicDomainModel domainModel,
PomArtifactResolver artifactResolver,
List<InterpolatorProperty> properties,
Collection<String> activeProfileIds)
Collection<String> activeProfileIds,
Collection<String> inactiveProfileIds )
throws IOException
{
List<DomainModel> domainModels = new ArrayList<DomainModel>();
@ -383,7 +393,7 @@ public class DefaultProjectBuilder
//Process Profiles
ProfileContext profileContext = new ProfileContext(new DefaultModelDataSource(parentDomainModel.getModelProperties(),
PomTransformer.MODEL_CONTAINER_FACTORIES), activeProfileIds, properties);
PomTransformer.MODEL_CONTAINER_FACTORIES), activeProfileIds, inactiveProfileIds, properties);
Collection<ModelContainer> profileContainers = profileContext.getActiveProfiles();
for(ModelContainer mc : profileContainers)
@ -403,7 +413,8 @@ public class DefaultProjectBuilder
domainModels.add(new PomClassicDomainModel(transformed));
}
domainModels.addAll( getDomainModelParentsFromRepository( parentDomainModel, artifactResolver, properties, activeProfileIds ) );
domainModels.addAll( getDomainModelParentsFromRepository( parentDomainModel, artifactResolver, properties,
activeProfileIds, inactiveProfileIds ) );
return domainModels;
}
@ -420,7 +431,8 @@ public class DefaultProjectBuilder
PomArtifactResolver artifactResolver,
File projectDirectory,
List<InterpolatorProperty> properties,
Collection<String> activeProfileIds)
Collection<String> activeProfileIds,
Collection<String> inactiveProfileIds )
throws IOException
{
List<DomainModel> domainModels = new ArrayList<DomainModel>();
@ -450,7 +462,7 @@ public class DefaultProjectBuilder
//Process Profiles
ProfileContext profileContext = new ProfileContext(new DefaultModelDataSource(parentDomainModel.getModelProperties(),
PomTransformer.MODEL_CONTAINER_FACTORIES), activeProfileIds, properties);
PomTransformer.MODEL_CONTAINER_FACTORIES), activeProfileIds, inactiveProfileIds, properties);
Collection<ModelContainer> profileContainers = profileContext.getActiveProfiles();
for(ModelContainer mc : profileContainers)
@ -475,7 +487,9 @@ public class DefaultProjectBuilder
+ parentDomainModel.getId() + ", Child ID = " + domainModel.getId() + ", Expected Parent ID = "
+ domainModel.getModel().getParent().getId() );
List<DomainModel> parentDomainModels = getDomainModelParentsFromRepository( domainModel, artifactResolver, properties, activeProfileIds );
List<DomainModel> parentDomainModels =
getDomainModelParentsFromRepository( domainModel, artifactResolver, properties, activeProfileIds,
inactiveProfileIds );
if(parentDomainModels.size() == 0)
{
@ -493,11 +507,14 @@ public class DefaultProjectBuilder
if ( isParentLocal( parentDomainModel.getModel().getParent(), parentFile.getParentFile() ) )
{
domainModels.addAll( getDomainModelParentsFromLocalPath( parentDomainModel, artifactResolver,
parentFile.getParentFile(), properties, activeProfileIds ) );
parentFile.getParentFile(), properties,
activeProfileIds, inactiveProfileIds ) );
}
else
{
domainModels.addAll( getDomainModelParentsFromRepository( parentDomainModel, artifactResolver, properties, activeProfileIds ) );
domainModels.addAll( getDomainModelParentsFromRepository( parentDomainModel, artifactResolver,
properties, activeProfileIds,
inactiveProfileIds ) );
}
}