Skip to content

Commit

Permalink
[MNG-8242] Cache flattened parents during model building
Browse files Browse the repository at this point in the history
During model building, we do not cache any hierarchy inheritance due to profiles that may be activated based on properties from the child projects.
So there's no way to cache the effective parent and just inject it into the child, the whole hierarchy need to be taken into account.
However, flattening the parent (i.e. inject the flattened parent's parent into the raw parent) should provide a parent model which has no parents anymore, not activated and not interpolated, but which can be cached and reused.

# Conflicts:
#	maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/DefaultModelBuilder.java

# Conflicts:
#	maven-api-impl/src/main/java/org/apache/maven/internal/impl/resolver/DefaultModelResolver.java
#	maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuilder.java
  • Loading branch information
gnodet committed Sep 18, 2024
1 parent 8d9f8da commit 8a6b631
Showing 1 changed file with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ public class DefaultModelBuilder implements ModelBuilder {
private static final String RAW = "raw";
private static final String FILE = "file";
private static final String IMPORT = "import";
private static final String PARENT = "parent";

private final Logger logger = LoggerFactory.getLogger(getClass());

Expand Down Expand Up @@ -1215,7 +1216,7 @@ private ModelData readParentExternally(
.source(modelSource)
.build();

Model parentModel = readRawModel(lenientRequest, problems);
Model parentModel = readParentModel(lenientRequest, problems);

if (!parent.getVersion().equals(version)) {
String rawChildModelVersion = childModel.getVersion();
Expand Down Expand Up @@ -1245,6 +1246,33 @@ private ModelData readParentExternally(
return new ModelData(modelSource, parentModel);
}

Model readParentModel(ModelBuilderRequest request, DefaultModelProblemCollector problems) {
ModelSource modelSource = request.getSource();
Model model = cache(getModelCache(request), modelSource, PARENT, () -> doReadParentModel(request, problems));
return model;
}

private Model doReadParentModel(ModelBuilderRequest request, DefaultModelProblemCollector problems) {
Model raw = readRawModel(request, problems);

ModelData parentData;
if (raw.getParent() != null) {
parentData = readParentExternally(raw, request, problems);
} else {
String superModelVersion = raw.getModelVersion() != null ? raw.getModelVersion() : "4.0.0";
if (!VALID_MODEL_VERSIONS.contains(superModelVersion)) {
// Maven 3.x is always using 4.0.0 version to load the supermodel, so
// do the same when loading a dependency. The model validator will also
// check that field later.
superModelVersion = MODEL_VERSION_4_0_0;
}
parentData = new ModelData(null, getSuperModel(superModelVersion));
}

Model parent = inheritanceAssembler.assembleModelInheritance(raw, parentData.model(), request, problems);
return parent.withParent(null);
}

private Model getSuperModel(String modelVersion) {
return superPomProvider.getSuperPom(modelVersion);
}
Expand Down

0 comments on commit 8a6b631

Please sign in to comment.