From 247efed7351236c4c6912b8a5e483aa04d01a931 Mon Sep 17 00:00:00 2001 From: Britton Isbell Date: Thu, 5 Feb 2009 20:38:37 +0000 Subject: [PATCH] [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 --- .../maven/mercury/MavenDomainModel.java | 2 +- .../builder/profile/ProfileContext.java | 30 +++++-- .../builder/profile/ProfileContextTest.java | 22 ++++- .../project/DefaultMavenProjectBuilder.java | 2 +- .../builder/PomClassicDomainModel.java | 4 + .../builder/impl/DefaultProjectBuilder.java | 89 ++++++++++++++++--- .../project/builder/PomConstructionTest.java | 15 ++++ .../pom.xml | 18 ++++ 8 files changed, 163 insertions(+), 19 deletions(-) create mode 100644 maven-project/src/test/resources-project-builder/dependencies-with-different-versions/pom.xml diff --git a/maven-mercury/src/main/java/org/apache/maven/mercury/MavenDomainModel.java b/maven-mercury/src/main/java/org/apache/maven/mercury/MavenDomainModel.java index a8e8a7eaa5..961c3a8a96 100644 --- a/maven-mercury/src/main/java/org/apache/maven/mercury/MavenDomainModel.java +++ b/maven-mercury/src/main/java/org/apache/maven/mercury/MavenDomainModel.java @@ -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() diff --git a/maven-project-builder/src/main/java/org/apache/maven/project/builder/profile/ProfileContext.java b/maven-project-builder/src/main/java/org/apache/maven/project/builder/profile/ProfileContext.java index eba3777478..2baa6906e9 100644 --- a/maven-project-builder/src/main/java/org/apache/maven/project/builder/profile/ProfileContext.java +++ b/maven-project-builder/src/main/java/org/apache/maven/project/builder/profile/ProfileContext.java @@ -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 properties; + private Collection activeProfileIds; + List matchers = Collections.unmodifiableList( Arrays.asList(new ByDefaultMatcher(), new FileMatcher(), new JdkMatcher(), new OperatingSystemMatcher(), new PropertyMatcher() ) ); - public ProfileContext(ModelDataSource modelDataSource, List properties) { + public ProfileContext(ModelDataSource modelDataSource, Collection activeProfileIds, + List properties) { this.modelDataSource = modelDataSource; this.properties = new ArrayList(properties); + this.activeProfileIds = (activeProfileIds != null) ? activeProfileIds : new ArrayList(); } public Collection 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 modelProperties) + { + for(ModelProperty mp : modelProperties) + { + if(mp.getUri().equals(ProfileUri.Profiles.Profile.id)) + { + return mp.getResolvedValue(); + } + } + return null; + } } diff --git a/maven-project-builder/src/test/java/org/apache/maven/project/builder/profile/ProfileContextTest.java b/maven-project-builder/src/test/java/org/apache/maven/project/builder/profile/ProfileContextTest.java index 08597997fe..6c788bfab7 100644 --- a/maven-project-builder/src/test/java/org/apache/maven/project/builder/profile/ProfileContextTest.java +++ b/maven-project-builder/src/test/java/org/apache/maven/project/builder/profile/ProfileContextTest.java @@ -35,7 +35,27 @@ public class ProfileContextTest { List interpolatorProperties = new ArrayList(); interpolatorProperties.add(new InterpolatorProperty( "${foo}", "bar")); - ProfileContext ctx = new ProfileContext(dataSource, interpolatorProperties); + ProfileContext ctx = new ProfileContext(dataSource, null, interpolatorProperties); + + Collection profiles = ctx.getActiveProfiles(); + + assertTrue(profiles.size() == 1); + + } + + @org.junit.Test + public void getActiveProfilesById() throws DataSourceException { + List modelProperties = new ArrayList(); + 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 interpolatorProperties = new ArrayList(); + + ProfileContext ctx = new ProfileContext(dataSource, Arrays.asList("test"), interpolatorProperties); Collection profiles = ctx.getActiveProfiles(); diff --git a/maven-project/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java b/maven-project/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java index 2455c3876c..54d5d5e21e 100644 --- a/maven-project/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java +++ b/maven-project/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java @@ -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 ); diff --git a/maven-project/src/main/java/org/apache/maven/project/builder/PomClassicDomainModel.java b/maven-project/src/main/java/org/apache/maven/project/builder/PomClassicDomainModel.java index f7238f02d4..5475819990 100644 --- a/maven-project/src/main/java/org/apache/maven/project/builder/PomClassicDomainModel.java +++ b/maven-project/src/main/java/org/apache/maven/project/builder/PomClassicDomainModel.java @@ -77,6 +77,10 @@ public final class PomClassicDomainModel private int lineageCount; + public PomClassicDomainModel( List modelProperties) + { + this.modelProperties = modelProperties; + } /** * Constructor * diff --git a/maven-project/src/main/java/org/apache/maven/project/builder/impl/DefaultProjectBuilder.java b/maven-project/src/main/java/org/apache/maven/project/builder/impl/DefaultProjectBuilder.java index dd1e6bac02..c5604bb213 100644 --- a/maven-project/src/main/java/org/apache/maven/project/builder/impl/DefaultProjectBuilder.java +++ b/maven-project/src/main/java/org/apache/maven/project/builder/impl/DefaultProjectBuilder.java @@ -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 mixins, Collection interpolatorProperties, + Collection activeProfileIds, PomArtifactResolver resolver ) throws IOException { @@ -192,6 +196,11 @@ public class DefaultProjectBuilder Collections.reverse( mixins ); } + if(activeProfileIds == null) + { + activeProfileIds = new ArrayList(); + } + List 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 profileContainers = profileContext.getActiveProfiles(); + //get mixin + List domainModels = new ArrayList(); domainModels.add( domainModel ); @@ -215,11 +229,11 @@ public class DefaultProjectBuilder List 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 profileIds = (projectBuilderConfiguration != null && + projectBuilderConfiguration.getGlobalProfileManager() != null && + projectBuilderConfiguration.getGlobalProfileManager().getProfileActivationContext() != null) ? + projectBuilderConfiguration.getGlobalProfileManager().getProfileActivationContext().getExplicitlyActiveProfileIds() : new ArrayList(); + + PomClassicDomainModel domainModel = buildModel( pom, mixins, - interpolatorProperties, + interpolatorProperties, + profileIds, resolver ); try @@ -313,7 +335,9 @@ public class DefaultProjectBuilder } private List getDomainModelParentsFromRepository( PomClassicDomainModel domainModel, - PomArtifactResolver artifactResolver ) + PomArtifactResolver artifactResolver, + List properties, + Collection activeProfileIds) throws IOException { List domainModels = new ArrayList(); @@ -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 profileContainers = profileContext.getActiveProfiles(); + + for(ModelContainer mc : profileContainers) + { + List transformed = new ArrayList(); + 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 getDomainModelParentsFromLocalPath( PomClassicDomainModel domainModel, PomArtifactResolver artifactResolver, - File projectDirectory ) + File projectDirectory, + List properties, + Collection activeProfileIds) throws IOException { List domainModels = new ArrayList(); @@ -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 profileContainers = profileContext.getActiveProfiles(); + + for(ModelContainer mc : profileContainers) + { + List transformed = new ArrayList(); + 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 parentDomainModels = getDomainModelParentsFromRepository( domainModel, artifactResolver ); + List 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 ) ); } } diff --git a/maven-project/src/test/java/org/apache/maven/project/builder/PomConstructionTest.java b/maven-project/src/test/java/org/apache/maven/project/builder/PomConstructionTest.java index f932d02a1b..53ac8a5427 100644 --- a/maven-project/src/test/java/org/apache/maven/project/builder/PomConstructionTest.java +++ b/maven-project/src/test/java/org/apache/maven/project/builder/PomConstructionTest.java @@ -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 ) ); } diff --git a/maven-project/src/test/resources-project-builder/dependencies-with-different-versions/pom.xml b/maven-project/src/test/resources-project-builder/dependencies-with-different-versions/pom.xml new file mode 100644 index 0000000000..dad056c78d --- /dev/null +++ b/maven-project/src/test/resources-project-builder/dependencies-with-different-versions/pom.xml @@ -0,0 +1,18 @@ + + + 4.0.0 + a + b + + + commons-collections + commons-collections + 2.0 + + + commons-collections + commons-collections + 3.1 + + + \ No newline at end of file