Skip to content

Commit

Permalink
Get rid of DefaultModelRepositoryHolder
Browse files Browse the repository at this point in the history
# 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/model/DefaultModelBuilder.java
  • Loading branch information
gnodet committed Sep 19, 2024
1 parent a6384bc commit 97e54bd
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 156 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@
* Request used to build a {@link org.apache.maven.api.Project} using
* the {@link ProjectBuilder} service.
*
* TODO: replace ModelRepositoryHolder with just the enum for the strategy
*
* @since 4.0.0
*/
@Experimental
Expand Down Expand Up @@ -128,9 +126,6 @@ enum RepositoryMerging {
@Nonnull
ModelResolver getModelResolver();

@Nonnull
Object getModelRepositoryHolder();

@Nonnull
RepositoryMerging getRepositoryMerging();

Expand Down Expand Up @@ -189,7 +184,6 @@ class ModelBuilderRequestBuilder {
Map<String, String> systemProperties;
Map<String, String> userProperties;
ModelResolver modelResolver;
Object modelRepositoryHolder;
RepositoryMerging repositoryMerging;
Object listener;
ModelBuilderResult interimResult;
Expand All @@ -209,7 +203,6 @@ class ModelBuilderRequestBuilder {
this.systemProperties = request.getSystemProperties();
this.userProperties = request.getUserProperties();
this.modelResolver = request.getModelResolver();
this.modelRepositoryHolder = request.getModelRepositoryHolder();
this.repositoryMerging = request.getRepositoryMerging();
this.listener = request.getListener();
this.interimResult = request.getInterimResult();
Expand Down Expand Up @@ -271,11 +264,6 @@ public ModelBuilderRequestBuilder modelResolver(ModelResolver modelResolver) {
return this;
}

public ModelBuilderRequestBuilder modelRepositoryHolder(Object modelRepositoryHolder) {
this.modelRepositoryHolder = modelRepositoryHolder;
return this;
}

public ModelBuilderRequestBuilder repositoryMerging(RepositoryMerging repositoryMerging) {
this.repositoryMerging = repositoryMerging;
return this;
Expand Down Expand Up @@ -309,7 +297,6 @@ public ModelBuilderRequest build() {
systemProperties,
userProperties,
modelResolver,
modelRepositoryHolder,
repositoryMerging,
listener,
interimResult,
Expand All @@ -327,7 +314,6 @@ private static class DefaultModelBuilderRequest extends BaseRequest implements M
private final Map<String, String> systemProperties;
private final Map<String, String> userProperties;
private final ModelResolver modelResolver;
private final Object modelRepositoryHolder;
private final RepositoryMerging repositoryMerging;
private final Object listener;
private final ModelBuilderResult interimResult;
Expand All @@ -346,7 +332,6 @@ private static class DefaultModelBuilderRequest extends BaseRequest implements M
Map<String, String> systemProperties,
Map<String, String> userProperties,
ModelResolver modelResolver,
Object modelRepositoryHolder,
RepositoryMerging repositoryMerging,
Object listener,
ModelBuilderResult interimResult,
Expand All @@ -363,7 +348,6 @@ private static class DefaultModelBuilderRequest extends BaseRequest implements M
systemProperties != null ? Map.copyOf(systemProperties) : session.getSystemProperties();
this.userProperties = userProperties != null ? Map.copyOf(userProperties) : session.getUserProperties();
this.modelResolver = modelResolver;
this.modelRepositoryHolder = modelRepositoryHolder;
this.repositoryMerging = repositoryMerging;
this.listener = listener;
this.interimResult = interimResult;
Expand Down Expand Up @@ -421,11 +405,6 @@ public ModelResolver getModelResolver() {
return modelResolver;
}

@Override
public Object getModelRepositoryHolder() {
return modelRepositoryHolder;
}

@Override
public RepositoryMerging getRepositoryMerging() {
return repositoryMerging;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.maven.api.RemoteRepository;
import org.apache.maven.api.Session;
import org.apache.maven.api.SessionData;
import org.apache.maven.api.Type;
Expand All @@ -69,6 +70,7 @@
import org.apache.maven.api.model.Plugin;
import org.apache.maven.api.model.PluginManagement;
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.ModelBuilder;
Expand All @@ -80,6 +82,7 @@
import org.apache.maven.api.services.ModelResolver;
import org.apache.maven.api.services.ModelResolverException;
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;
Expand Down Expand Up @@ -108,7 +111,6 @@
import org.apache.maven.api.spi.ModelParserException;
import org.apache.maven.api.spi.ModelTransformer;
import org.apache.maven.api.spi.ModelTransformerException;
import org.apache.maven.internal.impl.resolver.DefaultModelRepositoryHolder;
import org.apache.maven.internal.impl.resolver.DefaultModelResolver;
import org.apache.maven.model.v4.MavenTransformer;
import org.codehaus.plexus.interpolation.InterpolationException;
Expand Down Expand Up @@ -234,6 +236,7 @@ protected final class DefaultModelBuilderSession implements ModelProblemCollecto
final Map<Path, Holder> modelByPath;
final Map<GAKey, Holder> modelByGA;
final Map<GAKey, Set<ModelSource>> mappedSources;
final RepositoryHolder repositoryHolder;

private String source;
private Model sourceModel;
Expand Down Expand Up @@ -270,7 +273,8 @@ protected final class DefaultModelBuilderSession implements ModelProblemCollecto
new Graph(),
new ConcurrentHashMap<>(64),
new ConcurrentHashMap<>(64),
new ConcurrentHashMap<>(64));
new ConcurrentHashMap<>(64),
null);
}

@SuppressWarnings("checkstyle:ParameterNumber")
Expand All @@ -282,7 +286,8 @@ private DefaultModelBuilderSession(
Graph dag,
Map<Path, Holder> modelByPath,
Map<GAKey, Holder> modelByGA,
Map<GAKey, Set<ModelSource>> mappedSources) {
Map<GAKey, Set<ModelSource>> mappedSources,
RepositoryHolder repositoryHolder) {
this.session = session;
this.request = request;
this.result = result;
Expand All @@ -291,6 +296,12 @@ private DefaultModelBuilderSession(
this.modelByPath = modelByPath;
this.modelByGA = modelByGA;
this.mappedSources = mappedSources;
this.repositoryHolder = repositoryHolder != null
? repositoryHolder
: new RepositoryHolder(
request.getRepositories() != null
? request.getRepositories()
: session.getRemoteRepositories());
this.result.getProblems().forEach(p -> severities.add(p.getSeverity()));
}

Expand All @@ -303,7 +314,7 @@ public DefaultModelBuilderSession derive(ModelBuilderRequest request, DefaultMod
throw new IllegalArgumentException("Session mismatch");
}
return new DefaultModelBuilderSession(
session, request, result, cache, dag, modelByPath, modelByGA, mappedSources);
session, request, result, cache, dag, modelByPath, modelByGA, mappedSources, repositoryHolder);
}

public Session session() {
Expand Down Expand Up @@ -655,6 +666,70 @@ public ModelBuilderException newModelBuilderException() {
}
return new ModelBuilderException(result);
}

List<RemoteRepository> getRepositories() {
return repositoryHolder.getRepositories();
}

void mergeRepositories(List<Repository> repositories, boolean replace) {
repositoryHolder.mergeRepositories(repositories, replace);
}

class RepositoryHolder {

List<RemoteRepository> pomRepositories;
List<RemoteRepository> repositories;
List<RemoteRepository> externalRepositories;

RepositoryHolder(List<RemoteRepository> externalRepositories) {
this.pomRepositories = List.of();
this.externalRepositories = List.copyOf(externalRepositories);
this.repositories = List.copyOf(externalRepositories);
}

RepositoryHolder(RepositoryHolder holder) {
this.pomRepositories = List.copyOf(holder.pomRepositories);
this.externalRepositories = List.copyOf(holder.externalRepositories);
this.repositories = List.copyOf(holder.repositories);
}

public void mergeRepositories(List<Repository> toAdd, boolean replace) {
List<RemoteRepository> repos =
toAdd.stream().map(session::createRemoteRepository).toList();
if (replace) {
Set<String> ids =
repos.stream().map(RemoteRepository::getId).collect(Collectors.toSet());
repositories = repositories.stream()
.filter(r -> !ids.contains(r.getId()))
.toList();
pomRepositories = pomRepositories.stream()
.filter(r -> !ids.contains(r.getId()))
.toList();
} else {
Set<String> ids = pomRepositories.stream()
.map(RemoteRepository::getId)
.collect(Collectors.toSet());
repos = repos.stream().filter(r -> !ids.contains(r.getId())).toList();
}

RepositoryFactory repositoryFactory = session.getService(RepositoryFactory.class);
if (request.getRepositoryMerging() == ModelBuilderRequest.RepositoryMerging.REQUEST_DOMINANT) {
repositories = repositoryFactory.aggregate(session, repositories, repos, true);
pomRepositories = repositories;
} else {
pomRepositories = repositoryFactory.aggregate(session, pomRepositories, repos, true);
repositories = repositoryFactory.aggregate(session, pomRepositories, externalRepositories, false);
}
}

public List<RemoteRepository> getRepositories() {
return repositories;
}

public RepositoryHolder copy() {
return new RepositoryHolder(this);
}
}
}

DefaultModelProblemCollector newCollector(DefaultModelBuilderResult result) {
Expand All @@ -663,16 +738,6 @@ DefaultModelProblemCollector newCollector(DefaultModelBuilderResult result) {

private static ModelBuilderRequest fillRequestDefaults(ModelBuilderRequest request) {
ModelBuilderRequest.ModelBuilderRequestBuilder builder = ModelBuilderRequest.builder(request);
if (getModelRepositoryHolder(request) == null) {
builder.modelRepositoryHolder(new DefaultModelRepositoryHolder(
request.getSession(),
request.getRepositoryMerging() != null
? request.getRepositoryMerging()
: ModelBuilderRequest.RepositoryMerging.POM_DOMINANT,
request.getRepositories() != null
? request.getRepositories()
: request.getSession().getRemoteRepositories()));
}
if (request.getModelResolver() == null) {
builder.modelResolver(new DefaultModelResolver());
}
Expand Down Expand Up @@ -820,14 +885,11 @@ private Model readEffectiveModel(final DefaultModelBuilderSession build) throws

// add repositories specified by the current model so that we can resolve the parent
if (!model.getRepositories().isEmpty()) {
DefaultModelRepositoryHolder modelRepositoryHolder = getModelRepositoryHolder(request);
List<String> oldRepos = modelRepositoryHolder.getRepositories().stream()
.map(Object::toString)
.toList();
modelRepositoryHolder.merge(model.getRepositories(), false);
List<String> newRepos = modelRepositoryHolder.getRepositories().stream()
.map(Object::toString)
.toList();
List<String> oldRepos =
build.getRepositories().stream().map(Object::toString).toList();
build.mergeRepositories(model.getRepositories(), false);
List<String> newRepos =
build.getRepositories().stream().map(Object::toString).toList();
if (!Objects.equals(oldRepos, newRepos)) {
logger.debug("Merging repositories from " + model.getId() + "\n"
+ newRepos.stream().map(s -> " " + s).collect(Collectors.joining("\n")));
Expand Down Expand Up @@ -887,14 +949,11 @@ private Model readEffectiveModel(final DefaultModelBuilderSession build) throws

// Now the fully interpolated model is available: reconfigure the resolver
if (!resultModel.getRepositories().isEmpty()) {
DefaultModelRepositoryHolder modelRepositoryHolder = getModelRepositoryHolder(request);
List<String> oldRepos = modelRepositoryHolder.getRepositories().stream()
.map(Object::toString)
.toList();
modelRepositoryHolder.merge(resultModel.getRepositories(), true);
List<String> newRepos = modelRepositoryHolder.getRepositories().stream()
.map(Object::toString)
.toList();
List<String> oldRepos =
build.getRepositories().stream().map(Object::toString).toList();
build.mergeRepositories(resultModel.getRepositories(), true);
List<String> newRepos =
build.getRepositories().stream().map(Object::toString).toList();
if (!Objects.equals(oldRepos, newRepos)) {
logger.debug("Replacing repositories from " + resultModel.getId() + "\n"
+ newRepos.stream().map(s -> " " + s).collect(Collectors.joining("\n")));
Expand Down Expand Up @@ -1580,8 +1639,7 @@ private ModelData resolveAndReadParentExternally(DefaultModelBuilderSession buil
ModelSource modelSource;
try {
AtomicReference<Parent> modified = new AtomicReference<>();
modelSource = modelResolver.resolveModel(
request.getSession(), getModelRepositoryHolder(request).getRepositories(), parent, modified);
modelSource = modelResolver.resolveModel(request.getSession(), build.getRepositories(), parent, modified);
if (modified.get() != null) {
parent = modified.get();
}
Expand Down Expand Up @@ -1825,10 +1883,7 @@ private Model doLoadDependencyManagement(
final ModelSource importSource;
try {
importSource = modelResolver.resolveModel(
request.getSession(),
getModelRepositoryHolder(request).getRepositories(),
dependency,
new AtomicReference<>());
request.getSession(), build.getRepositories(), dependency, new AtomicReference<>());
} catch (ModelBuilderException e) {
StringBuilder buffer = new StringBuilder(256);
buffer.append("Non-resolvable import POM");
Expand Down Expand Up @@ -1867,9 +1922,8 @@ private Model doLoadDependencyManagement(
.userProperties(request.getUserProperties())
.source(importSource)
.modelResolver(modelResolver)
.modelRepositoryHolder(getModelRepositoryHolder(request).copy())
.twoPhaseBuilding(false)
.repositories(getModelRepositoryHolder(request).getRepositories())
.repositories(build.getRepositories())
.build();
importResult = build(new DefaultModelBuilderSession(importRequest), importIds);
} catch (ModelBuilderException e) {
Expand All @@ -1884,10 +1938,6 @@ private Model doLoadDependencyManagement(
return importModel;
}

private static DefaultModelRepositoryHolder getModelRepositoryHolder(ModelBuilderRequest request) {
return (DefaultModelRepositoryHolder) request.getModelRepositoryHolder();
}

private static <T> T cache(
ModelCache cache, String groupId, String artifactId, String version, String tag, Callable<T> supplier) {
return cache.computeIfAbsent(groupId, artifactId, version, tag, asSupplier(supplier));
Expand Down
Loading

0 comments on commit 97e54bd

Please sign in to comment.