From 83b3a2a35f0f4528b7f8eac30cbd784c20409f42 Mon Sep 17 00:00:00 2001 From: Arnaud Roger Date: Sat, 31 Aug 2019 22:52:46 +0100 Subject: [PATCH 1/2] add dependencies only for active profile --- .../codehaus/mojo/flatten/FlattenMojo.java | 51 +++++++++++++++---- 1 file changed, 41 insertions(+), 10 deletions(-) diff --git a/src/main/java/org/codehaus/mojo/flatten/FlattenMojo.java b/src/main/java/org/codehaus/mojo/flatten/FlattenMojo.java index eeb669b6..488ef634 100644 --- a/src/main/java/org/codehaus/mojo/flatten/FlattenMojo.java +++ b/src/main/java/org/codehaus/mojo/flatten/FlattenMojo.java @@ -31,14 +31,16 @@ import org.apache.maven.model.Repository; import org.apache.maven.model.RepositoryPolicy; import org.apache.maven.model.building.DefaultModelBuilder; -import org.apache.maven.model.building.DefaultModelBuilderFactory; import org.apache.maven.model.building.DefaultModelBuildingRequest; +import org.apache.maven.model.building.ModelBuilder; import org.apache.maven.model.building.ModelBuildingException; import org.apache.maven.model.building.ModelBuildingRequest; import org.apache.maven.model.building.ModelBuildingResult; import org.apache.maven.model.building.ModelProblemCollector; +import org.apache.maven.model.building.ModelProblemCollectorRequest; import org.apache.maven.model.interpolation.ModelInterpolator; import org.apache.maven.model.io.xpp3.MavenXpp3Writer; +import org.apache.maven.model.profile.DefaultProfileActivationContext; import org.apache.maven.model.profile.ProfileActivationContext; import org.apache.maven.model.profile.ProfileInjector; import org.apache.maven.model.profile.ProfileSelector; @@ -52,7 +54,6 @@ import org.apache.maven.project.MavenProject; import org.apache.maven.shared.dependencies.resolve.DependencyResolver; import org.codehaus.mojo.flatten.cifriendly.CiInterpolator; -import org.codehaus.mojo.flatten.cifriendly.CiModelInterpolator; import org.codehaus.mojo.flatten.model.resolution.FlattenModelResolver; import org.codehaus.plexus.util.IOUtil; import org.codehaus.plexus.util.StringUtils; @@ -72,10 +73,10 @@ import java.io.Writer; import java.util.ArrayList; import java.util.Collection; +import java.util.HashSet; import java.util.List; import java.util.Properties; -import org.apache.maven.model.building.ModelBuilder; -import org.codehaus.plexus.component.annotations.Requirement; +import java.util.Set; /** * This MOJO realizes the goal flatten that generates the flattened POM and {@link #isUpdatePomFile() @@ -312,6 +313,9 @@ public class FlattenMojo @Component private DependencyResolver dependencyResolver; + @Component + private ProfileSelector profileSelector; + @Component(role = ModelBuilder.class) private DefaultModelBuilder defaultModelBuilder; @@ -480,7 +484,7 @@ protected Model createFlattenedPom( File pomFile ) } flattenedPom.setModelEncoding( modelEncoding ); - Model cleanPom = createCleanPom( effectivePom ); + Model cleanPom = createCleanPom( effectivePom, buildingRequest); FlattenDescriptor descriptor = getFlattenDescriptor(); Model originalPom = this.project.getOriginalModel(); @@ -531,9 +535,10 @@ private Model createResolvedPom( ModelBuildingRequest buildingRequest ) * to be kept in flattened POM. * * @param effectivePom is the effective POM. + * @param buildingRequest * @return the clean POM. */ - protected Model createCleanPom( Model effectivePom ) + protected Model createCleanPom(Model effectivePom, ModelBuildingRequest buildingRequest) { Model cleanPom = new Model(); @@ -587,7 +592,7 @@ protected Model createCleanPom( Model effectivePom ) } // transform dependencies... - List dependencies = createFlattenedDependencies( effectivePom ); + List dependencies = createFlattenedDependencies( effectivePom, buildingRequest ); cleanPom.setDependencies( dependencies ); return cleanPom; } @@ -846,7 +851,7 @@ protected static boolean isBuildTimeDriven( Activation activation ) * @param effectiveModel is the effective POM {@link Model} to process. * @return the {@link List} of {@link Dependency dependencies}. */ - protected List createFlattenedDependencies( Model effectiveModel ) + protected List createFlattenedDependencies(final Model effectiveModel, final ModelBuildingRequest modelBuildingRequest ) { List flattenedDependencies = new ArrayList(); @@ -854,13 +859,16 @@ protected List createFlattenedDependencies( Model effectiveModel ) createFlattenedDependencies( effectiveModel, flattenedDependencies ); if ( isEmbedBuildProfileDependencies() ) { - Model projectModel = this.project.getModel(); + final Model projectModel = this.project.getModel(); Dependencies modelDependencies = new Dependencies(); modelDependencies.addAll( projectModel.getDependencies() ); + + Set activeProfile = resolveActiveProfileIds(effectiveModel, modelBuildingRequest); + for ( Profile profile : projectModel.getProfiles() ) { // build-time driven activation (by property or file)? - if ( isBuildTimeDriven( profile.getActivation() ) ) + if ( isBuildTimeDriven( profile.getActivation() ) && activeProfile.contains(profile.getId())) { List profileDependencies = profile.getDependencies(); for ( Dependency profileDependency : profileDependencies ) @@ -882,6 +890,29 @@ protected List createFlattenedDependencies( Model effectiveModel ) return flattenedDependencies; } + private Set resolveActiveProfileIds(final Model effectiveModel, final ModelBuildingRequest buildingRequest) { + DefaultProfileActivationContext context = new DefaultProfileActivationContext(); + context.setActiveProfileIds(buildingRequest.getActiveProfileIds()); + context.setInactiveProfileIds(buildingRequest.getInactiveProfileIds()); + context.setSystemProperties(buildingRequest.getSystemProperties()); + context.setUserProperties(buildingRequest.getUserProperties()); + context.setProjectDirectory(buildingRequest.getPomFile() != null ? buildingRequest.getPomFile().getParentFile() : null); + + List activeProfiles = profileSelector.getActiveProfiles(effectiveModel.getProfiles(), context, new ModelProblemCollector() { + @Override + public void add(ModelProblemCollectorRequest modelProblemCollectorRequest) { + + } + }); + + + Set activeProfile = new HashSet(); + for(Profile p : activeProfiles) { + activeProfile.add(p.getId()); + } + return activeProfile; + } + /** * Collects the resolved {@link Dependency dependencies} from the given effectiveModel. * From b2010203e3d0709e726a4f70826b5e6bed3f2bc5 Mon Sep 17 00:00:00 2001 From: Arnaud Roger Date: Sun, 1 Sep 2019 17:24:22 +0100 Subject: [PATCH 2/2] override dependency if management key matches --- .../codehaus/mojo/flatten/FlattenMojo.java | 26 ++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/codehaus/mojo/flatten/FlattenMojo.java b/src/main/java/org/codehaus/mojo/flatten/FlattenMojo.java index 488ef634..923809d0 100644 --- a/src/main/java/org/codehaus/mojo/flatten/FlattenMojo.java +++ b/src/main/java/org/codehaus/mojo/flatten/FlattenMojo.java @@ -880,7 +880,12 @@ protected List createFlattenedDependencies(final Model effectiveMode // Non build-time driven profiles will remain in the flattened POM with their dependencies // and // allow dynamic dependencies due to OS or JDK. - flattenedDependencies.add( profileDependency ); + int depIndex = findDependencyToOverride(flattenedDependencies, profileDependency); + if (depIndex == -1) { + flattenedDependencies.add(profileDependency); + } else { + flattenedDependencies.set(depIndex, profileDependency); + } } } } @@ -890,6 +895,25 @@ protected List createFlattenedDependencies(final Model effectiveMode return flattenedDependencies; } + private int findDependencyToOverride(List dependencies, Dependency override) { + + for( int i = 0; i < dependencies.size(); i++ ) + { + Dependency candidate = dependencies.get(i); + if (isOverrideOf(override, candidate)) + { + return i; + } + } + return -1; + } + + private boolean isOverrideOf(Dependency override, Dependency candidate) { + Dependency clone = candidate.clone(); + clone.setVersion(override.getVersion()); + return clone.getManagementKey().equals(override.getManagementKey()); + } + private Set resolveActiveProfileIds(final Model effectiveModel, final ModelBuildingRequest buildingRequest) { DefaultProfileActivationContext context = new DefaultProfileActivationContext(); context.setActiveProfileIds(buildingRequest.getActiveProfileIds());