[MNG-8242] Cache flattened parents during model building (#1703)

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.
This commit is contained in:
Guillaume Nodet 2024-09-18 16:37:24 +02:00 committed by GitHub
parent 2f5f61a043
commit e1e23d9e26
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 29 additions and 1 deletions

View File

@ -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());
@ -1215,7 +1216,7 @@ public class DefaultModelBuilder implements ModelBuilder {
.source(modelSource)
.build();
Model parentModel = readRawModel(lenientRequest, problems);
Model parentModel = readParentModel(lenientRequest, problems);
if (!parent.getVersion().equals(version)) {
String rawChildModelVersion = childModel.getVersion();
@ -1245,6 +1246,33 @@ public class DefaultModelBuilder implements ModelBuilder {
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);
}