Skip to content

Commit

Permalink
[MNG-8331] - Sometimes versioned dependencies disappear
Browse files Browse the repository at this point in the history
Avoid work when there are no dependencies
  • Loading branch information
jjkester committed Oct 19, 2024
1 parent 0e4401a commit 59c146e
Showing 1 changed file with 67 additions and 119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,35 @@
*/
package org.apache.maven.internal.impl.model;

import org.apache.maven.api.*;
import org.apache.maven.api.di.Inject;
import org.apache.maven.api.di.Named;
import org.apache.maven.api.di.Singleton;
import org.apache.maven.api.model.Dependency;
import org.apache.maven.api.model.Exclusion;
import org.apache.maven.api.model.Repository;
import org.apache.maven.api.model.*;
import org.apache.maven.api.services.*;
import org.apache.maven.api.services.BuilderProblem.Severity;
import org.apache.maven.api.services.ModelProblem.Version;
import org.apache.maven.api.services.model.*;
import org.apache.maven.api.services.xml.XmlReaderException;
import org.apache.maven.api.services.xml.XmlReaderRequest;
import org.apache.maven.api.spi.ModelParserException;
import org.apache.maven.api.spi.ModelTransformer;
import org.apache.maven.internal.impl.util.PhasingExecutor;
import org.apache.maven.model.v4.MavenTransformer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import java.util.Set;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.Executor;
Expand All @@ -49,72 +60,6 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.maven.api.Constants;
import org.apache.maven.api.RemoteRepository;
import org.apache.maven.api.Session;
import org.apache.maven.api.SessionData;
import org.apache.maven.api.Type;
import org.apache.maven.api.VersionRange;
import org.apache.maven.api.di.Inject;
import org.apache.maven.api.di.Named;
import org.apache.maven.api.di.Singleton;
import org.apache.maven.api.model.Activation;
import org.apache.maven.api.model.ActivationFile;
import org.apache.maven.api.model.Dependency;
import org.apache.maven.api.model.DependencyManagement;
import org.apache.maven.api.model.Exclusion;
import org.apache.maven.api.model.InputLocation;
import org.apache.maven.api.model.InputSource;
import org.apache.maven.api.model.Model;
import org.apache.maven.api.model.Parent;
import org.apache.maven.api.model.Profile;
import org.apache.maven.api.model.Repository;
import org.apache.maven.api.services.BuilderProblem;
import org.apache.maven.api.services.BuilderProblem.Severity;
import org.apache.maven.api.services.Interpolator;
import org.apache.maven.api.services.InterpolatorException;
import org.apache.maven.api.services.MavenException;
import org.apache.maven.api.services.ModelBuilder;
import org.apache.maven.api.services.ModelBuilderException;
import org.apache.maven.api.services.ModelBuilderRequest;
import org.apache.maven.api.services.ModelBuilderResult;
import org.apache.maven.api.services.ModelProblem;
import org.apache.maven.api.services.ModelProblem.Version;
import org.apache.maven.api.services.ModelProblemCollector;
import org.apache.maven.api.services.ModelSource;
import org.apache.maven.api.services.RepositoryFactory;
import org.apache.maven.api.services.Source;
import org.apache.maven.api.services.SuperPomProvider;
import org.apache.maven.api.services.VersionParserException;
import org.apache.maven.api.services.model.DependencyManagementImporter;
import org.apache.maven.api.services.model.DependencyManagementInjector;
import org.apache.maven.api.services.model.InheritanceAssembler;
import org.apache.maven.api.services.model.ModelCache;
import org.apache.maven.api.services.model.ModelCacheFactory;
import org.apache.maven.api.services.model.ModelInterpolator;
import org.apache.maven.api.services.model.ModelNormalizer;
import org.apache.maven.api.services.model.ModelPathTranslator;
import org.apache.maven.api.services.model.ModelProcessor;
import org.apache.maven.api.services.model.ModelResolver;
import org.apache.maven.api.services.model.ModelResolverException;
import org.apache.maven.api.services.model.ModelUrlNormalizer;
import org.apache.maven.api.services.model.ModelValidator;
import org.apache.maven.api.services.model.ModelVersionParser;
import org.apache.maven.api.services.model.PluginConfigurationExpander;
import org.apache.maven.api.services.model.PluginManagementInjector;
import org.apache.maven.api.services.model.ProfileActivationContext;
import org.apache.maven.api.services.model.ProfileInjector;
import org.apache.maven.api.services.model.ProfileSelector;
import org.apache.maven.api.services.model.RootLocator;
import org.apache.maven.api.services.xml.XmlReaderException;
import org.apache.maven.api.services.xml.XmlReaderRequest;
import org.apache.maven.api.spi.ModelParserException;
import org.apache.maven.api.spi.ModelTransformer;
import org.apache.maven.internal.impl.util.PhasingExecutor;
import org.apache.maven.model.v4.MavenTransformer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* The model builder is responsible for building the {@link Model} from the POM file.
* There are two ways to main use cases: the first one is to build the model from a POM file
Expand Down Expand Up @@ -564,19 +509,21 @@ public void mergeRepositories(List<Repository> toAdd, boolean replace) {
// Infer inner reactor dependencies version
//
Model transformFileToRaw(Model model) {
if (model.getDependencies().isEmpty()) {
return model;
}
List<Dependency> newDeps = new ArrayList<>(model.getDependencies().size());
for (Dependency dep : model.getDependencies()) {
if (dep.getVersion() == null) {
Dependency.Builder depBuilder = null;
Model depModel = getRawModel(model.getPomFile(), dep.getGroupId(), dep.getArtifactId());
if (depModel != null) {
Dependency.Builder depBuilder = Dependency.newBuilder(dep);
String version = depModel.getVersion();
InputLocation versionLocation = depModel.getLocation("version");
if (version == null && depModel.getParent() != null) {
version = depModel.getParent().getVersion();
versionLocation = depModel.getParent().getLocation("version");
}
depBuilder = Dependency.newBuilder(dep);
depBuilder.version(version).location("version", versionLocation);
if (dep.getGroupId() == null) {
String depGroupId = depModel.getGroupId();
Expand All @@ -587,8 +534,6 @@ Model transformFileToRaw(Model model) {
}
depBuilder.groupId(depGroupId).location("groupId", groupIdLocation);
}
}
if (depBuilder != null) {
newDeps.add(depBuilder.build());
} else {
newDeps.add(dep);
Expand Down Expand Up @@ -692,7 +637,7 @@ private void buildBuildPom() throws ModelBuilderException {
*
* @param r The initial DefaultModelBuilderResult object from which to generate the stream.
* @return A Stream of DefaultModelBuilderResult objects, starting with the provided result
* and including all its child results.
* and including all its child results.
*/
Stream<DefaultModelBuilderResult> results(DefaultModelBuilderResult r) {
return Stream.concat(Stream.of(r), r.getChildren().stream().flatMap(this::results));
Expand Down Expand Up @@ -1022,8 +967,8 @@ Model resolveAndReadParentExternally(Model childModel) throws ModelBuilderExcept
if (logger.isDebugEnabled()) {
logger.debug("Merging repositories from " + childModel.getId() + "\n"
+ repositories.stream()
.map(Object::toString)
.collect(Collectors.joining("\n", " ", "")));
.map(Object::toString)
.collect(Collectors.joining("\n", " ", "")));
}
}
}
Expand Down Expand Up @@ -1158,7 +1103,7 @@ private Model readEffectiveModel() throws ModelBuilderException {
String relPath;
if (parentModel.getPomFile() != null
&& (request.getRequestType() == ModelBuilderRequest.RequestType.BUILD_POM
|| request.getRequestType() == ModelBuilderRequest.RequestType.CONSUMER_POM)) {
|| request.getRequestType() == ModelBuilderRequest.RequestType.CONSUMER_POM)) {
relPath = inputModel
.getPomFile()
.getParent()
Expand Down Expand Up @@ -1412,9 +1357,9 @@ Model doReadFileModel() throws ModelBuilderException {
.parent(
model.getParent() != null
? model.getParent()
.withVersion(replaceCiFriendlyVersion(
properties,
model.getParent().getVersion()))
.withVersion(replaceCiFriendlyVersion(
properties,
model.getParent().getVersion()))
: null)
.build();
}
Expand Down Expand Up @@ -1453,7 +1398,7 @@ private Model doReadRawModel() throws ModelBuilderException {

if (!MODEL_VERSION_4_0_0.equals(rawModel.getModelVersion())
&& (request.getRequestType() == ModelBuilderRequest.RequestType.BUILD_POM
|| request.getRequestType() == ModelBuilderRequest.RequestType.CONSUMER_POM)) {
|| request.getRequestType() == ModelBuilderRequest.RequestType.CONSUMER_POM)) {
rawModel = transformFileToRaw(rawModel);
}

Expand Down Expand Up @@ -1487,35 +1432,37 @@ private Model doReadAsParentModel() throws ModelBuilderException {
Model raw = readRawModel();
Model parentData = readParent(raw);
Model parent = new DefaultInheritanceAssembler(new DefaultInheritanceAssembler.InheritanceModelMerger() {
@Override
protected void mergeModel_Modules(
Model.Builder builder,
Model target,
Model source,
boolean sourceDominant,
Map<Object, Object> context) {}

@Override
protected void mergeModel_Subprojects(
Model.Builder builder,
Model target,
Model source,
boolean sourceDominant,
Map<Object, Object> context) {}

@SuppressWarnings("deprecation")
@Override
protected void mergeModel_Profiles(
Model.Builder builder,
Model target,
Model source,
boolean sourceDominant,
Map<Object, Object> context) {
builder.profiles(Stream.concat(source.getProfiles().stream(), target.getProfiles().stream())
.map(p -> p.withModules(null).withSubprojects(null))
.toList());
}
})
@Override
protected void mergeModel_Modules(
Model.Builder builder,
Model target,
Model source,
boolean sourceDominant,
Map<Object, Object> context) {
}

@Override
protected void mergeModel_Subprojects(
Model.Builder builder,
Model target,
Model source,
boolean sourceDominant,
Map<Object, Object> context) {
}

@SuppressWarnings("deprecation")
@Override
protected void mergeModel_Profiles(
Model.Builder builder,
Model target,
Model source,
boolean sourceDominant,
Map<Object, Object> context) {
builder.profiles(Stream.concat(source.getProfiles().stream(), target.getProfiles().stream())
.map(p -> p.withModules(null).withSubprojects(null))
.toList());
}
})
.assembleModelInheritance(raw, parentData, request, this);

return parent.withParent(null);
Expand Down Expand Up @@ -1927,5 +1874,6 @@ private boolean containsCoordinates(String message, String groupId, String artif
&& (version == null || message.contains(version));
}

record GAKey(String groupId, String artifactId) {}
record GAKey(String groupId, String artifactId) {
}
}

0 comments on commit 59c146e

Please sign in to comment.