mirror of https://github.com/apache/maven.git
[MNG-3885] [MNG-4009] : Fixed problem of profile info in parents not being applied.
git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@741282 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
3ccd647208
commit
247efed735
|
@ -125,7 +125,7 @@ public final class MavenDomainModel
|
|||
{
|
||||
ModelDataSource dataSource = new DefaultModelDataSource( modelProperties, PomTransformer.MODEL_CONTAINER_FACTORIES );
|
||||
|
||||
return new ProfileContext( dataSource, properties ).getActiveProfiles();
|
||||
return new ProfileContext( dataSource, null, properties ).getActiveProfiles();
|
||||
}
|
||||
|
||||
public ArtifactBasicMetadata getParentMetadata()
|
||||
|
|
|
@ -1,9 +1,6 @@
|
|||
package org.apache.maven.project.builder.profile;
|
||||
|
||||
import org.apache.maven.shared.model.ModelContainer;
|
||||
import org.apache.maven.shared.model.ModelDataSource;
|
||||
import org.apache.maven.shared.model.DataSourceException;
|
||||
import org.apache.maven.shared.model.InterpolatorProperty;
|
||||
import org.apache.maven.shared.model.*;
|
||||
import org.apache.maven.project.builder.ProjectUri;
|
||||
|
||||
import java.util.*;
|
||||
|
@ -15,13 +12,17 @@ public class ProfileContext {
|
|||
|
||||
private List<InterpolatorProperty> properties;
|
||||
|
||||
private Collection<String> activeProfileIds;
|
||||
|
||||
List<ActiveProfileMatcher> matchers = Collections.unmodifiableList( Arrays.asList(new ByDefaultMatcher(),
|
||||
new FileMatcher(), new JdkMatcher(), new OperatingSystemMatcher(), new PropertyMatcher()
|
||||
) );
|
||||
|
||||
public ProfileContext(ModelDataSource modelDataSource, List<InterpolatorProperty> properties) {
|
||||
public ProfileContext(ModelDataSource modelDataSource, Collection<String> activeProfileIds,
|
||||
List<InterpolatorProperty> properties) {
|
||||
this.modelDataSource = modelDataSource;
|
||||
this.properties = new ArrayList<InterpolatorProperty>(properties);
|
||||
this.activeProfileIds = (activeProfileIds != null) ? activeProfileIds : new ArrayList<String>();
|
||||
}
|
||||
|
||||
public Collection<ModelContainer> getActiveProfiles() throws DataSourceException {
|
||||
|
@ -32,10 +33,29 @@ public class ProfileContext {
|
|||
for(ActiveProfileMatcher matcher : matchers) {
|
||||
if(matcher.isMatch(mc, properties)) {
|
||||
matchedContainers.add(mc);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
String profileId = getProfileId(mc.getProperties());
|
||||
if(profileId != null && activeProfileIds.contains(profileId))
|
||||
{
|
||||
matchedContainers.add(mc);
|
||||
}
|
||||
}
|
||||
|
||||
return matchedContainers;
|
||||
}
|
||||
|
||||
private String getProfileId(List<ModelProperty> modelProperties)
|
||||
{
|
||||
for(ModelProperty mp : modelProperties)
|
||||
{
|
||||
if(mp.getUri().equals(ProfileUri.Profiles.Profile.id))
|
||||
{
|
||||
return mp.getResolvedValue();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -35,7 +35,27 @@ public class ProfileContextTest {
|
|||
List<InterpolatorProperty> interpolatorProperties = new ArrayList<InterpolatorProperty>();
|
||||
interpolatorProperties.add(new InterpolatorProperty( "${foo}", "bar"));
|
||||
|
||||
ProfileContext ctx = new ProfileContext(dataSource, interpolatorProperties);
|
||||
ProfileContext ctx = new ProfileContext(dataSource, null, interpolatorProperties);
|
||||
|
||||
Collection<ModelContainer> profiles = ctx.getActiveProfiles();
|
||||
|
||||
assertTrue(profiles.size() == 1);
|
||||
|
||||
}
|
||||
|
||||
@org.junit.Test
|
||||
public void getActiveProfilesById() 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 , "test"));
|
||||
|
||||
DefaultModelDataSource dataSource = new DefaultModelDataSource(modelProperties, PomTransformer.MODEL_CONTAINER_FACTORIES );
|
||||
|
||||
List<InterpolatorProperty> interpolatorProperties = new ArrayList<InterpolatorProperty>();
|
||||
|
||||
ProfileContext ctx = new ProfileContext(dataSource, Arrays.asList("test"), interpolatorProperties);
|
||||
|
||||
Collection<ModelContainer> profiles = ctx.getActiveProfiles();
|
||||
|
||||
|
|
|
@ -460,7 +460,7 @@ public class DefaultMavenProjectBuilder
|
|||
private static void setBuildOutputDirectoryOnParent( MavenProject project )
|
||||
{
|
||||
MavenProject parent = project.getParent();
|
||||
if ( parent != null )
|
||||
if ( parent != null && parent.getFile() != null && parent.getModel().getBuild() != null)
|
||||
{
|
||||
parent.getModel().getBuild().setDirectory( parent.getFile().getAbsolutePath() );
|
||||
setBuildOutputDirectoryOnParent( parent );
|
||||
|
|
|
@ -77,6 +77,10 @@ public final class PomClassicDomainModel
|
|||
|
||||
private int lineageCount;
|
||||
|
||||
public PomClassicDomainModel( List<ModelProperty> modelProperties)
|
||||
{
|
||||
this.modelProperties = modelProperties;
|
||||
}
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
|
|
|
@ -26,6 +26,7 @@ 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;
|
||||
|
@ -46,6 +47,8 @@ import org.apache.maven.project.ProjectBuilderConfiguration;
|
|||
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;
|
||||
|
@ -162,12 +165,13 @@ public class DefaultProjectBuilder
|
|||
PomArtifactResolver resolver )
|
||||
throws IOException
|
||||
{
|
||||
return buildModel( pom, null, interpolatorProperties, resolver );
|
||||
return buildModel( pom, null, interpolatorProperties, null, resolver );
|
||||
}
|
||||
|
||||
public PomClassicDomainModel buildModel( File pom,
|
||||
private PomClassicDomainModel buildModel( File pom,
|
||||
List<Model> mixins,
|
||||
Collection<InterpolatorProperty> interpolatorProperties,
|
||||
Collection<String> activeProfileIds,
|
||||
PomArtifactResolver resolver )
|
||||
throws IOException
|
||||
{
|
||||
|
@ -192,6 +196,11 @@ public class DefaultProjectBuilder
|
|||
Collections.reverse( mixins );
|
||||
}
|
||||
|
||||
if(activeProfileIds == null)
|
||||
{
|
||||
activeProfileIds = new ArrayList<String>();
|
||||
}
|
||||
|
||||
List<InterpolatorProperty> properties;
|
||||
if ( interpolatorProperties == null )
|
||||
{
|
||||
|
@ -205,6 +214,11 @@ public class DefaultProjectBuilder
|
|||
PomClassicDomainModel domainModel = new PomClassicDomainModel( pom );
|
||||
domainModel.setProjectDirectory( pom.getParentFile() );
|
||||
|
||||
ProfileContext profileContext = new ProfileContext(new DefaultModelDataSource(domainModel.getModelProperties(),
|
||||
PomTransformer.MODEL_CONTAINER_FACTORIES), activeProfileIds, properties);
|
||||
Collection<ModelContainer> profileContainers = profileContext.getActiveProfiles();
|
||||
//get mixin
|
||||
|
||||
List<DomainModel> domainModels = new ArrayList<DomainModel>();
|
||||
domainModels.add( domainModel );
|
||||
|
||||
|
@ -215,11 +229,11 @@ public class DefaultProjectBuilder
|
|||
List<DomainModel> mavenParents;
|
||||
if ( isParentLocal( domainModel.getModel().getParent(), pom.getParentFile() ) )
|
||||
{
|
||||
mavenParents = getDomainModelParentsFromLocalPath( domainModel, resolver, pom.getParentFile() );
|
||||
mavenParents = getDomainModelParentsFromLocalPath( domainModel, resolver, pom.getParentFile(), properties, activeProfileIds );
|
||||
}
|
||||
else
|
||||
{
|
||||
mavenParents = getDomainModelParentsFromRepository( domainModel, resolver );
|
||||
mavenParents = getDomainModelParentsFromRepository( domainModel, resolver, properties, activeProfileIds );
|
||||
}
|
||||
|
||||
if ( mavenParents.size() > 0 )
|
||||
|
@ -263,9 +277,17 @@ public class DefaultProjectBuilder
|
|||
MavenProjectBuilder mavenProjectBuilder)
|
||||
throws IOException
|
||||
{
|
||||
|
||||
List<String> profileIds = (projectBuilderConfiguration != null &&
|
||||
projectBuilderConfiguration.getGlobalProfileManager() != null &&
|
||||
projectBuilderConfiguration.getGlobalProfileManager().getProfileActivationContext() != null) ?
|
||||
projectBuilderConfiguration.getGlobalProfileManager().getProfileActivationContext().getExplicitlyActiveProfileIds() : new ArrayList<String>();
|
||||
|
||||
|
||||
PomClassicDomainModel domainModel = buildModel( pom,
|
||||
mixins,
|
||||
interpolatorProperties,
|
||||
profileIds,
|
||||
resolver );
|
||||
|
||||
try
|
||||
|
@ -313,7 +335,9 @@ public class DefaultProjectBuilder
|
|||
}
|
||||
|
||||
private List<DomainModel> getDomainModelParentsFromRepository( PomClassicDomainModel domainModel,
|
||||
PomArtifactResolver artifactResolver )
|
||||
PomArtifactResolver artifactResolver,
|
||||
List<InterpolatorProperty> properties,
|
||||
Collection<String> activeProfileIds)
|
||||
throws IOException
|
||||
{
|
||||
List<DomainModel> domainModels = new ArrayList<DomainModel>();
|
||||
|
@ -339,7 +363,29 @@ public class DefaultProjectBuilder
|
|||
}
|
||||
|
||||
domainModels.add( parentDomainModel );
|
||||
domainModels.addAll( getDomainModelParentsFromRepository( parentDomainModel, artifactResolver ) );
|
||||
|
||||
ProfileContext profileContext = new ProfileContext(new DefaultModelDataSource(parentDomainModel.getModelProperties(),
|
||||
PomTransformer.MODEL_CONTAINER_FACTORIES), activeProfileIds, properties);
|
||||
Collection<ModelContainer> profileContainers = profileContext.getActiveProfiles();
|
||||
|
||||
for(ModelContainer mc : profileContainers)
|
||||
{
|
||||
List<ModelProperty> transformed = new ArrayList<ModelProperty>();
|
||||
transformed.add(new ModelProperty(ProjectUri.xUri, null));
|
||||
for(ModelProperty mp : mc.getProperties())
|
||||
{
|
||||
if(mp.getUri().startsWith(ProjectUri.Profiles.Profile.xUri) && !mp.getUri().equals(ProjectUri.Profiles.Profile.id)
|
||||
&& !mp.getUri().startsWith(ProjectUri.Profiles.Profile.Activation.xUri) )
|
||||
{
|
||||
transformed.add(new ModelProperty(mp.getUri().replace(ProjectUri.Profiles.Profile.xUri, ProjectUri.xUri),
|
||||
mp.getResolvedValue()));
|
||||
}
|
||||
}
|
||||
|
||||
domainModels.add(new PomClassicDomainModel(transformed));
|
||||
}
|
||||
|
||||
domainModels.addAll( getDomainModelParentsFromRepository( parentDomainModel, artifactResolver, properties, activeProfileIds ) );
|
||||
return domainModels;
|
||||
}
|
||||
|
||||
|
@ -354,7 +400,9 @@ public class DefaultProjectBuilder
|
|||
*/
|
||||
private List<DomainModel> getDomainModelParentsFromLocalPath( PomClassicDomainModel domainModel,
|
||||
PomArtifactResolver artifactResolver,
|
||||
File projectDirectory )
|
||||
File projectDirectory,
|
||||
List<InterpolatorProperty> properties,
|
||||
Collection<String> activeProfileIds)
|
||||
throws IOException
|
||||
{
|
||||
List<DomainModel> domainModels = new ArrayList<DomainModel>();
|
||||
|
@ -381,6 +429,25 @@ public class DefaultProjectBuilder
|
|||
|
||||
PomClassicDomainModel parentDomainModel = new PomClassicDomainModel( parentFile );
|
||||
parentDomainModel.setProjectDirectory( parentFile.getParentFile() );
|
||||
ProfileContext profileContext = new ProfileContext(new DefaultModelDataSource(parentDomainModel.getModelProperties(),
|
||||
PomTransformer.MODEL_CONTAINER_FACTORIES), activeProfileIds, properties);
|
||||
Collection<ModelContainer> profileContainers = profileContext.getActiveProfiles();
|
||||
|
||||
for(ModelContainer mc : profileContainers)
|
||||
{
|
||||
List<ModelProperty> transformed = new ArrayList<ModelProperty>();
|
||||
transformed.add(new ModelProperty(ProjectUri.xUri, null));
|
||||
for(ModelProperty mp : mc.getProperties())
|
||||
{
|
||||
if(mp.getUri().startsWith(ProjectUri.Profiles.Profile.xUri) && !mp.getUri().equals(ProjectUri.Profiles.Profile.id)
|
||||
&& !mp.getUri().startsWith(ProjectUri.Profiles.Profile.Activation.xUri))
|
||||
{
|
||||
transformed.add(new ModelProperty(mp.getUri().replace(ProjectUri.Profiles.Profile.xUri, ProjectUri.xUri),
|
||||
mp.getResolvedValue()));
|
||||
}
|
||||
}
|
||||
domainModels.add(new PomClassicDomainModel(transformed));
|
||||
}
|
||||
|
||||
if ( !parentDomainModel.matchesParent( domainModel.getModel().getParent() ) )
|
||||
{
|
||||
|
@ -388,7 +455,7 @@ public class DefaultProjectBuilder
|
|||
+ parentDomainModel.getId() + ", Child ID = " + domainModel.getId() + ", Expected Parent ID = "
|
||||
+ domainModel.getModel().getParent().getId() );
|
||||
|
||||
List<DomainModel> parentDomainModels = getDomainModelParentsFromRepository( domainModel, artifactResolver );
|
||||
List<DomainModel> parentDomainModels = getDomainModelParentsFromRepository( domainModel, artifactResolver, properties, activeProfileIds );
|
||||
|
||||
if(parentDomainModels.size() == 0)
|
||||
{
|
||||
|
@ -406,11 +473,11 @@ public class DefaultProjectBuilder
|
|||
if ( isParentLocal( parentDomainModel.getModel().getParent(), parentFile.getParentFile() ) )
|
||||
{
|
||||
domainModels.addAll( getDomainModelParentsFromLocalPath( parentDomainModel, artifactResolver,
|
||||
parentFile.getParentFile() ) );
|
||||
parentFile.getParentFile(), properties, activeProfileIds ) );
|
||||
}
|
||||
else
|
||||
{
|
||||
domainModels.addAll( getDomainModelParentsFromRepository( parentDomainModel, artifactResolver ) );
|
||||
domainModels.addAll( getDomainModelParentsFromRepository( parentDomainModel, artifactResolver, properties, activeProfileIds ) );
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -25,8 +25,13 @@ import java.io.FileInputStream;
|
|||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.apache.maven.MavenTools;
|
||||
import org.apache.maven.profiles.DefaultProfileManager;
|
||||
import org.apache.maven.profiles.activation.DefaultProfileActivationContext;
|
||||
import org.apache.maven.profiles.activation.ProfileActivationContext;
|
||||
import org.apache.maven.shared.model.InterpolatorProperty;
|
||||
import org.apache.maven.model.Model;
|
||||
import org.apache.maven.model.Plugin;
|
||||
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
|
||||
|
@ -160,6 +165,13 @@ public class PomConstructionTest
|
|||
|
||||
}
|
||||
*/
|
||||
/*MNG-3803*/
|
||||
public void testDependenciesWithDifferentVersions()
|
||||
throws Exception
|
||||
{
|
||||
PomTestWrapper pom = buildPom( "dependencies-with-different-versions" );
|
||||
assertEquals( 1, ( (List<?>) pom.getValue( "dependencies" ) ).size() );
|
||||
}
|
||||
|
||||
/* MNG-3567*/
|
||||
public void testParentInterpolation()
|
||||
|
@ -813,6 +825,9 @@ public class PomConstructionTest
|
|||
}
|
||||
ProjectBuilderConfiguration config = new DefaultProjectBuilderConfiguration();
|
||||
config.setLocalRepository(new DefaultArtifactRepository("default", "", new DefaultRepositoryLayout()));
|
||||
ProfileActivationContext pCtx = new DefaultProfileActivationContext(null, true);
|
||||
pCtx.setExplicitlyActiveProfileIds(Arrays.asList("release"));
|
||||
config.setGlobalProfileManager(new DefaultProfileManager(this.getContainer(), pCtx));
|
||||
return new PomTestWrapper( pomFile, projectBuilder.buildFromLocalPath( pomFile, null, null, pomArtifactResolver,
|
||||
config, mavenProjectBuilder ) );
|
||||
}
|
||||
|
|
|
@ -0,0 +1,18 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>a</groupId>
|
||||
<artifactId>b</artifactId>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>commons-collections</groupId>
|
||||
<artifactId>commons-collections</artifactId>
|
||||
<version>2.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-collections</groupId>
|
||||
<artifactId>commons-collections</artifactId>
|
||||
<version>3.1</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
Loading…
Reference in New Issue