From 4c95a50332f9f0acd853d977a618629431430e4d Mon Sep 17 00:00:00 2001 From: Tamas Cservenak Date: Thu, 1 Dec 2022 12:51:18 +0100 Subject: [PATCH] [MNG-6609] Profile activation based on packaging (#883) In short: do NOT set request.setRawModel as NOTHING sets it. The Maven4 vs Maven3 is different, in Maven 3 NOTHING calls request.setRawModel Full explanation: as ModelBuildingRequest is REUSED, and nothing sets this value in Maven3, once you set it here (as in original PR https://github.com/apache/maven/pull/849 ) results in awkward situation in Maven3: it will not load any other model... --- https://issues.apache.org/jira/browse/MNG-6609 --- .../model/building/DefaultModelBuilder.java | 24 +++++++++++-------- .../profile/ProfileActivationContext.java | 6 +++++ 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java b/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java index a2faac3000..5c79caea46 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java @@ -276,8 +276,15 @@ public class DefaultModelBuilder DefaultModelProblemCollector problems = new DefaultModelProblemCollector( result ); + // read and validate raw model + Model inputModel = request.getRawModel(); + if ( inputModel == null ) + { + inputModel = readModel( request.getModelSource(), request.getPomFile(), request, problems ); + } + // profile activation - DefaultProfileActivationContext profileActivationContext = getProfileActivationContext( request ); + DefaultProfileActivationContext profileActivationContext = getProfileActivationContext( request, inputModel ); problems.setSource( "(external profiles)" ); List activeExternalProfiles = profileSelector.getActiveProfiles( request.getProfiles(), @@ -296,13 +303,6 @@ public class DefaultModelBuilder profileActivationContext.setUserProperties( profileProps ); } - // read and validate raw model - Model inputModel = request.getRawModel(); - if ( inputModel == null ) - { - inputModel = readModel( request.getModelSource(), request.getPomFile(), request, problems ); - } - problems.setRootModel( inputModel ); ModelData resultData = new ModelData( request.getModelSource(), inputModel ); @@ -700,14 +700,18 @@ public class DefaultModelBuilder return model; } - private DefaultProfileActivationContext getProfileActivationContext( ModelBuildingRequest request ) + private DefaultProfileActivationContext getProfileActivationContext( ModelBuildingRequest request, Model rawModel ) { DefaultProfileActivationContext context = new DefaultProfileActivationContext(); context.setActiveProfileIds( request.getActiveProfileIds() ); context.setInactiveProfileIds( request.getInactiveProfileIds() ); context.setSystemProperties( request.getSystemProperties() ); - context.setUserProperties( request.getUserProperties() ); + // enrich user properties with project packaging + Properties userProperties = request.getUserProperties(); + userProperties.computeIfAbsent( (Object) ProfileActivationContext.PROPERTY_NAME_PACKAGING, + ( p ) -> (Object) rawModel.getPackaging() ); + context.setUserProperties( userProperties ); context.setProjectDirectory( ( request.getPomFile() != null ) ? request.getPomFile().getParentFile() : null ); return context; diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/profile/ProfileActivationContext.java b/maven-model-builder/src/main/java/org/apache/maven/model/profile/ProfileActivationContext.java index d501e660a5..cd6bd48aca 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/profile/ProfileActivationContext.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/profile/ProfileActivationContext.java @@ -30,6 +30,12 @@ import java.util.Map; */ public interface ProfileActivationContext { + /** + * Key of the property containing the project's packaging. + * Available in {@link #getUserProperties()}. + * @since 3.9 + */ + String PROPERTY_NAME_PACKAGING = "packaging"; /** * Gets the identifiers of those profiles that should be activated by explicit demand.