From 2b13a43c09896ffbdb10dc247a618b6b4fd97f85 Mon Sep 17 00:00:00 2001 From: Guillaume Nodet Date: Thu, 12 Sep 2024 08:19:14 +0200 Subject: [PATCH] [MNG-8235] Move remote repositories into requests (#1712) --- .../java/org/apache/maven/api/Session.java | 59 ++++++++++++++++++- .../maven/api/services/ArtifactResolver.java | 20 +++++++ .../api/services/ArtifactResolverRequest.java | 41 ++++++++++++- .../services/DependencyResolverRequest.java | 24 +++++++- .../api/services/ModelBuilderRequest.java | 24 +++++++- .../api/services/ModelBuilderResult.java | 2 +- .../maven/api/services/ModelResolver.java | 18 +++++- .../api/services/ProjectBuilderRequest.java | 24 +++++++- .../api/services/VersionRangeResolver.java | 27 +++++++++ .../services/VersionRangeResolverRequest.java | 37 +++++++++++- .../api/services/VersionResolverRequest.java | 40 ++++++++++++- .../maven/internal/impl/AbstractSession.java | 49 +++++++++++++++ .../impl/DefaultArtifactResolver.java | 3 +- .../impl/DefaultDependencyResolver.java | 11 +++- .../impl/DefaultVersionRangeResolver.java | 5 +- .../internal/impl/DefaultVersionResolver.java | 5 +- .../impl/model/DefaultModelBuilder.java | 20 +++---- .../DefaultArtifactDescriptorReader.java | 42 ++++++------- .../impl/resolver/DefaultModelResolver.java | 19 +++--- .../resolver/DefaultModelResolverTest.java | 20 +++---- .../internal/impl/DefaultProjectBuilder.java | 7 ++- .../internal/DefaultMavenPluginManager.java | 5 -- .../maven/project/DefaultProjectBuilder.java | 38 ++++++++---- .../impl/ConsumerPomBuilderTest.java | 9 ++- 24 files changed, 457 insertions(+), 92 deletions(-) diff --git a/api/maven-api-core/src/main/java/org/apache/maven/api/Session.java b/api/maven-api-core/src/main/java/org/apache/maven/api/Session.java index bc20c35b7d..26eeaf118d 100644 --- a/api/maven-api-core/src/main/java/org/apache/maven/api/Session.java +++ b/api/maven-api-core/src/main/java/org/apache/maven/api/Session.java @@ -38,7 +38,6 @@ import org.apache.maven.api.settings.Settings; /** * The session to install / deploy / resolve artifacts and dependencies. * - * TODO: move the remote repositories in the requests (plugins will need to access this list somehow) * TODO: add request trace so that requests can be linked together and through the resolver * TODO: add a Request interface holding session + parent request * @@ -425,6 +424,19 @@ public interface Session { @Nonnull DownloadedArtifact resolveArtifact(@Nonnull ArtifactCoordinates coordinates); + /** + * Shortcut for {@code getService(ArtifactResolver.class).resolve(...)}. + * + * @param coordinates coordinates of the artifact to resolve + * @param repositories repositories to use, if {@code null}, the session repositories are used + * @return requested artifact together with the path to its file + * @throws org.apache.maven.api.services.ArtifactResolverException if the artifact resolution failed + * + * @see org.apache.maven.api.services.ArtifactResolver#resolve(Session, Collection) + */ + @Nonnull + DownloadedArtifact resolveArtifact(@Nonnull ArtifactCoordinates coordinates, List repositories); + /** * Shortcut for {@code getService(ArtifactResolver.class).resolve(...)}. * @@ -449,6 +461,21 @@ public interface Session { @Nonnull Collection resolveArtifacts(@Nonnull Collection coordinates); + /** + * Shortcut for {@code getService(ArtifactResolver.class).resolve(...)}. + * + * @param coordinates coordinates of all artifacts to resolve + * @param repositories repositories to use, if {@code null}, the session repositories are used + * @return requested artifacts together with the paths to their files + * @throws org.apache.maven.api.services.ArtifactResolverException if the artifact resolution failed + * + * @see org.apache.maven.api.services.ArtifactResolver#resolve(Session, Collection) + */ + @Nonnull + Collection resolveArtifacts( + @Nonnull Collection coordinates, + @Nullable List repositories); + /** * Shortcut for {@code getService(ArtifactResolver.class).resolve(...)}. * @@ -461,6 +488,19 @@ public interface Session { @Nonnull DownloadedArtifact resolveArtifact(@Nonnull Artifact artifact); + /** + * Shortcut for {@code getService(ArtifactResolver.class).resolve(...)}. + * + * @param artifact the artifact to resolve + * @param repositories repositories to use, if {@code null}, the session repositories are used + * @return requested artifact together with the path to its file + * @throws org.apache.maven.api.services.ArtifactResolverException if the artifact resolution failed + * + * @see org.apache.maven.api.services.ArtifactResolver#resolve(Session, Collection) + */ + @Nonnull + DownloadedArtifact resolveArtifact(@Nonnull Artifact artifact, @Nullable List repositories); + /** * Shortcut for {@code getService(ArtifactResolver.class).resolve(...)}. * @@ -717,6 +757,23 @@ public interface Session { @Nonnull List resolveVersionRange(@Nonnull ArtifactCoordinates artifact); + /** + * Expands a version range to a list of matching versions, in ascending order. + * For example, resolves "[3.8,4.0)" to "3.8", "3.8.1", "3.8.2". + * The returned list of versions is only dependent on the configured repositories and their contents. + * The supplied request may also refer to a single concrete version rather than a version range. + * In this case though, the result contains simply the (parsed) input version, regardless of the + * repositories and their contents. + * + * @param artifact the artifact for which to resolve the versions + * @param repositories the repositories to use, or the session repositories if {@code null} + * @return a list of resolved {@code Version}s. + * @throws org.apache.maven.api.services.VersionRangeResolverException if the resolution failed + * @see org.apache.maven.api.services.VersionRangeResolver#resolve(Session, ArtifactCoordinates) (String) + */ + @Nonnull + List resolveVersionRange(@Nonnull ArtifactCoordinates artifact, List repositories); + /** * Parses the specified version string, for example "1.0". *

diff --git a/api/maven-api-core/src/main/java/org/apache/maven/api/services/ArtifactResolver.java b/api/maven-api-core/src/main/java/org/apache/maven/api/services/ArtifactResolver.java index 31e2d8d276..c4c413c547 100644 --- a/api/maven-api-core/src/main/java/org/apache/maven/api/services/ArtifactResolver.java +++ b/api/maven-api-core/src/main/java/org/apache/maven/api/services/ArtifactResolver.java @@ -19,8 +19,10 @@ package org.apache.maven.api.services; import java.util.Collection; +import java.util.List; import org.apache.maven.api.ArtifactCoordinates; +import org.apache.maven.api.RemoteRepository; import org.apache.maven.api.Service; import org.apache.maven.api.Session; import org.apache.maven.api.annotations.Experimental; @@ -55,4 +57,22 @@ public interface ArtifactResolver extends Service { default ArtifactResolverResult resolve(Session session, Collection coordinates) { return resolve(ArtifactResolverRequest.build(session, coordinates)); } + + /** + * Resolves several artifacts from their coordinates. + * + * @param session {@link Session} + * @param repositories the list of remote repositories or {@code null} to use the session repositories + * @param coordinates array of {@link ArtifactCoordinates} + * @return {@link ArtifactResolverResult} + * @throws ArtifactResolverException in case of an error. + * @throws IllegalArgumentException in case of parameter {@code buildingRequest} is {@code null} or + * parameter {@code coordinates} is {@code null} or invalid + */ + default ArtifactResolverResult resolve( + Session session, + Collection coordinates, + List repositories) { + return resolve(ArtifactResolverRequest.build(session, coordinates, repositories)); + } } diff --git a/api/maven-api-core/src/main/java/org/apache/maven/api/services/ArtifactResolverRequest.java b/api/maven-api-core/src/main/java/org/apache/maven/api/services/ArtifactResolverRequest.java index 7cfbcbc7c4..6fa14cc4fb 100644 --- a/api/maven-api-core/src/main/java/org/apache/maven/api/services/ArtifactResolverRequest.java +++ b/api/maven-api-core/src/main/java/org/apache/maven/api/services/ArtifactResolverRequest.java @@ -19,13 +19,16 @@ package org.apache.maven.api.services; import java.util.Collection; +import java.util.List; import org.apache.maven.api.ArtifactCoordinates; +import org.apache.maven.api.RemoteRepository; import org.apache.maven.api.Session; import org.apache.maven.api.annotations.Experimental; import org.apache.maven.api.annotations.Immutable; import org.apache.maven.api.annotations.Nonnull; import org.apache.maven.api.annotations.NotThreadSafe; +import org.apache.maven.api.annotations.Nullable; import static org.apache.maven.api.services.BaseRequest.nonNull; @@ -43,6 +46,9 @@ public interface ArtifactResolverRequest { @Nonnull Collection getCoordinates(); + @Nonnull + List getRepositories(); + @Nonnull static ArtifactResolverRequestBuilder builder() { return new ArtifactResolverRequestBuilder(); @@ -57,10 +63,23 @@ public interface ArtifactResolverRequest { .build(); } + @Nonnull + static ArtifactResolverRequest build( + @Nonnull Session session, + @Nonnull Collection coordinates, + List repositories) { + return builder() + .session(nonNull(session, "session cannot be null")) + .coordinates(nonNull(coordinates, "coordinates cannot be null")) + .repositories(repositories) + .build(); + } + @NotThreadSafe class ArtifactResolverRequestBuilder { Session session; Collection coordinates; + List repositories; ArtifactResolverRequestBuilder() {} @@ -76,19 +95,31 @@ public interface ArtifactResolverRequest { return this; } + @Nonnull + public ArtifactResolverRequestBuilder repositories(List repositories) { + this.repositories = repositories; + return this; + } + @Nonnull public ArtifactResolverRequest build() { - return new DefaultArtifactResolverRequest(session, coordinates); + return new DefaultArtifactResolverRequest(session, coordinates, repositories); } private static class DefaultArtifactResolverRequest extends BaseRequest implements ArtifactResolverRequest { @Nonnull private final Collection coordinates; + @Nullable + private final List repositories; + DefaultArtifactResolverRequest( - @Nonnull Session session, @Nonnull Collection coordinates) { + @Nonnull Session session, + @Nonnull Collection coordinates, + @Nonnull List repositories) { super(session); this.coordinates = unmodifiable(nonNull(coordinates, "coordinates cannot be null")); + this.repositories = repositories; } @Nonnull @@ -96,6 +127,12 @@ public interface ArtifactResolverRequest { public Collection getCoordinates() { return coordinates; } + + @Nullable + @Override + public List getRepositories() { + return repositories; + } } } } diff --git a/api/maven-api-core/src/main/java/org/apache/maven/api/services/DependencyResolverRequest.java b/api/maven-api-core/src/main/java/org/apache/maven/api/services/DependencyResolverRequest.java index 6b18394f96..928f99a742 100644 --- a/api/maven-api-core/src/main/java/org/apache/maven/api/services/DependencyResolverRequest.java +++ b/api/maven-api-core/src/main/java/org/apache/maven/api/services/DependencyResolverRequest.java @@ -31,6 +31,7 @@ import org.apache.maven.api.JavaPathType; import org.apache.maven.api.PathScope; import org.apache.maven.api.PathType; import org.apache.maven.api.Project; +import org.apache.maven.api.RemoteRepository; import org.apache.maven.api.Session; import org.apache.maven.api.annotations.Experimental; import org.apache.maven.api.annotations.Immutable; @@ -94,6 +95,9 @@ public interface DependencyResolverRequest { @Nullable Predicate getPathTypeFilter(); + @Nullable + List getRepositories(); + @Nonnull static DependencyResolverRequestBuilder builder() { return new DependencyResolverRequestBuilder(); @@ -170,6 +174,7 @@ public interface DependencyResolverRequest { boolean verbose; PathScope pathScope; Predicate pathTypeFilter; + List repositories; DependencyResolverRequestBuilder() {} @@ -327,6 +332,12 @@ public interface DependencyResolverRequest { return pathTypeFilter(desiredTypes::contains); } + @Nonnull + public DependencyResolverRequestBuilder repositories(@Nonnull List repositories) { + this.repositories = repositories; + return this; + } + @Nonnull public DependencyResolverRequest build() { return new DefaultDependencyResolverRequest( @@ -339,7 +350,8 @@ public interface DependencyResolverRequest { managedDependencies, verbose, pathScope, - pathTypeFilter); + pathTypeFilter, + repositories); } static class DefaultDependencyResolverRequest extends BaseRequest implements DependencyResolverRequest { @@ -352,6 +364,7 @@ public interface DependencyResolverRequest { private final boolean verbose; private final PathScope pathScope; private final Predicate pathTypeFilter; + private final List repositories; /** * Creates a request with the specified properties. @@ -371,7 +384,8 @@ public interface DependencyResolverRequest { @Nonnull Collection managedDependencies, boolean verbose, @Nullable PathScope pathScope, - @Nullable Predicate pathTypeFilter) { + @Nullable Predicate pathTypeFilter, + @Nullable List repositories) { super(session); this.requestType = nonNull(requestType, "requestType cannot be null"); this.project = project; @@ -383,6 +397,7 @@ public interface DependencyResolverRequest { this.verbose = verbose; this.pathScope = pathScope; this.pathTypeFilter = (pathTypeFilter != null) ? pathTypeFilter : (t) -> true; + this.repositories = repositories; if (verbose && requestType != RequestType.COLLECT) { throw new IllegalArgumentException("verbose cannot only be true when collecting dependencies"); } @@ -439,6 +454,11 @@ public interface DependencyResolverRequest { return pathTypeFilter; } + @Override + public List getRepositories() { + return repositories; + } + @Nonnull @Override public String toString() { diff --git a/api/maven-api-core/src/main/java/org/apache/maven/api/services/ModelBuilderRequest.java b/api/maven-api-core/src/main/java/org/apache/maven/api/services/ModelBuilderRequest.java index 31e639867f..323ba6466b 100644 --- a/api/maven-api-core/src/main/java/org/apache/maven/api/services/ModelBuilderRequest.java +++ b/api/maven-api-core/src/main/java/org/apache/maven/api/services/ModelBuilderRequest.java @@ -23,6 +23,7 @@ import java.util.Collection; import java.util.List; import java.util.Map; +import org.apache.maven.api.RemoteRepository; import org.apache.maven.api.Session; import org.apache.maven.api.annotations.Experimental; import org.apache.maven.api.annotations.Immutable; @@ -157,6 +158,9 @@ public interface ModelBuilderRequest { @Nullable ModelTransformerContextBuilder getTransformerContextBuilder(); + @Nullable + List getRepositories(); + @Nonnull static ModelBuilderRequest build(@Nonnull ModelBuilderRequest request, @Nonnull ModelSource source) { return builder(nonNull(request, "request cannot be null")) @@ -210,6 +214,7 @@ public interface ModelBuilderRequest { Object listener; ModelBuilderResult interimResult; ModelTransformerContextBuilder transformerContextBuilder; + List repositories; ModelBuilderRequestBuilder() {} @@ -232,6 +237,7 @@ public interface ModelBuilderRequest { this.listener = request.getListener(); this.interimResult = request.getInterimResult(); this.transformerContextBuilder = request.getTransformerContextBuilder(); + this.repositories = request.getRepositories(); } public ModelBuilderRequestBuilder session(Session session) { @@ -325,6 +331,11 @@ public interface ModelBuilderRequest { return this; } + public ModelBuilderRequestBuilder repositories(List repositories) { + this.repositories = repositories; + return this; + } + public ModelBuilderRequest build() { return new DefaultModelBuilderRequest( session, @@ -344,7 +355,8 @@ public interface ModelBuilderRequest { modelCache, listener, interimResult, - transformerContextBuilder); + transformerContextBuilder, + repositories); } private static class DefaultModelBuilderRequest extends BaseRequest implements ModelBuilderRequest { @@ -365,6 +377,7 @@ public interface ModelBuilderRequest { private final Object listener; private final ModelBuilderResult interimResult; private final ModelTransformerContextBuilder transformerContextBuilder; + private final List repositories; @SuppressWarnings("checkstyle:ParameterNumber") DefaultModelBuilderRequest( @@ -385,7 +398,8 @@ public interface ModelBuilderRequest { ModelCache modelCache, Object listener, ModelBuilderResult interimResult, - ModelTransformerContextBuilder transformerContextBuilder) { + ModelTransformerContextBuilder transformerContextBuilder, + List repositories) { super(session); this.validationLevel = validationLevel; this.locationTracking = locationTracking; @@ -405,6 +419,7 @@ public interface ModelBuilderRequest { this.listener = listener; this.interimResult = interimResult; this.transformerContextBuilder = transformerContextBuilder; + this.repositories = repositories != null ? List.copyOf(repositories) : null; } @Override @@ -489,6 +504,11 @@ public interface ModelBuilderRequest { public ModelTransformerContextBuilder getTransformerContextBuilder() { return transformerContextBuilder; } + + @Override + public List getRepositories() { + return repositories; + } } } } diff --git a/api/maven-api-core/src/main/java/org/apache/maven/api/services/ModelBuilderResult.java b/api/maven-api-core/src/main/java/org/apache/maven/api/services/ModelBuilderResult.java index 9c7829295e..fcaf26b81d 100644 --- a/api/maven-api-core/src/main/java/org/apache/maven/api/services/ModelBuilderResult.java +++ b/api/maven-api-core/src/main/java/org/apache/maven/api/services/ModelBuilderResult.java @@ -119,7 +119,7 @@ public interface ModelBuilderResult { List getProblems(); /** - * Creates a human readable representation of these errors. + * Creates a human-readable representation of these errors. */ String toString(); } diff --git a/api/maven-api-core/src/main/java/org/apache/maven/api/services/ModelResolver.java b/api/maven-api-core/src/main/java/org/apache/maven/api/services/ModelResolver.java index 560cbd0186..9bec734917 100644 --- a/api/maven-api-core/src/main/java/org/apache/maven/api/services/ModelResolver.java +++ b/api/maven-api-core/src/main/java/org/apache/maven/api/services/ModelResolver.java @@ -18,12 +18,15 @@ */ package org.apache.maven.api.services; +import java.util.List; import java.util.concurrent.atomic.AtomicReference; import java.util.function.Consumer; +import org.apache.maven.api.RemoteRepository; import org.apache.maven.api.Service; import org.apache.maven.api.Session; import org.apache.maven.api.annotations.Nonnull; +import org.apache.maven.api.annotations.Nullable; import org.apache.maven.api.model.Dependency; import org.apache.maven.api.model.Parent; @@ -36,6 +39,7 @@ public interface ModelResolver extends Service { * Tries to resolve the POM for the specified parent coordinates possibly updating {@code parent}. * * @param session The session to use to resolve the model, must not be {@code null}. + * @param repositories The repositories to use to resolve the model, may be {@code null} in which case the {@code Session} repositories will be used. * @param parent The parent coordinates to resolve, must not be {@code null}. * @param modified a holder for the updated parent, must not be {@code null}. * @return The source of the requested POM, never {@code null}. @@ -43,10 +47,14 @@ public interface ModelResolver extends Service { */ @Nonnull default ModelSource resolveModel( - @Nonnull Session session, @Nonnull Parent parent, @Nonnull AtomicReference modified) + @Nonnull Session session, + @Nullable List repositories, + @Nonnull Parent parent, + @Nonnull AtomicReference modified) throws ModelResolverException { return resolveModel( session, + repositories, parent.getGroupId(), parent.getArtifactId(), parent.getVersion(), @@ -57,6 +65,7 @@ public interface ModelResolver extends Service { * Tries to resolve the POM for the specified dependency coordinates possibly updating {@code dependency}. * * @param session The session to use to resolve the model, must not be {@code null}. + * @param repositories The repositories to use to resolve the model, may be {@code null} in which case the {@code Session} repositories will be used. * @param dependency The dependency coordinates to resolve, must not be {@code null}. * @param modified a holder for the updated dependency, must not be {@code null}. * @return The source of the requested POM, never {@code null}. @@ -64,10 +73,14 @@ public interface ModelResolver extends Service { */ @Nonnull default ModelSource resolveModel( - @Nonnull Session session, @Nonnull Dependency dependency, @Nonnull AtomicReference modified) + @Nonnull Session session, + @Nullable List repositories, + @Nonnull Dependency dependency, + @Nonnull AtomicReference modified) throws ModelResolverException { return resolveModel( session, + repositories, dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion(), @@ -77,6 +90,7 @@ public interface ModelResolver extends Service { @Nonnull ModelSource resolveModel( @Nonnull Session session, + @Nullable List repositories, @Nonnull String groupId, @Nonnull String artifactId, @Nonnull String version, diff --git a/api/maven-api-core/src/main/java/org/apache/maven/api/services/ProjectBuilderRequest.java b/api/maven-api-core/src/main/java/org/apache/maven/api/services/ProjectBuilderRequest.java index 697b3643ad..35e0081356 100644 --- a/api/maven-api-core/src/main/java/org/apache/maven/api/services/ProjectBuilderRequest.java +++ b/api/maven-api-core/src/main/java/org/apache/maven/api/services/ProjectBuilderRequest.java @@ -19,8 +19,10 @@ package org.apache.maven.api.services; import java.nio.file.Path; +import java.util.List; import java.util.Optional; +import org.apache.maven.api.RemoteRepository; import org.apache.maven.api.Session; import org.apache.maven.api.annotations.Experimental; import org.apache.maven.api.annotations.Immutable; @@ -57,6 +59,9 @@ public interface ProjectBuilderRequest { boolean isProcessPlugins(); + @Nullable + List getRepositories(); + @Nonnull static ProjectBuilderRequest build(@Nonnull Session session, @Nonnull Source source) { return builder() @@ -86,6 +91,7 @@ public interface ProjectBuilderRequest { boolean allowStubModel; boolean recursive; boolean processPlugins = true; + List repositories; ProjectBuilderRequestBuilder() {} @@ -109,8 +115,14 @@ public interface ProjectBuilderRequest { return this; } + public ProjectBuilderRequestBuilder repositories(List repositories) { + this.repositories = repositories; + return this; + } + public ProjectBuilderRequest build() { - return new DefaultProjectBuilderRequest(session, path, source, allowStubModel, recursive, processPlugins); + return new DefaultProjectBuilderRequest( + session, path, source, allowStubModel, recursive, processPlugins, repositories); } private static class DefaultProjectBuilderRequest extends BaseRequest implements ProjectBuilderRequest { @@ -119,6 +131,7 @@ public interface ProjectBuilderRequest { private final boolean allowStubModel; private final boolean recursive; private final boolean processPlugins; + private final List repositories; @SuppressWarnings("checkstyle:ParameterNumber") DefaultProjectBuilderRequest( @@ -127,13 +140,15 @@ public interface ProjectBuilderRequest { @Nullable Source source, boolean allowStubModel, boolean recursive, - boolean processPlugins) { + boolean processPlugins, + @Nullable List repositories) { super(session); this.path = path; this.source = source; this.allowStubModel = allowStubModel; this.recursive = recursive; this.processPlugins = processPlugins; + this.repositories = repositories; } @Nonnull @@ -162,6 +177,11 @@ public interface ProjectBuilderRequest { public boolean isProcessPlugins() { return processPlugins; } + + @Override + public List getRepositories() { + return repositories; + } } } } diff --git a/api/maven-api-core/src/main/java/org/apache/maven/api/services/VersionRangeResolver.java b/api/maven-api-core/src/main/java/org/apache/maven/api/services/VersionRangeResolver.java index 8e288d6433..aec999dd28 100644 --- a/api/maven-api-core/src/main/java/org/apache/maven/api/services/VersionRangeResolver.java +++ b/api/maven-api-core/src/main/java/org/apache/maven/api/services/VersionRangeResolver.java @@ -18,12 +18,16 @@ */ package org.apache.maven.api.services; +import java.util.List; + import org.apache.maven.api.ArtifactCoordinates; +import org.apache.maven.api.RemoteRepository; import org.apache.maven.api.Service; import org.apache.maven.api.Session; import org.apache.maven.api.annotations.Consumer; import org.apache.maven.api.annotations.Experimental; import org.apache.maven.api.annotations.Nonnull; +import org.apache.maven.api.annotations.Nullable; /** * Parses and evaluates version ranges encountered in dependency declarations. @@ -54,6 +58,29 @@ public interface VersionRangeResolver extends Service { return resolve(VersionRangeResolverRequest.build(session, artifactCoordinates)); } + /** + * Expands a version range to a list of matching versions, in ascending order. + * For example, resolves "[3.8,4.0)" to "3.8", "3.8.1", "3.8.2". + * The returned list of versions is only dependent on the configured repositories and their contents. + * The supplied request may also refer to a single concrete version rather than a version range. + * In this case though, the result contains simply the (parsed) input version, regardless of the + * repositories and their contents. + * + * @param session the session to use + * @param artifactCoordinates t + * @param repositories the repositories to use (if {@code null}, the session repositories are used) + * @return the version range resolution result + * @throws VersionResolverException if an errors occurs + */ + @Nonnull + default VersionRangeResolverResult resolve( + @Nonnull Session session, + @Nonnull ArtifactCoordinates artifactCoordinates, + @Nullable List repositories) + throws VersionResolverException { + return resolve(VersionRangeResolverRequest.build(session, artifactCoordinates, repositories)); + } + @Nonnull VersionRangeResolverResult resolve(@Nonnull VersionRangeResolverRequest request) throws VersionRangeResolverException; diff --git a/api/maven-api-core/src/main/java/org/apache/maven/api/services/VersionRangeResolverRequest.java b/api/maven-api-core/src/main/java/org/apache/maven/api/services/VersionRangeResolverRequest.java index 1ba7f8ec58..99f4f3ecc9 100644 --- a/api/maven-api-core/src/main/java/org/apache/maven/api/services/VersionRangeResolverRequest.java +++ b/api/maven-api-core/src/main/java/org/apache/maven/api/services/VersionRangeResolverRequest.java @@ -18,11 +18,15 @@ */ package org.apache.maven.api.services; +import java.util.List; + import org.apache.maven.api.ArtifactCoordinates; +import org.apache.maven.api.RemoteRepository; import org.apache.maven.api.Session; import org.apache.maven.api.annotations.Experimental; import org.apache.maven.api.annotations.Nonnull; import org.apache.maven.api.annotations.NotThreadSafe; +import org.apache.maven.api.annotations.Nullable; import static org.apache.maven.api.services.BaseRequest.nonNull; @@ -39,12 +43,24 @@ public interface VersionRangeResolverRequest { @Nonnull ArtifactCoordinates getArtifactCoordinates(); + @Nullable + List getRepositories(); + @Nonnull static VersionRangeResolverRequest build( @Nonnull Session session, @Nonnull ArtifactCoordinates artifactCoordinates) { + return build(session, artifactCoordinates, null); + } + + @Nonnull + static VersionRangeResolverRequest build( + @Nonnull Session session, + @Nonnull ArtifactCoordinates artifactCoordinates, + @Nullable List repositories) { return builder() .session(nonNull(session, "session cannot be null")) .artifactCoordinates(nonNull(artifactCoordinates, "artifactCoordinates cannot be null")) + .repositories(repositories) .build(); } @@ -57,6 +73,7 @@ public interface VersionRangeResolverRequest { class VersionResolverRequestBuilder { Session session; ArtifactCoordinates artifactCoordinates; + List repositories; public VersionResolverRequestBuilder session(Session session) { this.session = session; @@ -68,17 +85,27 @@ public interface VersionRangeResolverRequest { return this; } + public VersionResolverRequestBuilder repositories(List repositories) { + this.repositories = repositories; + return this; + } + public VersionRangeResolverRequest build() { - return new DefaultVersionResolverRequest(session, artifactCoordinates); + return new DefaultVersionResolverRequest(session, artifactCoordinates, repositories); } private static class DefaultVersionResolverRequest extends BaseRequest implements VersionRangeResolverRequest { private final ArtifactCoordinates artifactCoordinates; + private final List repositories; @SuppressWarnings("checkstyle:ParameterNumber") - DefaultVersionResolverRequest(@Nonnull Session session, @Nonnull ArtifactCoordinates artifactCoordinates) { + DefaultVersionResolverRequest( + @Nonnull Session session, + @Nonnull ArtifactCoordinates artifactCoordinates, + @Nullable List repositories) { super(session); this.artifactCoordinates = artifactCoordinates; + this.repositories = repositories; } @Nonnull @@ -86,6 +113,12 @@ public interface VersionRangeResolverRequest { public ArtifactCoordinates getArtifactCoordinates() { return artifactCoordinates; } + + @Nullable + @Override + public List getRepositories() { + return repositories; + } } } } diff --git a/api/maven-api-core/src/main/java/org/apache/maven/api/services/VersionResolverRequest.java b/api/maven-api-core/src/main/java/org/apache/maven/api/services/VersionResolverRequest.java index dd16d9d5f6..b0c9fe2f7f 100644 --- a/api/maven-api-core/src/main/java/org/apache/maven/api/services/VersionResolverRequest.java +++ b/api/maven-api-core/src/main/java/org/apache/maven/api/services/VersionResolverRequest.java @@ -18,11 +18,15 @@ */ package org.apache.maven.api.services; +import java.util.List; + import org.apache.maven.api.ArtifactCoordinates; +import org.apache.maven.api.RemoteRepository; import org.apache.maven.api.Session; import org.apache.maven.api.annotations.Experimental; import org.apache.maven.api.annotations.Nonnull; import org.apache.maven.api.annotations.NotThreadSafe; +import org.apache.maven.api.annotations.Nullable; import static org.apache.maven.api.services.BaseRequest.nonNull; @@ -39,6 +43,9 @@ public interface VersionResolverRequest { @Nonnull ArtifactCoordinates getArtifactCoordinates(); + @Nullable + List getRepositories(); + @Nonnull static VersionResolverRequest build(@Nonnull Session session, @Nonnull ArtifactCoordinates artifactCoordinates) { return builder() @@ -47,6 +54,18 @@ public interface VersionResolverRequest { .build(); } + @Nonnull + static VersionResolverRequest build( + @Nonnull Session session, + @Nonnull ArtifactCoordinates artifactCoordinates, + @Nullable List repositories) { + return builder() + .session(nonNull(session, "session cannot be null")) + .artifactCoordinates(nonNull(artifactCoordinates, "artifactCoordinates cannot be null")) + .repositories(repositories) + .build(); + } + @Nonnull static VersionResolverRequestBuilder builder() { return new VersionResolverRequestBuilder(); @@ -56,6 +75,7 @@ public interface VersionResolverRequest { class VersionResolverRequestBuilder { Session session; ArtifactCoordinates artifactCoordinates; + List repositories; public VersionResolverRequestBuilder session(Session session) { this.session = session; @@ -67,17 +87,27 @@ public interface VersionResolverRequest { return this; } + public VersionResolverRequestBuilder repositories(List repositories) { + this.repositories = repositories; + return this; + } + public VersionResolverRequest build() { - return new DefaultVersionResolverRequest(session, artifactCoordinates); + return new DefaultVersionResolverRequest(session, artifactCoordinates, repositories); } private static class DefaultVersionResolverRequest extends BaseRequest implements VersionResolverRequest { private final ArtifactCoordinates artifactCoordinates; + private final List repositories; @SuppressWarnings("checkstyle:ParameterNumber") - DefaultVersionResolverRequest(@Nonnull Session session, @Nonnull ArtifactCoordinates artifactCoordinates) { + DefaultVersionResolverRequest( + @Nonnull Session session, + @Nonnull ArtifactCoordinates artifactCoordinates, + @Nullable List repositories) { super(session); this.artifactCoordinates = artifactCoordinates; + this.repositories = repositories; } @Nonnull @@ -85,6 +115,12 @@ public interface VersionResolverRequest { public ArtifactCoordinates getArtifactCoordinates() { return artifactCoordinates; } + + @Nullable + @Override + public List getRepositories() { + return repositories; + } } } } diff --git a/maven-api-impl/src/main/java/org/apache/maven/internal/impl/AbstractSession.java b/maven-api-impl/src/main/java/org/apache/maven/internal/impl/AbstractSession.java index 77a9da652c..ece39133d3 100644 --- a/maven-api-impl/src/main/java/org/apache/maven/internal/impl/AbstractSession.java +++ b/maven-api-impl/src/main/java/org/apache/maven/internal/impl/AbstractSession.java @@ -547,6 +547,21 @@ public abstract class AbstractSession implements InternalSession { .next(); } + /** + * Shortcut for getService(ArtifactResolver.class).resolve(...) + * + * @throws ArtifactResolverException if the artifact resolution failed + * @see ArtifactResolver#resolve(Session, Collection) + */ + @Override + public DownloadedArtifact resolveArtifact(ArtifactCoordinates coordinates, List repositories) { + return getService(ArtifactResolver.class) + .resolve(this, Collections.singletonList(coordinates), repositories) + .getArtifacts() + .iterator() + .next(); + } + /** * Shortcut for getService(ArtifactResolver.class).resolve(...) * @@ -569,6 +584,20 @@ public abstract class AbstractSession implements InternalSession { return getService(ArtifactResolver.class).resolve(this, coordinates).getArtifacts(); } + /** + * Shortcut for getService(ArtifactResolver.class).resolve(...) + * + * @throws ArtifactResolverException if the artifact resolution failed + * @see ArtifactResolver#resolve(Session, Collection) + */ + @Override + public Collection resolveArtifacts( + Collection coordinates, List repositories) { + return getService(ArtifactResolver.class) + .resolve(this, coordinates, repositories) + .getArtifacts(); + } + /** * Shortcut for getService(ArtifactResolver.class).resolve(...) * @@ -582,6 +611,19 @@ public abstract class AbstractSession implements InternalSession { return resolveArtifact(coordinates); } + /** + * Shortcut for getService(ArtifactResolver.class).resolve(...) + * + * @throws ArtifactResolverException if the artifact resolution failed + * @see ArtifactResolver#resolve(Session, Collection) + */ + @Override + public DownloadedArtifact resolveArtifact(Artifact artifact, List repositories) { + ArtifactCoordinates coordinates = + getService(ArtifactCoordinatesFactory.class).create(this, artifact); + return resolveArtifact(coordinates, repositories); + } + @Override public Collection resolveArtifacts(Artifact... artifacts) { ArtifactCoordinatesFactory acf = getService(ArtifactCoordinatesFactory.class); @@ -803,6 +845,13 @@ public abstract class AbstractSession implements InternalSession { return getService(VersionRangeResolver.class).resolve(this, artifact).getVersions(); } + @Override + public List resolveVersionRange(ArtifactCoordinates artifact, List repositories) { + return getService(VersionRangeResolver.class) + .resolve(this, artifact, repositories) + .getVersions(); + } + @Override public Type requireType(String id) { return getService(TypeRegistry.class).require(id); diff --git a/maven-api-impl/src/main/java/org/apache/maven/internal/impl/DefaultArtifactResolver.java b/maven-api-impl/src/main/java/org/apache/maven/internal/impl/DefaultArtifactResolver.java index c7821b4163..49f6991666 100644 --- a/maven-api-impl/src/main/java/org/apache/maven/internal/impl/DefaultArtifactResolver.java +++ b/maven-api-impl/src/main/java/org/apache/maven/internal/impl/DefaultArtifactResolver.java @@ -54,7 +54,8 @@ public class DefaultArtifactResolver implements ArtifactResolver { try { Map paths = new HashMap<>(); ArtifactManager artifactManager = session.getService(ArtifactManager.class); - List repositories = session.toRepositories(session.getRemoteRepositories()); + List repositories = session.toRepositories( + request.getRepositories() != null ? request.getRepositories() : session.getRemoteRepositories()); List requests = new ArrayList<>(); for (ArtifactCoordinates coords : request.getCoordinates()) { org.eclipse.aether.artifact.Artifact aetherArtifact = session.toArtifact(coords); diff --git a/maven-api-impl/src/main/java/org/apache/maven/internal/impl/DefaultDependencyResolver.java b/maven-api-impl/src/main/java/org/apache/maven/internal/impl/DefaultDependencyResolver.java index d5a1746cfe..6675d7bf96 100644 --- a/maven-api-impl/src/main/java/org/apache/maven/internal/impl/DefaultDependencyResolver.java +++ b/maven-api-impl/src/main/java/org/apache/maven/internal/impl/DefaultDependencyResolver.java @@ -85,13 +85,16 @@ public class DefaultDependencyResolver implements DependencyResolver { root = null; dependencies = project.getDependencies(); managedDependencies = project.getManagedDependencies(); - remoteRepositories = session.getService(ProjectManager.class).getRemoteProjectRepositories(project); + remoteRepositories = request.getRepositories() != null + ? request.getRepositories() + : session.getService(ProjectManager.class).getRemoteProjectRepositories(project); } else { rootArtifact = request.getRootArtifact().orElse(null); root = request.getRoot().orElse(null); dependencies = request.getDependencies(); managedDependencies = request.getManagedDependencies(); - remoteRepositories = session.getRemoteRepositories(); + remoteRepositories = + request.getRepositories() != null ? request.getRepositories() : session.getRemoteRepositories(); } CollectRequest collectRequest = new CollectRequest() .setRootArtifact(rootArtifact != null ? session.toArtifact(rootArtifact) : null) @@ -149,6 +152,8 @@ public class DefaultDependencyResolver implements DependencyResolver { InternalSession.from(nonNull(request, "request").getSession()); DependencyResolverResult result; DependencyResolverResult collectorResult = collect(request); + List repositories = + request.getRepositories() != null ? request.getRepositories() : session.getRemoteRepositories(); if (request.getRequestType() == DependencyResolverRequest.RequestType.COLLECT) { result = collectorResult; } else { @@ -171,7 +176,7 @@ public class DefaultDependencyResolver implements DependencyResolver { DefaultDependencyResolverResult resolverResult = new DefaultDependencyResolverResult( cache, collectorResult.getExceptions(), collectorResult.getRoot(), nodes.size()); ArtifactResolverResult artifactResolverResult = - session.getService(ArtifactResolver.class).resolve(session, coordinates); + session.getService(ArtifactResolver.class).resolve(session, coordinates, repositories); for (Node node : nodes) { Dependency d = node.getDependency(); Path path = (d != null) ? artifactResolverResult.getPath(d) : null; diff --git a/maven-api-impl/src/main/java/org/apache/maven/internal/impl/DefaultVersionRangeResolver.java b/maven-api-impl/src/main/java/org/apache/maven/internal/impl/DefaultVersionRangeResolver.java index bed8ec9b93..2de529f6fc 100644 --- a/maven-api-impl/src/main/java/org/apache/maven/internal/impl/DefaultVersionRangeResolver.java +++ b/maven-api-impl/src/main/java/org/apache/maven/internal/impl/DefaultVersionRangeResolver.java @@ -63,7 +63,10 @@ public class DefaultVersionRangeResolver implements VersionRangeResolver { session.getSession(), new VersionRangeRequest( session.toArtifact(request.getArtifactCoordinates()), - session.toRepositories(session.getRemoteRepositories()), + session.toRepositories( + request.getRepositories() != null + ? request.getRepositories() + : session.getRemoteRepositories()), null)); Map repos = res.getVersions().stream() diff --git a/maven-api-impl/src/main/java/org/apache/maven/internal/impl/DefaultVersionResolver.java b/maven-api-impl/src/main/java/org/apache/maven/internal/impl/DefaultVersionResolver.java index e8a9643705..6c9f0ec916 100644 --- a/maven-api-impl/src/main/java/org/apache/maven/internal/impl/DefaultVersionResolver.java +++ b/maven-api-impl/src/main/java/org/apache/maven/internal/impl/DefaultVersionResolver.java @@ -58,7 +58,10 @@ public class DefaultVersionResolver implements VersionResolver { session.getSession(), new VersionRequest( session.toArtifact(request.getArtifactCoordinates()), - session.toRepositories(session.getRemoteRepositories()), + session.toRepositories( + request.getRepositories() != null + ? request.getRepositories() + : session.getRemoteRepositories()), null)); return new VersionResolverResult() { diff --git a/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/DefaultModelBuilder.java b/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/DefaultModelBuilder.java index a0a563167f..fb83c160f8 100644 --- a/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/DefaultModelBuilder.java +++ b/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/DefaultModelBuilder.java @@ -42,7 +42,6 @@ import java.util.function.UnaryOperator; import java.util.stream.Collectors; import java.util.stream.Stream; -import org.apache.maven.api.Session; import org.apache.maven.api.Type; import org.apache.maven.api.VersionRange; import org.apache.maven.api.annotations.Nullable; @@ -1180,9 +1179,8 @@ public class DefaultModelBuilder implements ModelBuilder { ModelSource modelSource; try { AtomicReference modified = new AtomicReference<>(); - Session session = request.getSession() - .withRemoteRepositories(request.getModelRepositoryHolder().getRepositories()); - modelSource = modelResolver.resolveModel(session, parent, modified); + modelSource = modelResolver.resolveModel( + request.getSession(), request.getModelRepositoryHolder().getRepositories(), parent, modified); if (modified.get() != null) { parent = modified.get(); } @@ -1422,10 +1420,11 @@ public class DefaultModelBuilder implements ModelBuilder { if (importModel == null) { final ModelSource importSource; try { - Session session = request.getSession() - .withRemoteRepositories( - request.getModelRepositoryHolder().getRepositories()); - importSource = modelResolver.resolveModel(session, dependency, new AtomicReference<>()); + importSource = modelResolver.resolveModel( + request.getSession(), + request.getModelRepositoryHolder().getRepositories(), + dependency, + new AtomicReference<>()); } catch (ModelBuilderException e) { StringBuilder buffer = new StringBuilder(256); buffer.append("Non-resolvable import POM"); @@ -1459,9 +1458,8 @@ public class DefaultModelBuilder implements ModelBuilder { final ModelBuilderResult importResult; try { ModelBuilderRequest importRequest = ModelBuilderRequest.builder() - .session(request.getSession() - .withRemoteRepositories( - request.getModelRepositoryHolder().getRepositories())) + .session(request.getSession()) + .repositories(request.getModelRepositoryHolder().getRepositories()) .validationLevel(ModelBuilderRequest.VALIDATION_LEVEL_MINIMAL) .systemProperties(request.getSystemProperties()) .userProperties(request.getUserProperties()) diff --git a/maven-api-impl/src/main/java/org/apache/maven/internal/impl/resolver/DefaultArtifactDescriptorReader.java b/maven-api-impl/src/main/java/org/apache/maven/internal/impl/resolver/DefaultArtifactDescriptorReader.java index 1c5839e094..2498441804 100644 --- a/maven-api-impl/src/main/java/org/apache/maven/internal/impl/resolver/DefaultArtifactDescriptorReader.java +++ b/maven-api-impl/src/main/java/org/apache/maven/internal/impl/resolver/DefaultArtifactDescriptorReader.java @@ -24,7 +24,7 @@ import java.util.List; import java.util.Map; import java.util.Objects; -import org.apache.maven.api.Session; +import org.apache.maven.api.RemoteRepository; import org.apache.maven.api.di.Inject; import org.apache.maven.api.di.Named; import org.apache.maven.api.di.Singleton; @@ -40,7 +40,6 @@ import org.apache.maven.api.services.ModelResolverException; import org.apache.maven.api.services.ModelSource; import org.apache.maven.internal.impl.InternalSession; import org.apache.maven.internal.impl.model.ModelProblemUtils; -import org.codehaus.plexus.interpolation.util.StringUtils; import org.eclipse.aether.RepositoryEvent; import org.eclipse.aether.RepositoryEvent.EventType; import org.eclipse.aether.RepositoryException; @@ -199,18 +198,16 @@ public class DefaultArtifactDescriptorReader implements ArtifactDescriptorReader try { InternalSession iSession = InternalSession.from(session); - Session iSessionWithRepos = iSession.withRemoteRepositories(request.getRepositories().stream() + List repositories = request.getRepositories().stream() .map(iSession::getRemoteRepository) - .toList()); + .toList(); String gav = pomArtifact.getGroupId() + ":" + pomArtifact.getArtifactId() + ":" + pomArtifact.getVersion(); ModelResolver modelResolver = new DefaultModelResolver(); ModelRepositoryHolder modelRepositoryHolder = new DefaultModelRepositoryHolder( - iSessionWithRepos, - DefaultModelRepositoryHolder.RepositoryMerging.REQUEST_DOMINANT, - iSessionWithRepos.getRemoteRepositories()); + iSession, DefaultModelRepositoryHolder.RepositoryMerging.REQUEST_DOMINANT, repositories); ModelBuilderRequest modelRequest = ModelBuilderRequest.builder() - .session(iSessionWithRepos) + .session(iSession) .projectBuild(false) .processPlugins(false) .twoPhaseBuilding(false) @@ -222,6 +219,7 @@ public class DefaultArtifactDescriptorReader implements ArtifactDescriptorReader .modelResolver(modelResolver) .modelRepositoryHolder(modelRepositoryHolder) .modelCache(DefaultModelCache.newInstance(session, false)) + .repositories(repositories) .build(); ModelBuilderResult modelResult = modelBuilder.build(modelRequest); @@ -230,21 +228,23 @@ public class DefaultArtifactDescriptorReader implements ArtifactDescriptorReader if (!modelResult.getProblems().isEmpty()) { List problems = modelResult.getProblems(); if (logger.isDebugEnabled()) { - String problem = (problems.size() == 1) ? "problem" : "problems"; - String problemPredicate = problem + ((problems.size() == 1) ? " was" : " were"); - String message = String.format( - "%s %s encountered while building the effective model for %s during %s\n", - problems.size(), - problemPredicate, - request.getArtifact(), - RequestTraceHelper.interpretTrace(true, request.getTrace())); - message += StringUtils.capitalizeFirstLetter(problem); + StringBuilder sb = new StringBuilder(); + sb.append(problems.size()) + .append(" ") + .append((problems.size() == 1) ? "problem was" : "problems were") + .append(" encountered while building the effective model for ") + .append(request.getArtifact()) + .append(" during ") + .append(RequestTraceHelper.interpretTrace(true, request.getTrace())) + .append("\n") + .append((problems.size() == 1) ? "Problem" : "Problems"); for (ModelProblem modelProblem : problems) { - message += String.format( - "\n* %s @ %s", - modelProblem.getMessage(), ModelProblemUtils.formatLocation(modelProblem, null)); + sb.append("\n* ") + .append(modelProblem.getMessage()) + .append(" @ ") + .append(ModelProblemUtils.formatLocation(modelProblem, null)); } - logger.warn(message); + logger.warn(sb.toString()); } else { logger.warn( "{} {} encountered while building the effective model for {} during {} (use -X to see details)", diff --git a/maven-api-impl/src/main/java/org/apache/maven/internal/impl/resolver/DefaultModelResolver.java b/maven-api-impl/src/main/java/org/apache/maven/internal/impl/resolver/DefaultModelResolver.java index 3b5a0da8e9..0a16b9696c 100644 --- a/maven-api-impl/src/main/java/org/apache/maven/internal/impl/resolver/DefaultModelResolver.java +++ b/maven-api-impl/src/main/java/org/apache/maven/internal/impl/resolver/DefaultModelResolver.java @@ -28,6 +28,7 @@ import java.util.stream.Collectors; import org.apache.maven.api.ArtifactCoordinates; import org.apache.maven.api.DownloadedArtifact; +import org.apache.maven.api.RemoteRepository; import org.apache.maven.api.Session; import org.apache.maven.api.Version; import org.apache.maven.api.di.Named; @@ -50,7 +51,12 @@ public class DefaultModelResolver implements ModelResolver { @Override public ModelSource resolveModel( - Session session, String groupId, String artifactId, String version, Consumer resolvedVersion) + Session session, + List repositories, + String groupId, + String artifactId, + String version, + Consumer resolvedVersion) throws ModelResolverException { try { ArtifactCoordinates coords = session.createArtifactCoordinates(groupId, artifactId, version, "pom"); @@ -63,7 +69,7 @@ public class DefaultModelResolver implements ModelResolver { artifactId, version); } - List versions = session.resolveVersionRange(coords); + List versions = session.resolveVersionRange(coords, repositories); if (versions.isEmpty()) { throw new ModelResolverException( String.format("No versions matched the requested version range '%s'", version), @@ -76,8 +82,8 @@ public class DefaultModelResolver implements ModelResolver { resolvedVersion.accept(newVersion); } - DownloadedArtifact resolved = - session.resolveArtifact(session.createArtifactCoordinates(groupId, artifactId, newVersion, "pom")); + DownloadedArtifact resolved = session.resolveArtifact( + session.createArtifactCoordinates(groupId, artifactId, newVersion, "pom"), repositories); Path path = resolved.getPath(); String location = groupId + ":" + artifactId + ":" + newVersion; return new ModelSource() { @@ -109,9 +115,8 @@ public class DefaultModelResolver implements ModelResolver { } catch (VersionRangeResolverException | ArtifactResolverException e) { throw new ModelResolverException( e.getMessage() + " (remote repositories: " - + session.getRemoteRepositories().stream() - .map(Object::toString) - .collect(Collectors.joining(", ")) + + (repositories != null ? repositories : session.getRemoteRepositories()) + .stream().map(Object::toString).collect(Collectors.joining(", ")) + ")", groupId, artifactId, diff --git a/maven-api-impl/src/test/java/org/apache/maven/internal/impl/resolver/DefaultModelResolverTest.java b/maven-api-impl/src/test/java/org/apache/maven/internal/impl/resolver/DefaultModelResolverTest.java index a6088b80f0..51daf7079a 100644 --- a/maven-api-impl/src/test/java/org/apache/maven/internal/impl/resolver/DefaultModelResolverTest.java +++ b/maven-api-impl/src/test/java/org/apache/maven/internal/impl/resolver/DefaultModelResolverTest.java @@ -69,7 +69,7 @@ class DefaultModelResolverTest { ModelResolverException e = assertThrows( ModelResolverException.class, - () -> newModelResolver().resolveModel(session, parent, new AtomicReference<>()), + () -> newModelResolver().resolveModel(session, null, parent, new AtomicReference<>()), "Expected 'ModelResolverException' not thrown."); assertNotNull(e.getMessage()); assertThat(e.getMessage(), containsString("Could not find artifact org.apache:apache:pom:0 in central")); @@ -85,7 +85,7 @@ class DefaultModelResolverTest { ModelResolverException e = assertThrows( ModelResolverException.class, - () -> newModelResolver().resolveModel(session, parent, new AtomicReference<>()), + () -> newModelResolver().resolveModel(session, null, parent, new AtomicReference<>()), "Expected 'ModelResolverException' not thrown."); assertEquals("No versions matched the requested version range '[2.0,2.1)'", e.getMessage()); } @@ -100,7 +100,7 @@ class DefaultModelResolverTest { ModelResolverException e = assertThrows( ModelResolverException.class, - () -> newModelResolver().resolveModel(session, parent, new AtomicReference<>()), + () -> newModelResolver().resolveModel(session, null, parent, new AtomicReference<>()), "Expected 'ModelResolverException' not thrown."); assertEquals("The requested version range '[1,)' does not specify an upper bound", e.getMessage()); } @@ -113,7 +113,7 @@ class DefaultModelResolverTest { .version("1") .build(); - assertNotNull(this.newModelResolver().resolveModel(session, parent, new AtomicReference<>())); + assertNotNull(this.newModelResolver().resolveModel(session, null, parent, new AtomicReference<>())); assertEquals("1", parent.getVersion()); } @@ -126,7 +126,7 @@ class DefaultModelResolverTest { .build(); AtomicReference modified = new AtomicReference<>(); - assertNotNull(this.newModelResolver().resolveModel(session, parent, modified)); + assertNotNull(this.newModelResolver().resolveModel(session, null, parent, modified)); assertEquals("1", modified.get().getVersion()); } @@ -140,7 +140,7 @@ class DefaultModelResolverTest { ModelResolverException e = assertThrows( ModelResolverException.class, - () -> newModelResolver().resolveModel(session, dependency, new AtomicReference<>()), + () -> newModelResolver().resolveModel(session, null, dependency, new AtomicReference<>()), "Expected 'ModelResolverException' not thrown."); assertNotNull(e.getMessage()); assertThat(e.getMessage(), containsString("Could not find artifact org.apache:apache:pom:0 in central")); @@ -156,7 +156,7 @@ class DefaultModelResolverTest { ModelResolverException e = assertThrows( ModelResolverException.class, - () -> newModelResolver().resolveModel(session, dependency, new AtomicReference<>()), + () -> newModelResolver().resolveModel(session, null, dependency, new AtomicReference<>()), "Expected 'ModelResolverException' not thrown."); assertEquals("No versions matched the requested version range '[2.0,2.1)'", e.getMessage()); } @@ -171,7 +171,7 @@ class DefaultModelResolverTest { ModelResolverException e = assertThrows( ModelResolverException.class, - () -> newModelResolver().resolveModel(session, dependency, new AtomicReference<>()), + () -> newModelResolver().resolveModel(session, null, dependency, new AtomicReference<>()), "Expected 'ModelResolverException' not thrown."); assertEquals("The requested version range '[1,)' does not specify an upper bound", e.getMessage()); } @@ -184,7 +184,7 @@ class DefaultModelResolverTest { .version("1") .build(); - assertNotNull(this.newModelResolver().resolveModel(session, dependency, new AtomicReference<>())); + assertNotNull(this.newModelResolver().resolveModel(session, null, dependency, new AtomicReference<>())); assertEquals("1", dependency.getVersion()); } @@ -197,7 +197,7 @@ class DefaultModelResolverTest { .build(); AtomicReference modified = new AtomicReference<>(); - assertNotNull(this.newModelResolver().resolveModel(session, dependency, modified)); + assertNotNull(this.newModelResolver().resolveModel(session, null, dependency, modified)); assertEquals("1", modified.get().getVersion()); } diff --git a/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultProjectBuilder.java b/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultProjectBuilder.java index cbc2e0afd7..a446f63b26 100644 --- a/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultProjectBuilder.java +++ b/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultProjectBuilder.java @@ -66,7 +66,8 @@ public class DefaultProjectBuilder implements ProjectBuilder { throws ProjectBuilderException, IllegalArgumentException { InternalMavenSession session = InternalMavenSession.from(request.getSession()); try { - List repositories = session.toArtifactRepositories(session.getRemoteRepositories()); + List repositories = session.toArtifactRepositories( + request.getRepositories() != null ? request.getRepositories() : session.getRemoteRepositories()); ProjectBuildingRequest req = new DefaultProjectBuildingRequest() .setRepositorySession(session.getSession()) .setRemoteRepositories(repositories) @@ -134,14 +135,14 @@ public class DefaultProjectBuilder implements ProjectBuilder { } if (getLineNumber() > 0) { - if (buffer.length() > 0) { + if (!buffer.isEmpty()) { buffer.append(", "); } buffer.append("line ").append(getLineNumber()); } if (getColumnNumber() > 0) { - if (buffer.length() > 0) { + if (!buffer.isEmpty()) { buffer.append(", "); } buffer.append("column ").append(getColumnNumber()); diff --git a/maven-core/src/main/java/org/apache/maven/plugin/internal/DefaultMavenPluginManager.java b/maven-core/src/main/java/org/apache/maven/plugin/internal/DefaultMavenPluginManager.java index 77d11a2706..76d359f461 100644 --- a/maven-core/src/main/java/org/apache/maven/plugin/internal/DefaultMavenPluginManager.java +++ b/maven-core/src/main/java/org/apache/maven/plugin/internal/DefaultMavenPluginManager.java @@ -54,7 +54,6 @@ import org.apache.maven.api.plugin.descriptor.Resolution; import org.apache.maven.api.services.DependencyResolver; import org.apache.maven.api.services.DependencyResolverResult; import org.apache.maven.api.services.PathScopeRegistry; -import org.apache.maven.api.services.ProjectManager; import org.apache.maven.api.xml.XmlNode; import org.apache.maven.artifact.Artifact; import org.apache.maven.classrealm.ClassRealmManager; @@ -549,10 +548,6 @@ public class DefaultMavenPluginManager implements MavenPluginManager { InternalMavenSession sessionV4 = InternalMavenSession.from(session.getSession()); Project project = sessionV4.getProject(session.getCurrentProject()); - List repos = - sessionV4.getService(ProjectManager.class).getRemoteProjectRepositories(project); - sessionV4 = InternalMavenSession.from(sessionV4.withRemoteRepositories(repos)); - org.apache.maven.api.MojoExecution execution = new DefaultMojoExecution(sessionV4, mojoExecution); org.apache.maven.api.plugin.Log log = new DefaultLog( LoggerFactory.getLogger(mojoExecution.getMojoDescriptor().getFullGoalName())); diff --git a/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuilder.java b/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuilder.java index ee945eff3f..ba7ca435f5 100644 --- a/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuilder.java +++ b/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuilder.java @@ -1108,9 +1108,7 @@ public class DefaultProjectBuilder implements ProjectBuilder { ModelBuilderRequest.ModelBuilderRequestBuilder modelBuildingRequest = ModelBuilderRequest.builder(); InternalSession internalSession = InternalSession.from(session); - modelBuildingRequest.session(internalSession.withRemoteRepositories(request.getRemoteRepositories().stream() - .map(r -> internalSession.getRemoteRepository(RepositoryUtils.toRepo(r))) - .toList())); + modelBuildingRequest.session(internalSession); modelBuildingRequest.validationLevel(request.getValidationLevel()); modelBuildingRequest.processPlugins(request.isProcessPlugins()); modelBuildingRequest.profiles( @@ -1135,6 +1133,9 @@ public class DefaultProjectBuilder implements ProjectBuilder { modelBuildingRequest.modelRepositoryHolder(holder); modelBuildingRequest.modelCache(modelCache); modelBuildingRequest.transformerContextBuilder(transformerContextBuilder); + modelBuildingRequest.repositories(request.getRemoteRepositories().stream() + .map(r -> internalSession.getRemoteRepository(RepositoryUtils.toRepo(r))) + .toList()); /* TODO: bv4 InternalMavenSession session = (InternalMavenSession) this.session.getData().get(InternalMavenSession.class); @@ -1300,12 +1301,17 @@ public class DefaultProjectBuilder implements ProjectBuilder { @Override public ModelSource resolveModel( - Session session, String groupId, String artifactId, String version, Consumer resolved) + Session session, + List repositories, + String groupId, + String artifactId, + String version, + Consumer resolved) throws ModelResolverException { try { InternalSession internalSession = InternalSession.from(session); - org.apache.maven.model.resolution.ModelResolver resolver = - getResolver(internalSession.toRepositories(internalSession.getRemoteRepositories())); + org.apache.maven.model.resolution.ModelResolver resolver = getResolver(internalSession.toRepositories( + repositories != null ? repositories : internalSession.getRemoteRepositories())); org.apache.maven.model.Parent p = new org.apache.maven.model.Parent(Parent.newBuilder() .groupId(groupId) .artifactId(artifactId) @@ -1322,13 +1328,17 @@ public class DefaultProjectBuilder implements ProjectBuilder { } @Override - public ModelSource resolveModel(Session session, Parent parent, AtomicReference modified) + public ModelSource resolveModel( + Session session, + List repositories, + Parent parent, + AtomicReference modified) throws ModelResolverException { try { org.apache.maven.model.Parent p = new org.apache.maven.model.Parent(parent); InternalSession internalSession = InternalSession.from(session); - org.apache.maven.model.resolution.ModelResolver resolver = - getResolver(internalSession.toRepositories(internalSession.getRemoteRepositories())); + org.apache.maven.model.resolution.ModelResolver resolver = getResolver(internalSession.toRepositories( + repositories != null ? repositories : internalSession.getRemoteRepositories())); ModelSource source = toSource(resolver.resolveModel(p)); if (p.getDelegate() != parent) { modified.set(p.getDelegate()); @@ -1340,13 +1350,17 @@ public class DefaultProjectBuilder implements ProjectBuilder { } @Override - public ModelSource resolveModel(Session session, Dependency dependency, AtomicReference modified) + public ModelSource resolveModel( + Session session, + List repositories, + Dependency dependency, + AtomicReference modified) throws ModelResolverException { try { org.apache.maven.model.Dependency d = new org.apache.maven.model.Dependency(dependency); InternalSession internalSession = InternalSession.from(session); - org.apache.maven.model.resolution.ModelResolver resolver = - getResolver(internalSession.toRepositories(internalSession.getRemoteRepositories())); + org.apache.maven.model.resolution.ModelResolver resolver = getResolver(internalSession.toRepositories( + repositories != null ? repositories : internalSession.getRemoteRepositories())); ModelSource source = toSource(resolver.resolveModel(d)); if (d.getDelegate() != dependency) { modified.set(d.getDelegate()); diff --git a/maven-core/src/test/java/org/apache/maven/internal/transformation/impl/ConsumerPomBuilderTest.java b/maven-core/src/test/java/org/apache/maven/internal/transformation/impl/ConsumerPomBuilderTest.java index 92308e2f2e..c21e890238 100644 --- a/maven-core/src/test/java/org/apache/maven/internal/transformation/impl/ConsumerPomBuilderTest.java +++ b/maven-core/src/test/java/org/apache/maven/internal/transformation/impl/ConsumerPomBuilderTest.java @@ -27,8 +27,10 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Collections; +import java.util.List; import java.util.function.Consumer; +import org.apache.maven.api.RemoteRepository; import org.apache.maven.api.Session; import org.apache.maven.api.model.Model; import org.apache.maven.api.services.ModelResolver; @@ -101,7 +103,12 @@ public class ConsumerPomBuilderTest extends AbstractRepositoryTestCase { public static class MyModelResolver implements ModelResolver { @Override public ModelSource resolveModel( - Session session, String groupId, String artifactId, String version, Consumer resolvedVersion) + Session session, + List repositories, + String groupId, + String artifactId, + String version, + Consumer resolvedVersion) throws ModelResolverException { String id = groupId + ":" + artifactId + ":" + version; if (id.startsWith("org.sonatype.mavenbook.multi:parent:")) {