diff --git a/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuilder.java b/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuilder.java index 3bcc615587..ec4d3d2f6c 100644 --- a/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuilder.java +++ b/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuilder.java @@ -17,7 +17,8 @@ package org.apache.maven.project; import java.io.File; import java.io.IOException; -import java.util.Date; +import java.util.ArrayList; +import java.util.List; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.ArtifactUtils; @@ -36,6 +37,7 @@ import org.apache.maven.model.ModelBuilder; import org.apache.maven.model.ModelBuildingException; import org.apache.maven.model.ModelBuildingRequest; import org.apache.maven.model.ModelBuildingResult; +import org.apache.maven.model.Profile; import org.apache.maven.model.io.ModelReader; import org.apache.maven.model.resolution.ModelResolver; import org.apache.maven.project.artifact.ProjectArtifact; @@ -141,7 +143,11 @@ public class DefaultProjectBuilder project.addCompileSourceRoot( build.getSourceDirectory() ); project.addTestCompileSourceRoot( build.getTestSourceDirectory() ); project.setFile( pomFile ); - project.setActiveProfiles( result.getActiveProfiles( result.getRawModel() ) ); + + List activeProfiles = new ArrayList(); + activeProfiles.addAll( result.getActivePomProfiles( result.getRawModel() ) ); + activeProfiles.addAll( result.getActiveExternalProfiles() ); + project.setActiveProfiles( activeProfiles ); return project; } diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/DefaultModelBuilder.java b/maven-model-builder/src/main/java/org/apache/maven/model/DefaultModelBuilder.java index e1ab687612..30491ff09c 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/DefaultModelBuilder.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/DefaultModelBuilder.java @@ -117,6 +117,8 @@ public class DefaultModelBuilder List activeExternalProfiles = getActiveExternalProfiles( request, profileActivationContext, problems ); + result.setActiveExternalProfiles( activeExternalProfiles ); + Model model = readModel( modelSource, pomFile, request, problems ); List rawModels = new ArrayList(); @@ -132,23 +134,22 @@ public class DefaultModelBuilder modelNormalizer.mergeDuplicates( resultModel, request ); - List activeProjectProfiles = - getActiveProjectProfiles( rawModel, profileActivationContext, problems ); + List activePomProfiles = getActivePomProfiles( rawModel, profileActivationContext, problems ); - List activeProfiles = activeProjectProfiles; - if ( current == model ) - { - activeProfiles = new ArrayList( activeProjectProfiles.size() + activeExternalProfiles.size() ); - activeProfiles.addAll( activeProjectProfiles ); - activeProfiles.addAll( activeExternalProfiles ); - } + result.setActivePomProfiles( rawModel, activePomProfiles ); - for ( Profile activeProfile : activeProfiles ) + for ( Profile activeProfile : activePomProfiles ) { profileInjector.injectProfile( resultModel, activeProfile, request ); } - result.setActiveProfiles( rawModel, activeProfiles ); + if ( current == model ) + { + for ( Profile activeProfile : activeExternalProfiles ) + { + profileInjector.injectProfile( resultModel, activeProfile, request ); + } + } configureResolver( request.getModelResolver(), resultModel, problems ); } @@ -265,8 +266,8 @@ public class DefaultModelBuilder return result.getActiveProfiles(); } - private List getActiveProjectProfiles( Model model, ProfileActivationContext context, - List problems ) + private List getActivePomProfiles( Model model, ProfileActivationContext context, + List problems ) { ProfileSelectionResult result = profileSelector.getActiveProfiles( model.getProfiles(), context ); diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/DefaultModelBuildingResult.java b/maven-model-builder/src/main/java/org/apache/maven/model/DefaultModelBuildingResult.java index 9dd0f8a1c7..7ff195d521 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/DefaultModelBuildingResult.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/DefaultModelBuildingResult.java @@ -38,12 +38,15 @@ public class DefaultModelBuildingResult private List rawModels; - private Map> activeProfiles; + private Map> activePomProfiles; + + private List activeExternalProfiles; public DefaultModelBuildingResult() { rawModels = new ArrayList(); - activeProfiles = new HashMap>(); + activePomProfiles = new HashMap>(); + activeExternalProfiles = new ArrayList(); } public Model getEffectiveModel() @@ -79,13 +82,13 @@ public class DefaultModelBuildingResult return this; } - public List getActiveProfiles( Model rawModel ) + public List getActivePomProfiles( Model rawModel ) { - List profiles = this.activeProfiles.get( rawModel ); + List profiles = this.activePomProfiles.get( rawModel ); return ( profiles == null ) ? Collections. emptyList() : Collections.unmodifiableList( profiles ); } - public DefaultModelBuildingResult setActiveProfiles( Model rawModel, List activeProfiles ) + public DefaultModelBuildingResult setActivePomProfiles( Model rawModel, List activeProfiles ) { if ( rawModel == null ) { @@ -94,32 +97,48 @@ public class DefaultModelBuildingResult if ( activeProfiles != null ) { - this.activeProfiles.put( rawModel, new ArrayList( activeProfiles ) ); + this.activePomProfiles.put( rawModel, new ArrayList( activeProfiles ) ); } else { - this.activeProfiles.remove( rawModel ); + this.activePomProfiles.remove( rawModel ); } return this; } - public DefaultModelBuildingResult addActiveProfiles( Model rawModel, List activeProfiles ) + public DefaultModelBuildingResult addActivePomProfiles( Model rawModel, List activeProfiles ) { if ( rawModel == null ) { throw new IllegalArgumentException( "no model specified" ); } - List currentProfiles = this.activeProfiles.get( rawModel ); + List currentProfiles = this.activePomProfiles.get( rawModel ); if ( currentProfiles == null ) { currentProfiles = new ArrayList( activeProfiles.size() ); - this.activeProfiles.put( rawModel, currentProfiles ); + this.activePomProfiles.put( rawModel, currentProfiles ); } currentProfiles.addAll( activeProfiles ); return this; } + public List getActiveExternalProfiles() + { + return Collections.unmodifiableList( activeExternalProfiles ); + } + + public DefaultModelBuildingResult setActiveExternalProfiles( List activeProfiles ) + { + this.activeExternalProfiles.clear(); + if ( activeProfiles != null ) + { + this.activeExternalProfiles.addAll( activeProfiles ); + } + + return this; + } + } diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/ModelBuildingResult.java b/maven-model-builder/src/main/java/org/apache/maven/model/ModelBuildingResult.java index 92484c7c08..39ee46f15b 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/ModelBuildingResult.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/ModelBuildingResult.java @@ -29,12 +29,44 @@ import java.util.List; public interface ModelBuildingResult { + /** + * Gets the fully assembled model. + * + * @return The fully assembled model, never {@code null}. + */ Model getEffectiveModel(); + /** + * Gets the raw model as it was read from the model source. Apart from basic validation, the raw model has not + * undergone any updates by the model builder, e.g. reflects neither inheritance or interpolation. + * + * @return The raw model, never {@code null}. + */ Model getRawModel(); + /** + * Gets the lineage of raw models from which the effective model was constructed. The first model is the model on + * which the model builder was originally invoked, the last model is the super POM. + * + * @return The lineage of raw models, never {@code null}. + */ List getRawModels(); - List getActiveProfiles( Model rawModel ); + /** + * Gets the profiles from the specified (raw) model that were active during model building. The input parameter + * should be a model from the collection obtained by {@link #getRawModels()}. + * + * @param rawModel The (raw) model for whose active profiles should be retrieved, must not be {@code null}. + * @return The active profiles of the model or an empty list if none, never {@code null}. + */ + List getActivePomProfiles( Model rawModel ); + + /** + * Gets the external profiles that were active during model building. External profiles are those that were + * contributed by {@link ModelBuildingRequest#getProfiles()}. + * + * @return The active external profiles or an empty list if none, never {@code null}. + */ + List getActiveExternalProfiles(); }