[MNG-7880] Trim down consumer POM (#1247)

This commit is contained in:
Guillaume Nodet 2023-09-26 08:51:42 +02:00 committed by GitHub
parent 10fa5c713a
commit 558dfc9c6e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 82 additions and 36 deletions

View File

@ -32,11 +32,17 @@
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.stream.Collectors;
import org.apache.maven.api.Repository;
import org.apache.maven.api.feature.Features;
import org.apache.maven.api.model.DistributionManagement;
import org.apache.maven.api.model.Model;
import org.apache.maven.api.model.ModelBase;
import org.apache.maven.api.model.Profile;
import org.apache.maven.model.building.FileModelSource;
import org.apache.maven.model.building.ModelBuilder;
import org.apache.maven.model.building.ModelBuildingRequest;
@ -212,6 +218,8 @@ void transform(Path src, Path dest, Model model) {
Model consumer = null;
String version;
String packaging = model.getPackaging();
if (POM_PACKAGING.equals(packaging)) {
// This is a bit of a hack, but all models are cached, so not sure why we'd need to parse it again
ModelCache cache = DefaultModelCache.newInstance(session);
Object modelData = cache.get(new FileModelSource(src.toFile()), "raw");
@ -241,9 +249,6 @@ void transform(Path src, Path dest, Model model) {
}
// raw to consumer transform
if (BOM_PACKAGING.equals(consumer.getPackaging())) {
consumer = consumer.withPackaging(POM_PACKAGING);
}
consumer = consumer.withRoot(false).withModules(null);
if (consumer.getParent() != null) {
consumer = consumer.withParent(consumer.getParent().withRelativePath(null));
@ -256,6 +261,25 @@ void transform(Path src, Path dest, Model model) {
} else {
version = consumer.getModelVersion();
}
} else {
Model.Builder builder = prune(
Model.newBuilder(model, true)
.preserveModelVersion(false)
.root(false)
.parent(null)
.build(null),
model);
boolean isBom = BOM_PACKAGING.equals(packaging);
if (isBom) {
builder.packaging(POM_PACKAGING);
}
builder.profiles(model.getProfiles().stream()
.map(p -> prune(Profile.newBuilder(p, true), p).build())
.collect(Collectors.toList()));
consumer = builder.build();
version = new MavenModelVersion().getModelVersion(consumer);
consumer = consumer.withModelVersion(version);
}
try {
Files.createDirectories(dest.getParent());
@ -271,4 +295,26 @@ void transform(Path src, Path dest, Model model) {
}
}
}
private static <T extends ModelBase.Builder> T prune(T builder, ModelBase model) {
builder.properties(null).reporting(null);
if (model.getDistributionManagement() != null
&& model.getDistributionManagement().getRelocation() != null) {
// keep relocation only
builder.distributionManagement(DistributionManagement.newBuilder()
.relocation(model.getDistributionManagement().getRelocation())
.build());
}
// only keep repositories others than 'central'
builder.pluginRepositories(pruneRepositories(model.getPluginRepositories()));
builder.repositories(pruneRepositories(model.getRepositories()));
return builder;
}
private static List<org.apache.maven.api.model.Repository> pruneRepositories(
List<org.apache.maven.api.model.Repository> repositories) {
return repositories.stream()
.filter(r -> !Repository.CENTRAL_ID.equals(r.getId()))
.collect(Collectors.toList());
}
}