From fcd9c0f0180880865ca76b1cf31d8504aa50e3ed Mon Sep 17 00:00:00 2001 From: Tamas Cservenak Date: Wed, 20 Nov 2024 20:58:27 +0100 Subject: [PATCH] [MNG-8385] Introduce proto session, make CLIng use PropertyContributor (#1929) And make use of it in CLIng. Also, move from "late" Resolver session factory to "early" CLIng invocation of `PropertyContributor` SPI and make contribution visible across whole Maven, not only Resolver. --- https://issues.apache.org/jira/browse/MNG-8385 --- .../org/apache/maven/api/ProtoSession.java | 212 ++++ .../java/org/apache/maven/api/Session.java | 69 +- .../ArtifactCoordinatesFactoryRequest.java | 2 +- .../api/services/ArtifactDeployerRequest.java | 3 +- .../api/services/ArtifactFactoryRequest.java | 3 +- .../services/ArtifactInstallerRequest.java | 2 +- .../api/services/ArtifactResolverRequest.java | 3 +- .../maven/api/services/BaseRequest.java | 10 +- .../DependencyCoordinatesFactoryRequest.java | 2 +- .../services/DependencyResolverRequest.java | 3 +- .../api/services/ModelBuilderRequest.java | 2 +- .../api/services/ProjectBuilderRequest.java | 3 +- .../api/services/SettingsBuilderRequest.java | 23 +- .../services/ToolchainsBuilderRequest.java | 19 +- .../services/VersionRangeResolverRequest.java | 3 +- .../api/services/VersionResolverRequest.java | 3 +- .../maven/api/spi/PropertyContributor.java | 19 +- .../maven/cling/invoker/LookupContext.java | 24 +- .../maven/cling/invoker/LookupInvoker.java | 71 +- .../PlexusContainerCapsuleFactory.java | 18 +- .../maven/cling/invoker/mvn/MavenContext.java | 7 +- .../maven/cling/invoker/mvn/MavenInvoker.java | 61 +- .../maven/cling/invoker/mvn/ProtoSession.java | 409 ------ .../invoker/mvn/RepositorySystemSupplier.java | 1098 ----------------- .../mvn/resident/ResidentMavenContext.java | 6 - .../spi/PropertyContributorsHolder.java | 42 + ...DefaultRepositorySystemSessionFactory.java | 9 - .../aether/PropertyContributorExtender.java | 62 - ...ultRepositorySystemSessionFactoryTest.java | 10 - .../internal/impl/DefaultSettingsBuilder.java | 32 +- ...venITmng8385PropertyContributoSPITest.java | 58 + .../apache/maven/it/TestSuiteOrdering.java | 1 + .../mng-8385/spi-consumer/.mvn/extensions.xml | 7 + .../resources/mng-8385/spi-consumer/pom.xml | 32 + .../resources/mng-8385/spi-extension/pom.xml | 29 + .../mng8385/CustomPropertyContributor.java | 21 +- 36 files changed, 584 insertions(+), 1794 deletions(-) create mode 100644 api/maven-api-core/src/main/java/org/apache/maven/api/ProtoSession.java delete mode 100644 impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvn/ProtoSession.java delete mode 100644 impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvn/RepositorySystemSupplier.java create mode 100644 impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/spi/PropertyContributorsHolder.java delete mode 100644 impl/maven-core/src/main/java/org/apache/maven/internal/aether/PropertyContributorExtender.java create mode 100644 its/core-it-suite/src/test/java/org/apache/maven/it/MavenITmng8385PropertyContributoSPITest.java create mode 100644 its/core-it-suite/src/test/resources/mng-8385/spi-consumer/.mvn/extensions.xml create mode 100644 its/core-it-suite/src/test/resources/mng-8385/spi-consumer/pom.xml create mode 100644 its/core-it-suite/src/test/resources/mng-8385/spi-extension/pom.xml rename impl/maven-core/src/main/java/org/apache/maven/internal/aether/MavenExecutionRequestExtender.java => its/core-it-suite/src/test/resources/mng-8385/spi-extension/src/main/java/org/apache/maven/its/mng8385/CustomPropertyContributor.java (68%) diff --git a/api/maven-api-core/src/main/java/org/apache/maven/api/ProtoSession.java b/api/maven-api-core/src/main/java/org/apache/maven/api/ProtoSession.java new file mode 100644 index 0000000000..97ac59cb24 --- /dev/null +++ b/api/maven-api-core/src/main/java/org/apache/maven/api/ProtoSession.java @@ -0,0 +1,212 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.maven.api; + +import java.nio.file.Path; +import java.time.Instant; +import java.util.HashMap; +import java.util.Map; + +import org.apache.maven.api.annotations.Experimental; +import org.apache.maven.api.annotations.Nonnull; +import org.apache.maven.api.annotations.Nullable; +import org.apache.maven.api.annotations.ThreadSafe; + +import static java.util.Objects.requireNonNull; + +/** + * The proto session, material used to create {@link Session}. + * + * @since 4.0.0 + */ +@Experimental +@ThreadSafe +public interface ProtoSession { + + /** + * Returns immutable user properties to use for interpolation. The user properties have been configured directly + * by the user, e.g. via the {@code -Dkey=value} parameter on the command line. + * + * @return the user properties, never {@code null} + */ + @Nonnull + Map getUserProperties(); + + /** + * Returns immutable system properties to use for interpolation. The system properties are collected from the + * runtime environment such as {@link System#getProperties()} and environment variables + * (prefixed with {@code env.}). + * + * @return the system properties, never {@code null} + */ + @Nonnull + Map getSystemProperties(); + + /** + * Returns the start time of the session. + * + * @return the start time as an Instant object, never {@code null} + */ + @Nonnull + Instant getStartTime(); + + /** + * Gets the directory of the topmost project being built, usually the current directory or the + * directory pointed at by the {@code -f/--file} command line argument. + * + * @return the directory of the topmost project, never {@code null} + * @see Project#isTopProject() + * @see #getRootDirectory() + */ + @Nonnull + Path getTopDirectory(); + + /** + * Gets the root directory of the session, which is the root directory for the top directory project. + * + * @return the root directory, never {@code null} + * @throws IllegalStateException if the root directory could not be found + * @see #getTopDirectory() + * @see Project#getRootDirectory() + * @see Project#isRootProject() + */ + @Nonnull + Path getRootDirectory(); + + /** + * Returns a proto session builder of this instance. + */ + @Nonnull + default Builder toBuilder() { + try { + return new Builder( + getUserProperties(), getSystemProperties(), getStartTime(), getTopDirectory(), getRootDirectory()); + } catch (IllegalStateException e) { + return new Builder(getUserProperties(), getSystemProperties(), getStartTime(), getTopDirectory(), null); + } + } + + /** + * Returns new builder from scratch. + */ + static Builder newBuilder() { + return new Builder().withStartTime(Instant.now()); + } + + class Builder { + private Map userProperties; + private Map systemProperties; + private Instant startTime; + private Path topDirectory; + private Path rootDirectory; + + private Builder() {} + + private Builder( + Map userProperties, + Map systemProperties, + Instant startTime, + Path topDirectory, + Path rootDirectory) { + this.userProperties = userProperties; + this.systemProperties = systemProperties; + this.startTime = startTime; + this.topDirectory = topDirectory; + this.rootDirectory = rootDirectory; + } + + public Builder withUserProperties(@Nonnull Map userProperties) { + this.userProperties = new HashMap<>(userProperties); + return this; + } + + public Builder withSystemProperties(@Nonnull Map systemProperties) { + this.systemProperties = new HashMap<>(systemProperties); + return this; + } + + public Builder withStartTime(@Nonnull Instant startTime) { + this.startTime = requireNonNull(startTime, "startTime"); + return this; + } + + public Builder withTopDirectory(@Nonnull Path topDirectory) { + this.topDirectory = requireNonNull(topDirectory, "topDirectory"); + return this; + } + + public Builder withRootDirectory(@Nullable Path rootDirectory) { + this.rootDirectory = rootDirectory; + return this; + } + + public ProtoSession build() { + return new Impl(userProperties, systemProperties, startTime, topDirectory, rootDirectory); + } + + private static class Impl implements ProtoSession { + private final Map userProperties; + private final Map systemProperties; + private final Instant startTime; + private final Path topDirectory; + private final Path rootDirectory; + + private Impl( + Map userProperties, + Map systemProperties, + Instant startTime, + Path topDirectory, + Path rootDirectory) { + this.userProperties = requireNonNull(userProperties); + this.systemProperties = requireNonNull(systemProperties); + this.startTime = requireNonNull(startTime); + this.topDirectory = requireNonNull(topDirectory); + this.rootDirectory = rootDirectory; + } + + @Override + public Map getUserProperties() { + return userProperties; + } + + @Override + public Map getSystemProperties() { + return systemProperties; + } + + @Override + public Instant getStartTime() { + return startTime; + } + + @Override + public Path getTopDirectory() { + return topDirectory; + } + + @Override + public Path getRootDirectory() { + if (rootDirectory == null) { + throw new IllegalStateException("root directory not set"); + } + return rootDirectory; + } + } + } +} 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 7c82a8ef4b..681ebb5040 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 @@ -19,7 +19,6 @@ package org.apache.maven.api; import java.nio.file.Path; -import java.time.Instant; import java.util.Collection; import java.util.List; import java.util.Map; @@ -46,7 +45,15 @@ import org.apache.maven.api.settings.Settings; */ @Experimental @ThreadSafe -public interface Session { +public interface Session extends ProtoSession { + + /** + * Returns the current maven version. + * + * @return the maven version, never {@code null} + */ + @Nonnull + Version getMavenVersion(); /** * Retrieves the settings for the current session. @@ -80,25 +87,6 @@ public interface Session { @Nonnull SessionData getData(); - /** - * Returns immutable user properties to use for interpolation. The user properties have been configured directly - * by the user, e.g. via the {@code -Dkey=value} parameter on the command line. - * - * @return the user properties, never {@code null} - */ - @Nonnull - Map getUserProperties(); - - /** - * Returns immutable system properties to use for interpolation. The system properties are collected from the - * runtime environment such as {@link System#getProperties()} and environment variables - * (prefixed with {@code env.}). - * - * @return the system properties, never {@code null} - */ - @Nonnull - Map getSystemProperties(); - /** * Each invocation computes a new map of effective properties. To be used in interpolation. *

@@ -121,14 +109,6 @@ public interface Session { @Nonnull Map getEffectiveProperties(@Nullable Project project); - /** - * Returns the current maven version. - * - * @return the maven version, never {@code null} - */ - @Nonnull - Version getMavenVersion(); - /** * Returns the degree of concurrency for the build. * @@ -136,37 +116,6 @@ public interface Session { */ int getDegreeOfConcurrency(); - /** - * Returns the start time of the session. - * - * @return the start time as an Instant object, never {@code null} - */ - @Nonnull - Instant getStartTime(); - - /** - * Gets the directory of the topmost project being built, usually the current directory or the - * directory pointed at by the {@code -f/--file} command line argument. - * - * @return the directory of the topmost project, never {@code null} - * @see Project#isTopProject() - * @see #getRootDirectory() - */ - @Nonnull - Path getTopDirectory(); - - /** - * Gets the root directory of the session, which is the root directory for the top directory project. - * - * @return the root directory, never {@code null} - * @throws IllegalStateException if the root directory could not be found - * @see #getTopDirectory() - * @see Project#getRootDirectory() - * @see Project#isRootProject() - */ - @Nonnull - Path getRootDirectory(); - /** * Retrieves a list of projects associated with the session. * diff --git a/api/maven-api-core/src/main/java/org/apache/maven/api/services/ArtifactCoordinatesFactoryRequest.java b/api/maven-api-core/src/main/java/org/apache/maven/api/services/ArtifactCoordinatesFactoryRequest.java index 495ddc7367..e887ffda11 100644 --- a/api/maven-api-core/src/main/java/org/apache/maven/api/services/ArtifactCoordinatesFactoryRequest.java +++ b/api/maven-api-core/src/main/java/org/apache/maven/api/services/ArtifactCoordinatesFactoryRequest.java @@ -167,7 +167,7 @@ public interface ArtifactCoordinatesFactoryRequest { session, groupId, artifactId, version, classifier, extension, type, coordinateString); } - private static class DefaultArtifactFactoryRequestArtifact extends BaseRequest + private static class DefaultArtifactFactoryRequestArtifact extends BaseRequest implements ArtifactCoordinatesFactoryRequest { private final String groupId; private final String artifactId; diff --git a/api/maven-api-core/src/main/java/org/apache/maven/api/services/ArtifactDeployerRequest.java b/api/maven-api-core/src/main/java/org/apache/maven/api/services/ArtifactDeployerRequest.java index b6bc678166..e82f7331e6 100644 --- a/api/maven-api-core/src/main/java/org/apache/maven/api/services/ArtifactDeployerRequest.java +++ b/api/maven-api-core/src/main/java/org/apache/maven/api/services/ArtifactDeployerRequest.java @@ -101,7 +101,8 @@ public interface ArtifactDeployerRequest { return new DefaultArtifactDeployerRequest(session, repository, artifacts, retryFailedDeploymentCount); } - private static class DefaultArtifactDeployerRequest extends BaseRequest implements ArtifactDeployerRequest { + private static class DefaultArtifactDeployerRequest extends BaseRequest + implements ArtifactDeployerRequest { private final RemoteRepository repository; private final Collection artifacts; diff --git a/api/maven-api-core/src/main/java/org/apache/maven/api/services/ArtifactFactoryRequest.java b/api/maven-api-core/src/main/java/org/apache/maven/api/services/ArtifactFactoryRequest.java index 41f1944bbf..7e373cdd38 100644 --- a/api/maven-api-core/src/main/java/org/apache/maven/api/services/ArtifactFactoryRequest.java +++ b/api/maven-api-core/src/main/java/org/apache/maven/api/services/ArtifactFactoryRequest.java @@ -136,7 +136,8 @@ public interface ArtifactFactoryRequest { session, groupId, artifactId, version, classifier, extension, type); } - private static class DefaultArtifactFactoryRequest extends BaseRequest implements ArtifactFactoryRequest { + private static class DefaultArtifactFactoryRequest extends BaseRequest + implements ArtifactFactoryRequest { private final String groupId; private final String artifactId; private final String version; diff --git a/api/maven-api-core/src/main/java/org/apache/maven/api/services/ArtifactInstallerRequest.java b/api/maven-api-core/src/main/java/org/apache/maven/api/services/ArtifactInstallerRequest.java index 4795b325dd..d3c0e26cce 100644 --- a/api/maven-api-core/src/main/java/org/apache/maven/api/services/ArtifactInstallerRequest.java +++ b/api/maven-api-core/src/main/java/org/apache/maven/api/services/ArtifactInstallerRequest.java @@ -83,7 +83,7 @@ public interface ArtifactInstallerRequest { return new DefaultArtifactInstallerRequest(session, artifacts); } - static class DefaultArtifactInstallerRequest extends BaseRequest implements ArtifactInstallerRequest { + static class DefaultArtifactInstallerRequest extends BaseRequest implements ArtifactInstallerRequest { private final Collection artifacts; 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 6fa14cc4fb..24be5cbb23 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 @@ -106,7 +106,8 @@ public interface ArtifactResolverRequest { return new DefaultArtifactResolverRequest(session, coordinates, repositories); } - private static class DefaultArtifactResolverRequest extends BaseRequest implements ArtifactResolverRequest { + private static class DefaultArtifactResolverRequest extends BaseRequest + implements ArtifactResolverRequest { @Nonnull private final Collection coordinates; diff --git a/api/maven-api-core/src/main/java/org/apache/maven/api/services/BaseRequest.java b/api/maven-api-core/src/main/java/org/apache/maven/api/services/BaseRequest.java index 8b777206df..59d330a9e0 100644 --- a/api/maven-api-core/src/main/java/org/apache/maven/api/services/BaseRequest.java +++ b/api/maven-api-core/src/main/java/org/apache/maven/api/services/BaseRequest.java @@ -22,7 +22,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Collections; -import org.apache.maven.api.Session; +import org.apache.maven.api.ProtoSession; import org.apache.maven.api.annotations.Experimental; import org.apache.maven.api.annotations.Nonnull; @@ -32,16 +32,16 @@ import org.apache.maven.api.annotations.Nonnull; * @since 4.0.0 */ @Experimental -abstract class BaseRequest { +abstract class BaseRequest { - private final Session session; + private final S session; - protected BaseRequest(@Nonnull Session session) { + protected BaseRequest(@Nonnull S session) { this.session = nonNull(session, "session cannot be null"); } @Nonnull - public Session getSession() { + public S getSession() { return session; } diff --git a/api/maven-api-core/src/main/java/org/apache/maven/api/services/DependencyCoordinatesFactoryRequest.java b/api/maven-api-core/src/main/java/org/apache/maven/api/services/DependencyCoordinatesFactoryRequest.java index 730838271a..d9cbbcb0ab 100644 --- a/api/maven-api-core/src/main/java/org/apache/maven/api/services/DependencyCoordinatesFactoryRequest.java +++ b/api/maven-api-core/src/main/java/org/apache/maven/api/services/DependencyCoordinatesFactoryRequest.java @@ -202,7 +202,7 @@ public interface DependencyCoordinatesFactoryRequest extends ArtifactCoordinates exclusions); } - private static class DefaultDependencyCoordinatesFactoryRequest extends BaseRequest + private static class DefaultDependencyCoordinatesFactoryRequest extends BaseRequest implements DependencyCoordinatesFactoryRequest { private final String groupId; private final String artifactId; 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 6b0598e605..dc8570a01a 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 @@ -360,7 +360,8 @@ public interface DependencyResolverRequest { repositories); } - static class DefaultDependencyResolverRequest extends BaseRequest implements DependencyResolverRequest { + static class DefaultDependencyResolverRequest extends BaseRequest + implements DependencyResolverRequest { private final RequestType requestType; private final Project project; private final Artifact rootArtifact; 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 f29d253e4d..435874c8a3 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 @@ -290,7 +290,7 @@ public interface ModelBuilderRequest { lifecycleBindingsInjector); } - private static class DefaultModelBuilderRequest extends BaseRequest implements ModelBuilderRequest { + private static class DefaultModelBuilderRequest extends BaseRequest implements ModelBuilderRequest { private final RequestType requestType; private final boolean locationTracking; private final boolean recursive; 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 35e0081356..770d9e1067 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 @@ -125,7 +125,8 @@ public interface ProjectBuilderRequest { session, path, source, allowStubModel, recursive, processPlugins, repositories); } - private static class DefaultProjectBuilderRequest extends BaseRequest implements ProjectBuilderRequest { + private static class DefaultProjectBuilderRequest extends BaseRequest + implements ProjectBuilderRequest { private final Path path; private final Source source; private final boolean allowStubModel; diff --git a/api/maven-api-core/src/main/java/org/apache/maven/api/services/SettingsBuilderRequest.java b/api/maven-api-core/src/main/java/org/apache/maven/api/services/SettingsBuilderRequest.java index 5c028e1e64..78a4fc5b07 100644 --- a/api/maven-api-core/src/main/java/org/apache/maven/api/services/SettingsBuilderRequest.java +++ b/api/maven-api-core/src/main/java/org/apache/maven/api/services/SettingsBuilderRequest.java @@ -23,7 +23,7 @@ import java.nio.file.Path; import java.util.Optional; import java.util.function.Function; -import org.apache.maven.api.Session; +import org.apache.maven.api.ProtoSession; import org.apache.maven.api.annotations.Experimental; import org.apache.maven.api.annotations.Immutable; import org.apache.maven.api.annotations.Nonnull; @@ -40,7 +40,7 @@ import static org.apache.maven.api.services.BaseRequest.nonNull; public interface SettingsBuilderRequest { @Nonnull - Session getSession(); + ProtoSession getSession(); /** * Gets the installation settings source. @@ -76,19 +76,21 @@ public interface SettingsBuilderRequest { @Nonnull static SettingsBuilderRequest build( - @Nonnull Session session, @Nonnull Source installationSettingsSource, @Nonnull Source userSettingsSource) { + @Nonnull ProtoSession session, + @Nonnull Source installationSettingsSource, + @Nonnull Source userSettingsSource) { return build(session, installationSettingsSource, null, userSettingsSource); } @Nonnull static SettingsBuilderRequest build( - @Nonnull Session session, @Nonnull Path installationSettingsPath, @Nonnull Path userSettingsPath) { + @Nonnull ProtoSession session, @Nonnull Path installationSettingsPath, @Nonnull Path userSettingsPath) { return build(session, Source.fromPath(installationSettingsPath), null, Source.fromPath(userSettingsPath)); } @Nonnull static SettingsBuilderRequest build( - @Nonnull Session session, + @Nonnull ProtoSession session, @Nullable Source installationSettingsSource, @Nullable Source projectSettingsSource, @Nullable Source userSettingsSource) { @@ -102,7 +104,7 @@ public interface SettingsBuilderRequest { @Nonnull static SettingsBuilderRequest build( - @Nonnull Session session, + @Nonnull ProtoSession session, @Nullable Path installationSettingsPath, @Nullable Path projectSettingsPath, @Nullable Path userSettingsPath) { @@ -130,13 +132,13 @@ public interface SettingsBuilderRequest { @NotThreadSafe class SettingsBuilderRequestBuilder { - Session session; + ProtoSession session; Source installationSettingsSource; Source projectSettingsSource; Source userSettingsSource; Function interpolationSource; - public SettingsBuilderRequestBuilder session(Session session) { + public SettingsBuilderRequestBuilder session(ProtoSession session) { this.session = session; return this; } @@ -170,7 +172,8 @@ public interface SettingsBuilderRequest { interpolationSource); } - private static class DefaultSettingsBuilderRequest extends BaseRequest implements SettingsBuilderRequest { + private static class DefaultSettingsBuilderRequest extends BaseRequest + implements SettingsBuilderRequest { private final Source installationSettingsSource; private final Source projectSettingsSource; private final Source userSettingsSource; @@ -178,7 +181,7 @@ public interface SettingsBuilderRequest { @SuppressWarnings("checkstyle:ParameterNumber") DefaultSettingsBuilderRequest( - @Nonnull Session session, + @Nonnull ProtoSession session, @Nullable Source installationSettingsSource, @Nullable Source projectSettingsSource, @Nullable Source userSettingsSource, diff --git a/api/maven-api-core/src/main/java/org/apache/maven/api/services/ToolchainsBuilderRequest.java b/api/maven-api-core/src/main/java/org/apache/maven/api/services/ToolchainsBuilderRequest.java index bfe3241fe4..d6502bb562 100644 --- a/api/maven-api-core/src/main/java/org/apache/maven/api/services/ToolchainsBuilderRequest.java +++ b/api/maven-api-core/src/main/java/org/apache/maven/api/services/ToolchainsBuilderRequest.java @@ -22,7 +22,7 @@ import java.nio.file.Files; import java.nio.file.Path; import java.util.Optional; -import org.apache.maven.api.Session; +import org.apache.maven.api.ProtoSession; import org.apache.maven.api.annotations.Experimental; import org.apache.maven.api.annotations.Nonnull; import org.apache.maven.api.annotations.NotThreadSafe; @@ -37,7 +37,7 @@ import static org.apache.maven.api.services.BaseRequest.nonNull; @Experimental public interface ToolchainsBuilderRequest { @Nonnull - Session getSession(); + ProtoSession getSession(); /** * Gets the installation Toolchains source. @@ -57,7 +57,7 @@ public interface ToolchainsBuilderRequest { @Nonnull static ToolchainsBuilderRequest build( - @Nonnull Session session, + @Nonnull ProtoSession session, @Nullable Source installationToolchainsFile, @Nullable Source userToolchainsSource) { return builder() @@ -69,7 +69,9 @@ public interface ToolchainsBuilderRequest { @Nonnull static ToolchainsBuilderRequest build( - @Nonnull Session session, @Nullable Path installationToolchainsFile, @Nullable Path userToolchainsPath) { + @Nonnull ProtoSession session, + @Nullable Path installationToolchainsFile, + @Nullable Path userToolchainsPath) { return builder() .session(nonNull(session, "session cannot be null")) .installationToolchainsSource( @@ -90,11 +92,11 @@ public interface ToolchainsBuilderRequest { @NotThreadSafe class ToolchainsBuilderRequestBuilder { - Session session; + ProtoSession session; Source installationToolchainsSource; Source userToolchainsSource; - public ToolchainsBuilderRequestBuilder session(Session session) { + public ToolchainsBuilderRequestBuilder session(ProtoSession session) { this.session = session; return this; } @@ -114,13 +116,14 @@ public interface ToolchainsBuilderRequest { session, installationToolchainsSource, userToolchainsSource); } - private static class DefaultToolchainsBuilderRequest extends BaseRequest implements ToolchainsBuilderRequest { + private static class DefaultToolchainsBuilderRequest extends BaseRequest + implements ToolchainsBuilderRequest { private final Source installationToolchainsSource; private final Source userToolchainsSource; @SuppressWarnings("checkstyle:ParameterNumber") DefaultToolchainsBuilderRequest( - @Nonnull Session session, + @Nonnull ProtoSession session, @Nullable Source installationToolchainsSource, @Nullable Source userToolchainsSource) { super(session); 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 99f4f3ecc9..cd1eb9ce65 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 @@ -94,7 +94,8 @@ public interface VersionRangeResolverRequest { return new DefaultVersionResolverRequest(session, artifactCoordinates, repositories); } - private static class DefaultVersionResolverRequest extends BaseRequest implements VersionRangeResolverRequest { + private static class DefaultVersionResolverRequest extends BaseRequest + implements VersionRangeResolverRequest { private final ArtifactCoordinates artifactCoordinates; private final List 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 b0c9fe2f7f..5c7032a6e5 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 @@ -96,7 +96,8 @@ public interface VersionResolverRequest { return new DefaultVersionResolverRequest(session, artifactCoordinates, repositories); } - private static class DefaultVersionResolverRequest extends BaseRequest implements VersionResolverRequest { + private static class DefaultVersionResolverRequest extends BaseRequest + implements VersionResolverRequest { private final ArtifactCoordinates artifactCoordinates; private final List repositories; diff --git a/api/maven-api-spi/src/main/java/org/apache/maven/api/spi/PropertyContributor.java b/api/maven-api-spi/src/main/java/org/apache/maven/api/spi/PropertyContributor.java index 139ec94037..97bd4f2430 100644 --- a/api/maven-api-spi/src/main/java/org/apache/maven/api/spi/PropertyContributor.java +++ b/api/maven-api-spi/src/main/java/org/apache/maven/api/spi/PropertyContributor.java @@ -18,8 +18,10 @@ */ package org.apache.maven.api.spi; +import java.util.HashMap; import java.util.Map; +import org.apache.maven.api.ProtoSession; import org.apache.maven.api.annotations.Consumer; import org.apache.maven.api.annotations.Experimental; import org.apache.maven.api.di.Named; @@ -38,6 +40,21 @@ public interface PropertyContributor extends SpiService { * Invoked just before session is created with a mutable map that carries collected user properties so far. * * @param userProperties The mutable user properties, never {@code null}. + * @see #contribute(ProtoSession) */ - void contribute(Map userProperties); + default void contribute(Map userProperties) {} + + /** + * Invoked just before session is created with proto session instance. The proto session contains user and + * system properties collected so far, along with other information. This method should return altered + * (contributions applied) user properties, not only the "new" or "added" properties! + * + * @param protoSession The proto session, never {@code null}. + * @return The user properties with contributions. + */ + default Map contribute(ProtoSession protoSession) { + HashMap userProperties = new HashMap<>(protoSession.getUserProperties()); + contribute(userProperties); + return userProperties; + } } diff --git a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/LookupContext.java b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/LookupContext.java index e5af6e8513..51a2ad96d9 100644 --- a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/LookupContext.java +++ b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/LookupContext.java @@ -27,13 +27,12 @@ import java.util.Map; import java.util.function.Consumer; import java.util.function.Function; -import org.apache.maven.api.Session; +import org.apache.maven.api.ProtoSession; import org.apache.maven.api.cli.InvokerException; import org.apache.maven.api.cli.InvokerRequest; import org.apache.maven.api.cli.Logger; import org.apache.maven.api.services.Lookup; import org.apache.maven.api.settings.Settings; -import org.apache.maven.cling.invoker.mvn.ProtoSession; import org.apache.maven.cling.logging.Slf4jConfiguration; import org.jline.terminal.Terminal; import org.slf4j.ILoggerFactory; @@ -46,7 +45,6 @@ public class LookupContext implements AutoCloseable { public final Function cwdResolver; public final Function installationResolver; public final Function userResolver; - public final Session session; protected LookupContext(InvokerRequest invokerRequest) { this.invokerRequest = requireNonNull(invokerRequest); @@ -58,12 +56,23 @@ public class LookupContext implements AutoCloseable { this.logger = invokerRequest.parserRequest().logger(); Map user = new HashMap<>(invokerRequest.userProperties()); - user.put("session.rootDirectory", invokerRequest.rootDirectory().toString()); user.put("session.topDirectory", invokerRequest.topDirectory().toString()); - Map system = new HashMap<>(invokerRequest.systemProperties()); - this.session = ProtoSession.create(user, system); + if (invokerRequest.rootDirectory().isEmpty()) { + user.put( + "session.rootDirectory", + invokerRequest.rootDirectory().get().toString()); + } + this.protoSession = ProtoSession.newBuilder() + .withSystemProperties(invokerRequest.systemProperties()) + .withUserProperties(user) + .withTopDirectory(invokerRequest.topDirectory()) + .withRootDirectory(invokerRequest.rootDirectory().orElse(null)) + .build(); } + // this one "evolves" as process progresses (instance is immutable but instances are replaced) + public ProtoSession protoSession; + public Logger logger; public ILoggerFactory loggerFactory; public Slf4jConfiguration slf4jConfiguration; @@ -76,9 +85,6 @@ public class LookupContext implements AutoCloseable { public boolean interactive; public Path localRepositoryPath; - public Path installationSettingsPath; - public Path projectSettingsPath; - public Path userSettingsPath; public Settings effectiveSettings; public final List closeables = new ArrayList<>(); diff --git a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/LookupInvoker.java b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/LookupInvoker.java index ae229ec7c0..766414d165 100644 --- a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/LookupInvoker.java +++ b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/LookupInvoker.java @@ -33,6 +33,7 @@ import java.util.function.Consumer; import java.util.function.Function; import org.apache.maven.api.Constants; +import org.apache.maven.api.ProtoSession; import org.apache.maven.api.cli.Invoker; import org.apache.maven.api.cli.InvokerException; import org.apache.maven.api.cli.InvokerRequest; @@ -40,6 +41,7 @@ import org.apache.maven.api.cli.Logger; import org.apache.maven.api.cli.Options; import org.apache.maven.api.services.BuilderProblem; import org.apache.maven.api.services.Interpolator; +import org.apache.maven.api.services.Lookup; import org.apache.maven.api.services.MavenException; import org.apache.maven.api.services.MessageBuilder; import org.apache.maven.api.services.SettingsBuilder; @@ -52,12 +54,14 @@ import org.apache.maven.api.settings.Proxy; import org.apache.maven.api.settings.Repository; import org.apache.maven.api.settings.Server; import org.apache.maven.api.settings.Settings; +import org.apache.maven.api.spi.PropertyContributor; import org.apache.maven.artifact.InvalidRepositoryException; import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy; import org.apache.maven.artifact.repository.MavenArtifactRepository; import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout; import org.apache.maven.bridge.MavenRepositorySystem; +import org.apache.maven.cling.invoker.spi.PropertyContributorsHolder; import org.apache.maven.cling.logging.Slf4jConfiguration; import org.apache.maven.cling.logging.Slf4jConfigurationFactory; import org.apache.maven.cling.transfer.ConsoleMavenTransferListener; @@ -124,7 +128,7 @@ public abstract class LookupInvoker implements Invoker } protected int doInvoke(C context) throws Exception { - pushProperties(context); + pushCoreProperties(context); validate(context); prepare(context); configureLogging(context); @@ -132,6 +136,8 @@ public abstract class LookupInvoker implements Invoker helpOrVersionAndMayExit(context); preCommands(context); container(context); + postContainer(context); + pushUserProperties(context); lookup(context); init(context); postCommands(context); @@ -157,14 +163,18 @@ public abstract class LookupInvoker implements Invoker protected abstract C createContext(InvokerRequest invokerRequest) throws InvokerException; - protected void pushProperties(C context) throws Exception { - InvokerRequest invokerRequest = context.invokerRequest; - HashSet sys = new HashSet<>(invokerRequest.systemProperties().keySet()); - invokerRequest.userProperties().entrySet().stream() + protected void pushCoreProperties(C context) throws Exception { + System.setProperty( + Constants.MAVEN_HOME, + context.invokerRequest.installationDirectory().toString()); + } + + protected void pushUserProperties(C context) throws Exception { + ProtoSession protoSession = context.protoSession; + HashSet sys = new HashSet<>(protoSession.getSystemProperties().keySet()); + protoSession.getUserProperties().entrySet().stream() .filter(k -> !sys.contains(k.getKey())) .forEach(k -> System.setProperty(k.getKey(), k.getValue())); - System.setProperty( - Constants.MAVEN_HOME, invokerRequest.installationDirectory().toString()); } protected void validate(C context) throws Exception {} @@ -172,10 +182,9 @@ public abstract class LookupInvoker implements Invoker protected void prepare(C context) throws Exception {} protected void configureLogging(C context) throws Exception { - InvokerRequest invokerRequest = context.invokerRequest; // LOG COLOR - Options mavenOptions = invokerRequest.options(); - Map userProperties = invokerRequest.userProperties(); + Options mavenOptions = context.invokerRequest.options(); + Map userProperties = context.protoSession.getUserProperties(); String styleColor = mavenOptions .color() .orElse(userProperties.getOrDefault( @@ -381,6 +390,19 @@ public abstract class LookupInvoker implements Invoker return new PlexusContainerCapsuleFactory<>(); } + protected void postContainer(C context) throws Exception { + ProtoSession protoSession = context.protoSession; + for (PropertyContributor propertyContributor : context.lookup + .lookup(PropertyContributorsHolder.class) + .getPropertyContributors() + .values()) { + protoSession = protoSession.toBuilder() + .withUserProperties(propertyContributor.contribute(protoSession)) + .build(); + } + context.protoSession = protoSession; + } + protected void lookup(C context) throws Exception {} protected void init(C context) throws Exception {} @@ -427,7 +449,8 @@ public abstract class LookupInvoker implements Invoker throw new FileNotFoundException("The specified user settings file does not exist: " + userSettingsFile); } } else { - String userSettingsFileStr = context.invokerRequest.userProperties().get(Constants.MAVEN_USER_SETTINGS); + String userSettingsFileStr = + context.protoSession.getUserProperties().get(Constants.MAVEN_USER_SETTINGS); if (userSettingsFileStr != null) { userSettingsFile = context.userResolver.apply(userSettingsFileStr); } @@ -445,7 +468,7 @@ public abstract class LookupInvoker implements Invoker } } else { String projectSettingsFileStr = - context.invokerRequest.userProperties().get(Constants.MAVEN_PROJECT_SETTINGS); + context.protoSession.getUserProperties().get(Constants.MAVEN_PROJECT_SETTINGS); if (projectSettingsFileStr != null) { projectSettingsFile = context.cwdResolver.apply(projectSettingsFileStr); } @@ -463,20 +486,16 @@ public abstract class LookupInvoker implements Invoker } } else { String installationSettingsFileStr = - context.invokerRequest.userProperties().get(Constants.MAVEN_INSTALLATION_SETTINGS); + context.protoSession.getUserProperties().get(Constants.MAVEN_INSTALLATION_SETTINGS); if (installationSettingsFileStr != null) { installationSettingsFile = context.installationResolver.apply(installationSettingsFileStr); } } - context.installationSettingsPath = installationSettingsFile; - context.projectSettingsPath = projectSettingsFile; - context.userSettingsPath = userSettingsFile; - Function interpolationSource = Interpolator.chain( - context.invokerRequest.userProperties()::get, context.invokerRequest.systemProperties()::get); + context.protoSession.getUserProperties()::get, context.protoSession.getSystemProperties()::get); SettingsBuilderRequest settingsRequest = SettingsBuilderRequest.builder() - .session(context.session) + .session(context.protoSession) .installationSettingsSource( installationSettingsFile != null && Files.exists(installationSettingsFile) ? Source.fromPath(installationSettingsFile) @@ -540,9 +559,9 @@ public abstract class LookupInvoker implements Invoker protected Path localRepositoryPath(C context) { // user override - String userDefinedLocalRepo = context.invokerRequest.userProperties().get(Constants.MAVEN_REPO_LOCAL); + String userDefinedLocalRepo = context.protoSession.getUserProperties().get(Constants.MAVEN_REPO_LOCAL); if (userDefinedLocalRepo == null) { - userDefinedLocalRepo = context.invokerRequest.systemProperties().get(Constants.MAVEN_REPO_LOCAL); + userDefinedLocalRepo = context.protoSession.getUserProperties().get(Constants.MAVEN_REPO_LOCAL); if (userDefinedLocalRepo != null) { context.logger.warn("The property '" + Constants.MAVEN_REPO_LOCAL + "' has been set using a JVM system property which is deprecated. " @@ -560,11 +579,11 @@ public abstract class LookupInvoker implements Invoker } // defaults return context.userResolver - .apply(context.invokerRequest.userProperties().get(Constants.MAVEN_USER_CONF)) + .apply(context.protoSession.getUserProperties().get(Constants.MAVEN_USER_CONF)) .resolve("repository"); } - protected void populateRequest(C context, MavenExecutionRequest request) throws Exception { + protected void populateRequest(C context, Lookup lookup, MavenExecutionRequest request) throws Exception { populateRequestFromSettings(request, context.effectiveSettings); Options options = context.invokerRequest.options(); @@ -575,8 +594,8 @@ public abstract class LookupInvoker implements Invoker request.setInteractiveMode(context.interactive); request.setShowErrors(options.showErrors().orElse(false)); request.setBaseDirectory(context.invokerRequest.topDirectory().toFile()); - request.setSystemProperties(toProperties(context.invokerRequest.systemProperties())); - request.setUserProperties(toProperties(context.invokerRequest.userProperties())); + request.setSystemProperties(toProperties(context.protoSession.getSystemProperties())); + request.setUserProperties(toProperties(context.protoSession.getUserProperties())); request.setTopDirectory(context.invokerRequest.topDirectory()); if (context.invokerRequest.rootDirectory().isPresent()) { @@ -720,7 +739,7 @@ public abstract class LookupInvoker implements Invoker } protected boolean isRunningOnCI(C context) { - String ciEnv = context.invokerRequest.systemProperties().get("env.CI"); + String ciEnv = context.protoSession.getSystemProperties().get("env.CI"); return ciEnv != null && !"false".equals(ciEnv); } diff --git a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/PlexusContainerCapsuleFactory.java b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/PlexusContainerCapsuleFactory.java index bfcef51282..14673ef88d 100644 --- a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/PlexusContainerCapsuleFactory.java +++ b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/PlexusContainerCapsuleFactory.java @@ -30,6 +30,7 @@ import java.util.function.Function; import com.google.inject.AbstractModule; import com.google.inject.Module; import org.apache.maven.api.Constants; +import org.apache.maven.api.ProtoSession; import org.apache.maven.api.cli.InvokerException; import org.apache.maven.api.cli.InvokerRequest; import org.apache.maven.api.cli.Logger; @@ -47,6 +48,7 @@ import org.apache.maven.execution.scope.internal.MojoExecutionScope; import org.apache.maven.execution.scope.internal.MojoExecutionScopeModule; import org.apache.maven.extension.internal.CoreExports; import org.apache.maven.extension.internal.CoreExtensionEntry; +import org.apache.maven.internal.impl.DefaultLookup; import org.apache.maven.session.scope.internal.SessionScope; import org.apache.maven.session.scope.internal.SessionScopeModule; import org.codehaus.plexus.ContainerConfiguration; @@ -108,11 +110,11 @@ public class PlexusContainerCapsuleFactory implements C Thread.currentThread().setContextClassLoader(container.getContainerRealm()); container.setLoggerManager(createLoggerManager()); - InvokerRequest invokerRequest = context.invokerRequest; + ProtoSession protoSession = context.protoSession; Function extensionSource = expression -> { - String value = invokerRequest.userProperties().get(expression); + String value = protoSession.getUserProperties().get(expression); if (value == null) { - value = invokerRequest.systemProperties().get(expression); + value = protoSession.getSystemProperties().get(expression); } return value; }; @@ -189,10 +191,10 @@ public class PlexusContainerCapsuleFactory implements C protected void customizeContainer(C context, PlexusContainer container) throws Exception {} protected List parseExtClasspath(C context) throws Exception { - InvokerRequest invokerRequest = context.invokerRequest; - String extClassPath = invokerRequest.userProperties().get(Constants.MAVEN_EXT_CLASS_PATH); + ProtoSession protoSession = context.protoSession; + String extClassPath = protoSession.getUserProperties().get(Constants.MAVEN_EXT_CLASS_PATH); if (extClassPath == null) { - extClassPath = invokerRequest.systemProperties().get(Constants.MAVEN_EXT_CLASS_PATH); + extClassPath = protoSession.getSystemProperties().get(Constants.MAVEN_EXT_CLASS_PATH); if (extClassPath != null) { context.logger.warn("The property '" + Constants.MAVEN_EXT_CLASS_PATH + "' has been set using a JVM system property which is deprecated. " @@ -272,7 +274,7 @@ public class PlexusContainerCapsuleFactory implements C DefaultPlexusContainer container = new DefaultPlexusContainer(cc, new AbstractModule() { @Override protected void configure() { - bind(ILoggerFactory.class).toInstance(context.loggerFactory); + bind(ILoggerFactory.class).toProvider(() -> context.loggerFactory); } }); @@ -286,7 +288,7 @@ public class PlexusContainerCapsuleFactory implements C invoker.settings(context, container.lookup(SettingsBuilder.class)); MavenExecutionRequest mer = new DefaultMavenExecutionRequest(); - invoker.populateRequest(context, mer); + invoker.populateRequest(context, new DefaultLookup(container), mer); mer = container.lookup(MavenExecutionRequestPopulator.class).populateDefaults(mer); return Collections.unmodifiableList(container .lookup(BootstrapCoreExtensionManager.class) diff --git a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvn/MavenContext.java b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvn/MavenContext.java index 26d9c2b512..7ffbcab9e9 100644 --- a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvn/MavenContext.java +++ b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvn/MavenContext.java @@ -20,11 +20,8 @@ package org.apache.maven.cling.invoker.mvn; import org.apache.maven.Maven; import org.apache.maven.api.cli.InvokerRequest; -import org.apache.maven.api.services.model.ModelProcessor; import org.apache.maven.cling.invoker.LookupContext; import org.apache.maven.eventspy.internal.EventSpyDispatcher; -import org.apache.maven.execution.MavenExecutionRequest; -import org.apache.maven.execution.MavenExecutionRequestPopulator; import org.apache.maven.logging.BuildEventListener; @SuppressWarnings("VisibilityModifier") @@ -34,9 +31,7 @@ public class MavenContext extends LookupContext { } public BuildEventListener buildEventListener; - public MavenExecutionRequest mavenExecutionRequest; public EventSpyDispatcher eventSpyDispatcher; - public MavenExecutionRequestPopulator mavenExecutionRequestPopulator; - public ModelProcessor modelProcessor; + public Maven maven; } diff --git a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvn/MavenInvoker.java b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvn/MavenInvoker.java index 9fdb40c148..ffcf67c3c2 100644 --- a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvn/MavenInvoker.java +++ b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvn/MavenInvoker.java @@ -38,6 +38,7 @@ import org.apache.maven.api.cli.InvokerRequest; import org.apache.maven.api.cli.Logger; import org.apache.maven.api.cli.mvn.MavenOptions; import org.apache.maven.api.services.BuilderProblem; +import org.apache.maven.api.services.Lookup; import org.apache.maven.api.services.SettingsBuilderRequest; import org.apache.maven.api.services.SettingsBuilderResult; import org.apache.maven.api.services.Source; @@ -88,13 +89,13 @@ public abstract class MavenInvoker extends LookupInvoker @Override protected int execute(C context) throws Exception { - toolchains(context); - populateRequest(context, context.mavenExecutionRequest); - return doExecute(context); + MavenExecutionRequest request = prepareMavenExecutionRequest(); + toolchains(context, request); + populateRequest(context, context.lookup, request); + return doExecute(context, request); } - @Override - protected void prepare(C context) throws Exception { + protected MavenExecutionRequest prepareMavenExecutionRequest() throws Exception { // explicitly fill in "defaults"? DefaultMavenExecutionRequest mavenExecutionRequest = new DefaultMavenExecutionRequest(); mavenExecutionRequest.setRepositoryCache(new DefaultRepositoryCache()); @@ -108,26 +109,24 @@ public abstract class MavenInvoker extends LookupInvoker mavenExecutionRequest.setLoggingLevel(MavenExecutionRequest.LOGGING_LEVEL_INFO); mavenExecutionRequest.setDegreeOfConcurrency(1); mavenExecutionRequest.setBuilderId("singlethreaded"); - - context.mavenExecutionRequest = mavenExecutionRequest; + return mavenExecutionRequest; } @Override protected void lookup(C context) throws Exception { context.eventSpyDispatcher = context.lookup.lookup(EventSpyDispatcher.class); - context.mavenExecutionRequestPopulator = context.lookup.lookup(MavenExecutionRequestPopulator.class); - context.modelProcessor = context.lookup.lookup(ModelProcessor.class); context.maven = context.lookup.lookup(Maven.class); } @Override protected void init(C context) throws Exception { + super.init(context); InvokerRequest invokerRequest = context.invokerRequest; Map data = new HashMap<>(); data.put("plexus", context.lookup.lookup(PlexusContainer.class)); data.put("workingDirectory", invokerRequest.cwd().toString()); - data.put("systemProperties", toProperties(invokerRequest.systemProperties())); - data.put("userProperties", toProperties(invokerRequest.userProperties())); + data.put("systemProperties", toProperties(context.protoSession.getSystemProperties())); + data.put("userProperties", toProperties(context.protoSession.getUserProperties())); data.put("versionProperties", CLIReportingUtils.getBuildProperties()); context.eventSpyDispatcher.init(() -> data); } @@ -181,7 +180,7 @@ public abstract class MavenInvoker extends LookupInvoker } } - protected void toolchains(C context) throws Exception { + protected void toolchains(C context, MavenExecutionRequest request) throws Exception { Path userToolchainsFile = null; if (context.invokerRequest.options().altUserToolchains().isPresent()) { @@ -194,7 +193,7 @@ public abstract class MavenInvoker extends LookupInvoker } } else { String userToolchainsFileStr = - context.invokerRequest.userProperties().get(Constants.MAVEN_USER_TOOLCHAINS); + context.protoSession.getUserProperties().get(Constants.MAVEN_USER_TOOLCHAINS); if (userToolchainsFileStr != null) { userToolchainsFile = context.cwdResolver.apply(userToolchainsFileStr); } @@ -212,19 +211,18 @@ public abstract class MavenInvoker extends LookupInvoker } } else { String installationToolchainsFileStr = - context.invokerRequest.userProperties().get(Constants.MAVEN_INSTALLATION_TOOLCHAINS); + context.protoSession.getUserProperties().get(Constants.MAVEN_INSTALLATION_TOOLCHAINS); if (installationToolchainsFileStr != null) { installationToolchainsFile = context.cwdResolver.apply(installationToolchainsFileStr); } } - context.mavenExecutionRequest.setInstallationToolchainsFile( + request.setInstallationToolchainsFile( installationToolchainsFile != null ? installationToolchainsFile.toFile() : null); - context.mavenExecutionRequest.setUserToolchainsFile( - userToolchainsFile != null ? userToolchainsFile.toFile() : null); + request.setUserToolchainsFile(userToolchainsFile != null ? userToolchainsFile.toFile() : null); ToolchainsBuilderRequest toolchainsRequest = ToolchainsBuilderRequest.builder() - .session(context.session) + .session(context.protoSession) .installationToolchainsSource( installationToolchainsFile != null && Files.isRegularFile(installationToolchainsFile) ? Source.fromPath(installationToolchainsFile) @@ -245,9 +243,12 @@ public abstract class MavenInvoker extends LookupInvoker context.eventSpyDispatcher.onEvent(toolchainsResult); - context.mavenExecutionRequestPopulator.populateFromToolchains( - context.mavenExecutionRequest, - new org.apache.maven.toolchain.model.PersistedToolchains(toolchainsResult.getEffectiveToolchains())); + context.lookup + .lookup(MavenExecutionRequestPopulator.class) + .populateFromToolchains( + request, + new org.apache.maven.toolchain.model.PersistedToolchains( + toolchainsResult.getEffectiveToolchains())); if (!toolchainsResult.getProblems().isEmpty()) { context.logger.warn(""); @@ -262,8 +263,8 @@ public abstract class MavenInvoker extends LookupInvoker } @Override - protected void populateRequest(C context, MavenExecutionRequest request) throws Exception { - super.populateRequest(context, request); + protected void populateRequest(C context, Lookup lookup, MavenExecutionRequest request) throws Exception { + super.populateRequest(context, lookup, request); if (context.invokerRequest.rootDirectory().isEmpty()) { // maven requires this to be set; so default it (and see below at POM) request.setMultiModuleProjectDirectory( @@ -280,7 +281,7 @@ public abstract class MavenInvoker extends LookupInvoker request.setUpdateSnapshots(options.updateSnapshots().orElse(false)); request.setGlobalChecksumPolicy(determineGlobalChecksumPolicy(context)); - Path pom = determinePom(context); + Path pom = determinePom(context, lookup); if (pom != null) { request.setPom(pom.toFile()); if (pom.getParent() != null) { @@ -344,14 +345,16 @@ public abstract class MavenInvoker extends LookupInvoker } } - protected Path determinePom(C context) { + protected Path determinePom(C context, Lookup lookup) { Path current = context.invokerRequest.cwd(); MavenOptions options = (MavenOptions) context.invokerRequest.options(); if (options.alternatePomFile().isPresent()) { current = context.cwdResolver.apply(options.alternatePomFile().get()); } - if (context.modelProcessor != null) { - return context.modelProcessor.locateExistingPom(current); + ModelProcessor modelProcessor = + lookup.lookupOptional(ModelProcessor.class).orElse(null); + if (modelProcessor != null) { + return modelProcessor.locateExistingPom(current); } else { return Files.isRegularFile(current) ? current : null; } @@ -467,9 +470,7 @@ public abstract class MavenInvoker extends LookupInvoker } } - protected int doExecute(C context) throws Exception { - MavenExecutionRequest request = context.mavenExecutionRequest; - + protected int doExecute(C context, MavenExecutionRequest request) throws Exception { context.eventSpyDispatcher.onEvent(request); MavenExecutionResult result; diff --git a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvn/ProtoSession.java b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvn/ProtoSession.java deleted file mode 100644 index 9a19c21eb8..0000000000 --- a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvn/ProtoSession.java +++ /dev/null @@ -1,409 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.maven.cling.invoker.mvn; - -import java.nio.file.Path; -import java.nio.file.Paths; -import java.time.Instant; -import java.util.Collections; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.Optional; -import java.util.concurrent.ConcurrentHashMap; - -import org.apache.maven.api.Artifact; -import org.apache.maven.api.Lifecycle; -import org.apache.maven.api.Packaging; -import org.apache.maven.api.ProducedArtifact; -import org.apache.maven.api.Project; -import org.apache.maven.api.RemoteRepository; -import org.apache.maven.api.Session; -import org.apache.maven.api.Type; -import org.apache.maven.api.Version; -import org.apache.maven.api.di.Provides; -import org.apache.maven.api.di.SessionScoped; -import org.apache.maven.api.model.PluginContainer; -import org.apache.maven.api.model.Profile; -import org.apache.maven.api.services.ArtifactManager; -import org.apache.maven.api.services.LifecycleRegistry; -import org.apache.maven.api.services.Lookup; -import org.apache.maven.api.services.MavenException; -import org.apache.maven.api.services.PackagingRegistry; -import org.apache.maven.api.services.RepositoryFactory; -import org.apache.maven.api.services.SettingsBuilder; -import org.apache.maven.api.services.TypeRegistry; -import org.apache.maven.api.settings.Settings; -import org.apache.maven.api.spi.TypeProvider; -import org.apache.maven.di.Injector; -import org.apache.maven.di.Key; -import org.apache.maven.di.impl.DIException; -import org.apache.maven.internal.impl.AbstractSession; -import org.apache.maven.internal.impl.InternalSession; -import org.apache.maven.internal.impl.di.SessionScope; -import org.eclipse.aether.DefaultRepositorySystemSession; -import org.eclipse.aether.RepositorySystem; -import org.eclipse.aether.RepositorySystemSession; -import org.eclipse.aether.impl.RemoteRepositoryManager; -import org.eclipse.aether.repository.LocalRepository; -import org.eclipse.aether.repository.LocalRepositoryManager; - -public class ProtoSession extends AbstractSession { - - private final Map userProperties; - private final Map systemProperties; - private final Instant startTime = Instant.now(); - - ProtoSession(RepositorySystemSession session, RepositorySystem repositorySystem, Lookup lookup) { - this(session, repositorySystem, Collections.emptyList(), null, lookup, new Context(Map.of(), Map.of())); - } - - protected ProtoSession( - RepositorySystemSession session, - RepositorySystem repositorySystem, - List repositories, - List resolverRepositories, - Lookup lookup, - Context context) { - super(session, repositorySystem, repositories, resolverRepositories, lookup); - userProperties = context.userProperties; - systemProperties = context.systemProperties; - } - - /** - * - */ - public static Session create() { - Map properties = new HashMap<>(); - // Env variables prefixed with "env." - System.getenv().forEach((k, v) -> properties.put("env." + k, v)); - // Java System properties - System.getProperties().forEach((k, v) -> properties.put(k.toString(), v.toString())); - // create session - return create(Map.of(), properties); - } - - /** - * Create a new session. - */ - public static Session create(Map userProperties, Map systemProperties) { - Injector injector = Injector.create(); - injector.bindInstance(Injector.class, injector); - injector.bindInstance(Context.class, new Context(userProperties, systemProperties)); - injector.bindImplicit(Providers.class); - injector.discover(ProtoSession.class.getClassLoader()); - Session session = injector.getInstance(Session.class); - SessionScope scope = new SessionScope(); - scope.enter(); - scope.seed(Session.class, session); - injector.bindScope(SessionScoped.class, scope); - return session; - } - - record Context(Map userProperties, Map systemProperties) {} - - @Override - protected Session newSession(RepositorySystemSession session, List repositories) { - return new ProtoSession( - session, repositorySystem, repositories, null, lookup, new Context(userProperties, systemProperties)); - } - - @Override - public Settings getSettings() { - return Settings.newInstance(); - } - - @Override - public Map getUserProperties() { - return Map.of(); - } - - @Override - public Map getSystemProperties() { - return systemProperties; - } - - @Override - public Map getEffectiveProperties(Project project) { - HashMap result = new HashMap<>(getSystemProperties()); - if (project != null) { - result.putAll(project.getModel().getProperties()); - } - result.putAll(getUserProperties()); - return result; - } - - @Override - public Version getMavenVersion() { - return null; - } - - @Override - public int getDegreeOfConcurrency() { - return 0; - } - - @Override - public Instant getStartTime() { - return startTime; - } - - @Override - public Path getTopDirectory() { - return null; - } - - @Override - public Path getRootDirectory() { - throw new IllegalStateException(); - } - - @Override - public List getProjects() { - return List.of(); - } - - @Override - public Map getPluginContext(Project project) { - throw new UnsupportedInStandaloneModeException(); - } - - static class Providers { - - @Provides - @SuppressWarnings("unused") - static Lookup newLookup(Injector injector) { - return new Lookup() { - @Override - public T lookup(Class type) { - try { - return injector.getInstance(type); - } catch (DIException e) { - throw new MavenException("Unable to locate instance of type " + type, e); - } - } - - @Override - public T lookup(Class type, String name) { - try { - return injector.getInstance(Key.of(type, name)); - } catch (DIException e) { - throw new MavenException("Unable to locate instance of type " + type, e); - } - } - - @Override - public Optional lookupOptional(Class type) { - try { - return Optional.of(injector.getInstance(type)); - } catch (DIException e) { - return Optional.empty(); - } - } - - @Override - public Optional lookupOptional(Class type, String name) { - try { - return Optional.of(injector.getInstance(Key.of(type, name))); - } catch (DIException e) { - return Optional.empty(); - } - } - - @Override - public List lookupList(Class type) { - return injector.getInstance(new Key>() {}); - } - - @Override - public Map lookupMap(Class type) { - return injector.getInstance(new Key>() {}); - } - }; - } - - @Provides - @SuppressWarnings("unused") - static ArtifactManager newArtifactManager() { - return new ArtifactManager() { - private final Map paths = new ConcurrentHashMap<>(); - - @Override - public Optional getPath(Artifact artifact) { - return Optional.ofNullable(paths.get(artifact)); - } - - @Override - public void setPath(ProducedArtifact artifact, Path path) { - paths.put(artifact, path); - } - }; - } - - @Provides - @SuppressWarnings("unused") - static PackagingRegistry newPackagingRegistry(TypeRegistry typeRegistry) { - return id -> Optional.of(new DumbPackaging(id, typeRegistry.require(id), Map.of())); - } - - @Provides - @SuppressWarnings("unused") - static TypeRegistry newTypeRegistry(List providers) { - return new TypeRegistry() { - @Override - public Optional lookup(String id) { - return providers.stream() - .flatMap(p -> p.provides().stream()) - .filter(t -> Objects.equals(id, t.id())) - .findAny(); - } - }; - } - - @Provides - @SuppressWarnings("unused") - static LifecycleRegistry newLifecycleRegistry() { - return new LifecycleRegistry() { - - @Override - public Iterator iterator() { - return Collections.emptyIterator(); - } - - @Override - public Optional lookup(String id) { - return Optional.empty(); - } - - @Override - public List computePhases(Lifecycle lifecycle) { - return List.of(); - } - }; - } - - @Provides - @SuppressWarnings("unused") - static RepositorySystemSupplier newRepositorySystemSupplier() { - return new RepositorySystemSupplier(); - } - - @Provides - @SuppressWarnings("unused") - static RepositorySystem newRepositorySystem(RepositorySystemSupplier repositorySystemSupplier) { - return repositorySystemSupplier.getRepositorySystem(); - } - - @Provides - @SuppressWarnings("unused") - static RemoteRepositoryManager newRemoteRepositoryManager(RepositorySystemSupplier repositorySystemSupplier) { - return repositorySystemSupplier.getRemoteRepositoryManager(); - } - - @Provides - @SuppressWarnings("unused") - static Session newSession(RepositorySystem system, Lookup lookup, Context context) { - - // SettingsDecrypter settingsDecrypter = - // (SettingsDecrypter)Objects.requireNonNull(this.createSettingsDecrypter(preBoot)); - // new DefaultProfileSelector(List.of( - // new JdkVersionProfileActivator(), - // new PropertyProfileActivator(), - // new OperatingSystemProfileActivator(), - // new FileProfileActivator(new ProfileActivationFilePathInterpolator( - // new DefaultPathTranslator(), new DefaultRootLocator())))); - Map properties = context.systemProperties; - - Path userHome = Paths.get(properties.get("user.home")); - Path mavenUserHome = userHome.resolve(".m2"); - Path mavenSystemHome = properties.containsKey("maven.home") - ? Paths.get(properties.get("maven.home")) - : properties.containsKey("env.MAVEN_HOME") ? Paths.get(properties.get("env.MAVEN_HOME")) : null; - - DefaultRepositorySystemSession rsession = new DefaultRepositorySystemSession(h -> false); - rsession.setSystemProperties(properties); - rsession.setConfigProperties(properties); - - ProtoSession session = new ProtoSession( - rsession, - system, - List.of(lookup.lookup(RepositoryFactory.class) - .createRemote("central", "https://repo.maven.apache.org/maven2")), - null, - lookup, - context); - - Settings settings = session.getService(SettingsBuilder.class) - .build( - session, - mavenSystemHome != null ? mavenSystemHome.resolve("settings.xml") : null, - mavenUserHome.resolve("settings.xml")) - .getEffectiveSettings(); - - settings.getProfiles(); - - // local repository - String localRepository = settings.getLocalRepository() != null - ? settings.getLocalRepository() - : mavenUserHome.resolve("repository").toString(); - LocalRepositoryManager llm = - system.newLocalRepositoryManager(rsession, new LocalRepository(localRepository)); - rsession.setLocalRepositoryManager(llm); - // active proxies - // TODO - // active profiles - - // DefaultSession defaultSession = new DefaultSession( - // rsession, - // system, - // List.of(lookup.lookup(RepositoryFactory.class) - // .createRemote("central", "https://repo.maven.apache.org/maven2")), - // null, - // lookup); - - Profile profile = session.getService(SettingsBuilder.class) - .convert(org.apache.maven.api.settings.Profile.newBuilder() - .repositories(settings.getRepositories()) - .pluginRepositories(settings.getPluginRepositories()) - .build()); - RepositoryFactory repositoryFactory = session.getService(RepositoryFactory.class); - List repositories = profile.getRepositories().stream() - .map(repositoryFactory::createRemote) - .toList(); - InternalSession s = (InternalSession) session.withRemoteRepositories(repositories); - InternalSession.associate(rsession, s); - return s; - - // List repositories = repositoryFactory.createRemote(); - - // session.getService(SettingsBuilder.class).convert() - - // settings.getDelegate().getRepositories().stream() - // .map(r -> SettingsUtilsV4.) - // defaultSession.getService(RepositoryFactory.class).createRemote() - // return defaultSession; - } - } - - static class UnsupportedInStandaloneModeException extends MavenException {} - - record DumbPackaging(String id, Type type, Map plugins) implements Packaging {} -} diff --git a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvn/RepositorySystemSupplier.java b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvn/RepositorySystemSupplier.java deleted file mode 100644 index b4188631ca..0000000000 --- a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvn/RepositorySystemSupplier.java +++ /dev/null @@ -1,1098 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.maven.cling.invoker.mvn; - -import java.util.HashMap; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; -import java.util.concurrent.atomic.AtomicBoolean; -import java.util.function.Supplier; - -import org.apache.maven.api.services.ModelBuilder; -import org.apache.maven.internal.impl.DefaultModelUrlNormalizer; -import org.apache.maven.internal.impl.DefaultModelVersionParser; -import org.apache.maven.internal.impl.DefaultModelXmlFactory; -import org.apache.maven.internal.impl.DefaultPluginConfigurationExpander; -import org.apache.maven.internal.impl.DefaultSuperPomProvider; -import org.apache.maven.internal.impl.DefaultUrlNormalizer; -import org.apache.maven.internal.impl.model.DefaultDependencyManagementImporter; -import org.apache.maven.internal.impl.model.DefaultDependencyManagementInjector; -import org.apache.maven.internal.impl.model.DefaultInheritanceAssembler; -import org.apache.maven.internal.impl.model.DefaultInterpolator; -import org.apache.maven.internal.impl.model.DefaultModelBuilder; -import org.apache.maven.internal.impl.model.DefaultModelCacheFactory; -import org.apache.maven.internal.impl.model.DefaultModelInterpolator; -import org.apache.maven.internal.impl.model.DefaultModelNormalizer; -import org.apache.maven.internal.impl.model.DefaultModelPathTranslator; -import org.apache.maven.internal.impl.model.DefaultModelProcessor; -import org.apache.maven.internal.impl.model.DefaultModelValidator; -import org.apache.maven.internal.impl.model.DefaultPathTranslator; -import org.apache.maven.internal.impl.model.DefaultPluginManagementInjector; -import org.apache.maven.internal.impl.model.DefaultProfileInjector; -import org.apache.maven.internal.impl.model.DefaultProfileSelector; -import org.apache.maven.internal.impl.model.ProfileActivationFilePathInterpolator; -import org.apache.maven.internal.impl.model.rootlocator.DefaultRootLocator; -import org.apache.maven.internal.impl.resolver.DefaultArtifactDescriptorReader; -import org.apache.maven.internal.impl.resolver.DefaultModelResolver; -import org.apache.maven.internal.impl.resolver.DefaultVersionRangeResolver; -import org.apache.maven.internal.impl.resolver.DefaultVersionResolver; -import org.apache.maven.internal.impl.resolver.MavenArtifactRelocationSource; -import org.apache.maven.internal.impl.resolver.PluginsMetadataGeneratorFactory; -import org.apache.maven.internal.impl.resolver.SnapshotMetadataGeneratorFactory; -import org.apache.maven.internal.impl.resolver.VersionsMetadataGeneratorFactory; -import org.apache.maven.internal.impl.resolver.relocation.DistributionManagementArtifactRelocationSource; -import org.apache.maven.internal.impl.resolver.relocation.UserPropertiesArtifactRelocationSource; -import org.eclipse.aether.RepositoryListener; -import org.eclipse.aether.RepositorySystem; -import org.eclipse.aether.impl.ArtifactDescriptorReader; -import org.eclipse.aether.impl.ArtifactResolver; -import org.eclipse.aether.impl.DependencyCollector; -import org.eclipse.aether.impl.Deployer; -import org.eclipse.aether.impl.Installer; -import org.eclipse.aether.impl.LocalRepositoryProvider; -import org.eclipse.aether.impl.MetadataGeneratorFactory; -import org.eclipse.aether.impl.MetadataResolver; -import org.eclipse.aether.impl.OfflineController; -import org.eclipse.aether.impl.RemoteRepositoryFilterManager; -import org.eclipse.aether.impl.RemoteRepositoryManager; -import org.eclipse.aether.impl.RepositoryConnectorProvider; -import org.eclipse.aether.impl.RepositoryEventDispatcher; -import org.eclipse.aether.impl.RepositorySystemLifecycle; -import org.eclipse.aether.impl.UpdateCheckManager; -import org.eclipse.aether.impl.UpdatePolicyAnalyzer; -import org.eclipse.aether.impl.VersionRangeResolver; -import org.eclipse.aether.impl.VersionResolver; -import org.eclipse.aether.internal.impl.DefaultArtifactPredicateFactory; -import org.eclipse.aether.internal.impl.DefaultArtifactResolver; -import org.eclipse.aether.internal.impl.DefaultChecksumPolicyProvider; -import org.eclipse.aether.internal.impl.DefaultChecksumProcessor; -import org.eclipse.aether.internal.impl.DefaultDeployer; -import org.eclipse.aether.internal.impl.DefaultInstaller; -import org.eclipse.aether.internal.impl.DefaultLocalPathComposer; -import org.eclipse.aether.internal.impl.DefaultLocalPathPrefixComposerFactory; -import org.eclipse.aether.internal.impl.DefaultLocalRepositoryProvider; -import org.eclipse.aether.internal.impl.DefaultMetadataResolver; -import org.eclipse.aether.internal.impl.DefaultOfflineController; -import org.eclipse.aether.internal.impl.DefaultPathProcessor; -import org.eclipse.aether.internal.impl.DefaultRemoteRepositoryManager; -import org.eclipse.aether.internal.impl.DefaultRepositoryConnectorProvider; -import org.eclipse.aether.internal.impl.DefaultRepositoryEventDispatcher; -import org.eclipse.aether.internal.impl.DefaultRepositoryLayoutProvider; -import org.eclipse.aether.internal.impl.DefaultRepositorySystem; -import org.eclipse.aether.internal.impl.DefaultRepositorySystemLifecycle; -import org.eclipse.aether.internal.impl.DefaultTrackingFileManager; -import org.eclipse.aether.internal.impl.DefaultTransporterProvider; -import org.eclipse.aether.internal.impl.DefaultUpdateCheckManager; -import org.eclipse.aether.internal.impl.DefaultUpdatePolicyAnalyzer; -import org.eclipse.aether.internal.impl.EnhancedLocalRepositoryManagerFactory; -import org.eclipse.aether.internal.impl.LocalPathComposer; -import org.eclipse.aether.internal.impl.LocalPathPrefixComposerFactory; -import org.eclipse.aether.internal.impl.Maven2RepositoryLayoutFactory; -import org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory; -import org.eclipse.aether.internal.impl.TrackingFileManager; -import org.eclipse.aether.internal.impl.checksum.DefaultChecksumAlgorithmFactorySelector; -import org.eclipse.aether.internal.impl.checksum.Md5ChecksumAlgorithmFactory; -import org.eclipse.aether.internal.impl.checksum.Sha1ChecksumAlgorithmFactory; -import org.eclipse.aether.internal.impl.checksum.Sha256ChecksumAlgorithmFactory; -import org.eclipse.aether.internal.impl.checksum.Sha512ChecksumAlgorithmFactory; -import org.eclipse.aether.internal.impl.checksum.SparseDirectoryTrustedChecksumsSource; -import org.eclipse.aether.internal.impl.checksum.SummaryFileTrustedChecksumsSource; -import org.eclipse.aether.internal.impl.checksum.TrustedToProvidedChecksumsSourceAdapter; -import org.eclipse.aether.internal.impl.collect.DefaultDependencyCollector; -import org.eclipse.aether.internal.impl.collect.DependencyCollectorDelegate; -import org.eclipse.aether.internal.impl.collect.bf.BfDependencyCollector; -import org.eclipse.aether.internal.impl.collect.df.DfDependencyCollector; -import org.eclipse.aether.internal.impl.filter.DefaultRemoteRepositoryFilterManager; -import org.eclipse.aether.internal.impl.filter.GroupIdRemoteRepositoryFilterSource; -import org.eclipse.aether.internal.impl.filter.PrefixesRemoteRepositoryFilterSource; -import org.eclipse.aether.internal.impl.resolution.TrustedChecksumsArtifactResolverPostProcessor; -import org.eclipse.aether.internal.impl.synccontext.DefaultSyncContextFactory; -import org.eclipse.aether.internal.impl.synccontext.named.NameMapper; -import org.eclipse.aether.internal.impl.synccontext.named.NameMappers; -import org.eclipse.aether.internal.impl.synccontext.named.NamedLockFactoryAdapterFactory; -import org.eclipse.aether.internal.impl.synccontext.named.NamedLockFactoryAdapterFactoryImpl; -import org.eclipse.aether.internal.impl.transport.http.DefaultChecksumExtractor; -import org.eclipse.aether.internal.impl.transport.http.Nx2ChecksumExtractor; -import org.eclipse.aether.internal.impl.transport.http.XChecksumExtractor; -import org.eclipse.aether.named.NamedLockFactory; -import org.eclipse.aether.named.providers.FileLockNamedLockFactory; -import org.eclipse.aether.named.providers.LocalReadWriteLockNamedLockFactory; -import org.eclipse.aether.named.providers.LocalSemaphoreNamedLockFactory; -import org.eclipse.aether.named.providers.NoopNamedLockFactory; -import org.eclipse.aether.spi.artifact.ArtifactPredicateFactory; -import org.eclipse.aether.spi.artifact.decorator.ArtifactDecoratorFactory; -import org.eclipse.aether.spi.artifact.generator.ArtifactGeneratorFactory; -import org.eclipse.aether.spi.checksums.ProvidedChecksumsSource; -import org.eclipse.aether.spi.checksums.TrustedChecksumsSource; -import org.eclipse.aether.spi.connector.RepositoryConnectorFactory; -import org.eclipse.aether.spi.connector.checksum.ChecksumAlgorithmFactory; -import org.eclipse.aether.spi.connector.checksum.ChecksumAlgorithmFactorySelector; -import org.eclipse.aether.spi.connector.checksum.ChecksumPolicyProvider; -import org.eclipse.aether.spi.connector.filter.RemoteRepositoryFilterSource; -import org.eclipse.aether.spi.connector.layout.RepositoryLayoutFactory; -import org.eclipse.aether.spi.connector.layout.RepositoryLayoutProvider; -import org.eclipse.aether.spi.connector.transport.TransporterFactory; -import org.eclipse.aether.spi.connector.transport.TransporterProvider; -import org.eclipse.aether.spi.connector.transport.http.ChecksumExtractor; -import org.eclipse.aether.spi.connector.transport.http.ChecksumExtractorStrategy; -import org.eclipse.aether.spi.io.ChecksumProcessor; -import org.eclipse.aether.spi.io.PathProcessor; -import org.eclipse.aether.spi.localrepo.LocalRepositoryManagerFactory; -import org.eclipse.aether.spi.resolution.ArtifactResolverPostProcessor; -import org.eclipse.aether.spi.synccontext.SyncContextFactory; -import org.eclipse.aether.util.version.GenericVersionScheme; -import org.eclipse.aether.version.VersionScheme; - -/** - * A simple memorizing {@link Supplier} of {@link RepositorySystem} instance, that on first call - * supplies lazily constructed instance, and on each subsequent call same instance. Hence, this instance should be - * thrown away immediately once repository system was created and there is no need for more instances. If new - * repository system instance needed, new instance of this class must be created. For proper shut down of returned - * repository system instance(s) use {@link RepositorySystem#shutdown()} method on supplied instance(s). - *

- * Since Resolver 2.0 this class offers access to various components via public getters, and allows even partial object - * graph construction. - *

- * Extend this class {@code createXXX()} methods and override to customize, if needed. The contract of this class makes - * sure that these (potentially overridden) methods are invoked only once, and instance created by those methods are - * memorized and kept as long as supplier instance is kept open. - *

- * This class is not thread safe and must be used from one thread only, while the constructed {@link RepositorySystem} - * is thread safe. - *

- * Important: Given the instance of supplier memorizes the supplier {@link RepositorySystem} instance it supplies, - * their lifecycle is shared as well: once supplied repository system is shut-down, this instance becomes closed as - * well. Any subsequent {@code getXXX} method invocation attempt will fail with {@link IllegalStateException}. - */ -public class RepositorySystemSupplier implements Supplier { - private final AtomicBoolean closed = new AtomicBoolean(false); - - public RepositorySystemSupplier() {} - - private void checkClosed() { - if (closed.get()) { - throw new IllegalStateException("Supplier is closed"); - } - } - - private PathProcessor pathProcessor; - - public final PathProcessor getPathProcessor() { - checkClosed(); - if (pathProcessor == null) { - pathProcessor = createPathProcessor(); - } - return pathProcessor; - } - - protected PathProcessor createPathProcessor() { - return new DefaultPathProcessor(); - } - - private ChecksumProcessor checksumProcessor; - - public final ChecksumProcessor getChecksumProcessor() { - checkClosed(); - if (checksumProcessor == null) { - checksumProcessor = createChecksumProcessor(); - } - return checksumProcessor; - } - - protected ChecksumProcessor createChecksumProcessor() { - return new DefaultChecksumProcessor(getPathProcessor()); - } - - private TrackingFileManager trackingFileManager; - - public final TrackingFileManager getTrackingFileManager() { - checkClosed(); - if (trackingFileManager == null) { - trackingFileManager = createTrackingFileManager(); - } - return trackingFileManager; - } - - protected TrackingFileManager createTrackingFileManager() { - return new DefaultTrackingFileManager(); - } - - private LocalPathComposer localPathComposer; - - public final LocalPathComposer getLocalPathComposer() { - checkClosed(); - if (localPathComposer == null) { - localPathComposer = createLocalPathComposer(); - } - return localPathComposer; - } - - protected LocalPathComposer createLocalPathComposer() { - return new DefaultLocalPathComposer(); - } - - private LocalPathPrefixComposerFactory localPathPrefixComposerFactory; - - public final LocalPathPrefixComposerFactory getLocalPathPrefixComposerFactory() { - checkClosed(); - if (localPathPrefixComposerFactory == null) { - localPathPrefixComposerFactory = createLocalPathPrefixComposerFactory(); - } - return localPathPrefixComposerFactory; - } - - protected LocalPathPrefixComposerFactory createLocalPathPrefixComposerFactory() { - return new DefaultLocalPathPrefixComposerFactory(); - } - - private RepositorySystemLifecycle repositorySystemLifecycle; - - public final RepositorySystemLifecycle getRepositorySystemLifecycle() { - checkClosed(); - if (repositorySystemLifecycle == null) { - repositorySystemLifecycle = createRepositorySystemLifecycle(); - repositorySystemLifecycle.addOnSystemEndedHandler(() -> closed.set(true)); - } - return repositorySystemLifecycle; - } - - protected RepositorySystemLifecycle createRepositorySystemLifecycle() { - return new DefaultRepositorySystemLifecycle(); - } - - private OfflineController offlineController; - - public final OfflineController getOfflineController() { - checkClosed(); - if (offlineController == null) { - offlineController = createOfflineController(); - } - return offlineController; - } - - protected OfflineController createOfflineController() { - return new DefaultOfflineController(); - } - - private UpdatePolicyAnalyzer updatePolicyAnalyzer; - - public final UpdatePolicyAnalyzer getUpdatePolicyAnalyzer() { - checkClosed(); - if (updatePolicyAnalyzer == null) { - updatePolicyAnalyzer = createUpdatePolicyAnalyzer(); - } - return updatePolicyAnalyzer; - } - - protected UpdatePolicyAnalyzer createUpdatePolicyAnalyzer() { - return new DefaultUpdatePolicyAnalyzer(); - } - - private ChecksumPolicyProvider checksumPolicyProvider; - - public final ChecksumPolicyProvider getChecksumPolicyProvider() { - checkClosed(); - if (checksumPolicyProvider == null) { - checksumPolicyProvider = createChecksumPolicyProvider(); - } - return checksumPolicyProvider; - } - - protected ChecksumPolicyProvider createChecksumPolicyProvider() { - return new DefaultChecksumPolicyProvider(); - } - - private UpdateCheckManager updateCheckManager; - - public final UpdateCheckManager getUpdateCheckManager() { - checkClosed(); - if (updateCheckManager == null) { - updateCheckManager = createUpdateCheckManager(); - } - return updateCheckManager; - } - - protected UpdateCheckManager createUpdateCheckManager() { - return new DefaultUpdateCheckManager(getTrackingFileManager(), getUpdatePolicyAnalyzer(), getPathProcessor()); - } - - private Map namedLockFactories; - - public final Map getNamedLockFactories() { - checkClosed(); - if (namedLockFactories == null) { - namedLockFactories = createNamedLockFactories(); - } - return namedLockFactories; - } - - protected Map createNamedLockFactories() { - HashMap result = new HashMap<>(); - result.put(NoopNamedLockFactory.NAME, new NoopNamedLockFactory()); - result.put(LocalReadWriteLockNamedLockFactory.NAME, new LocalReadWriteLockNamedLockFactory()); - result.put(LocalSemaphoreNamedLockFactory.NAME, new LocalSemaphoreNamedLockFactory()); - result.put(FileLockNamedLockFactory.NAME, new FileLockNamedLockFactory()); - return result; - } - - private Map nameMappers; - - public final Map getNameMappers() { - checkClosed(); - if (nameMappers == null) { - nameMappers = createNameMappers(); - } - return nameMappers; - } - - protected Map createNameMappers() { - HashMap result = new HashMap<>(); - result.put(NameMappers.STATIC_NAME, NameMappers.staticNameMapper()); - result.put(NameMappers.GAV_NAME, NameMappers.gavNameMapper()); - result.put(NameMappers.DISCRIMINATING_NAME, NameMappers.discriminatingNameMapper()); - result.put(NameMappers.FILE_GAV_NAME, NameMappers.fileGavNameMapper()); - result.put(NameMappers.FILE_HGAV_NAME, NameMappers.fileHashingGavNameMapper()); - return result; - } - - private NamedLockFactoryAdapterFactory namedLockFactoryAdapterFactory; - - public final NamedLockFactoryAdapterFactory getNamedLockFactoryAdapterFactory() { - checkClosed(); - if (namedLockFactoryAdapterFactory == null) { - namedLockFactoryAdapterFactory = createNamedLockFactoryAdapterFactory(); - } - return namedLockFactoryAdapterFactory; - } - - protected NamedLockFactoryAdapterFactory createNamedLockFactoryAdapterFactory() { - return new NamedLockFactoryAdapterFactoryImpl( - getNamedLockFactories(), getNameMappers(), getRepositorySystemLifecycle()); - } - - private SyncContextFactory syncContextFactory; - - public final SyncContextFactory getSyncContextFactory() { - checkClosed(); - if (syncContextFactory == null) { - syncContextFactory = createSyncContextFactory(); - } - return syncContextFactory; - } - - protected SyncContextFactory createSyncContextFactory() { - return new DefaultSyncContextFactory(getNamedLockFactoryAdapterFactory()); - } - - private Map checksumAlgorithmFactories; - - public final Map getChecksumAlgorithmFactories() { - checkClosed(); - if (checksumAlgorithmFactories == null) { - checksumAlgorithmFactories = createChecksumAlgorithmFactories(); - } - return checksumAlgorithmFactories; - } - - protected Map createChecksumAlgorithmFactories() { - HashMap result = new HashMap<>(); - result.put(Sha512ChecksumAlgorithmFactory.NAME, new Sha512ChecksumAlgorithmFactory()); - result.put(Sha256ChecksumAlgorithmFactory.NAME, new Sha256ChecksumAlgorithmFactory()); - result.put(Sha1ChecksumAlgorithmFactory.NAME, new Sha1ChecksumAlgorithmFactory()); - result.put(Md5ChecksumAlgorithmFactory.NAME, new Md5ChecksumAlgorithmFactory()); - return result; - } - - private ChecksumAlgorithmFactorySelector checksumAlgorithmFactorySelector; - - public final ChecksumAlgorithmFactorySelector getChecksumAlgorithmFactorySelector() { - checkClosed(); - if (checksumAlgorithmFactorySelector == null) { - checksumAlgorithmFactorySelector = createChecksumAlgorithmFactorySelector(); - } - return checksumAlgorithmFactorySelector; - } - - protected ChecksumAlgorithmFactorySelector createChecksumAlgorithmFactorySelector() { - return new DefaultChecksumAlgorithmFactorySelector(getChecksumAlgorithmFactories()); - } - - private ArtifactPredicateFactory artifactPredicateFactory; - - public final ArtifactPredicateFactory getArtifactPredicateFactory() { - checkClosed(); - if (artifactPredicateFactory == null) { - artifactPredicateFactory = createArtifactPredicateFactory(); - } - return artifactPredicateFactory; - } - - protected ArtifactPredicateFactory createArtifactPredicateFactory() { - return new DefaultArtifactPredicateFactory(getChecksumAlgorithmFactorySelector()); - } - - private Map repositoryLayoutFactories; - - public final Map getRepositoryLayoutFactories() { - checkClosed(); - if (repositoryLayoutFactories == null) { - repositoryLayoutFactories = createRepositoryLayoutFactories(); - } - return repositoryLayoutFactories; - } - - protected Map createRepositoryLayoutFactories() { - HashMap result = new HashMap<>(); - result.put( - Maven2RepositoryLayoutFactory.NAME, - new Maven2RepositoryLayoutFactory( - getChecksumAlgorithmFactorySelector(), getArtifactPredicateFactory())); - return result; - } - - private RepositoryLayoutProvider repositoryLayoutProvider; - - public final RepositoryLayoutProvider getRepositoryLayoutProvider() { - checkClosed(); - if (repositoryLayoutProvider == null) { - repositoryLayoutProvider = createRepositoryLayoutProvider(); - } - return repositoryLayoutProvider; - } - - protected RepositoryLayoutProvider createRepositoryLayoutProvider() { - return new DefaultRepositoryLayoutProvider(getRepositoryLayoutFactories()); - } - - private LocalRepositoryProvider localRepositoryProvider; - - public final LocalRepositoryProvider getLocalRepositoryProvider() { - checkClosed(); - if (localRepositoryProvider == null) { - localRepositoryProvider = createLocalRepositoryProvider(); - } - return localRepositoryProvider; - } - - protected LocalRepositoryProvider createLocalRepositoryProvider() { - LocalPathComposer localPathComposer = getLocalPathComposer(); - HashMap localRepositoryProviders = new HashMap<>(2); - localRepositoryProviders.put( - SimpleLocalRepositoryManagerFactory.NAME, new SimpleLocalRepositoryManagerFactory(localPathComposer)); - localRepositoryProviders.put( - EnhancedLocalRepositoryManagerFactory.NAME, - new EnhancedLocalRepositoryManagerFactory( - localPathComposer, getTrackingFileManager(), getLocalPathPrefixComposerFactory())); - return new DefaultLocalRepositoryProvider(localRepositoryProviders); - } - - private RemoteRepositoryManager remoteRepositoryManager; - - public final RemoteRepositoryManager getRemoteRepositoryManager() { - checkClosed(); - if (remoteRepositoryManager == null) { - remoteRepositoryManager = createRemoteRepositoryManager(); - } - return remoteRepositoryManager; - } - - protected RemoteRepositoryManager createRemoteRepositoryManager() { - return new DefaultRemoteRepositoryManager(getUpdatePolicyAnalyzer(), getChecksumPolicyProvider()); - } - - private Map remoteRepositoryFilterSources; - - public final Map getRemoteRepositoryFilterSources() { - checkClosed(); - if (remoteRepositoryFilterSources == null) { - remoteRepositoryFilterSources = createRemoteRepositoryFilterSources(); - } - return remoteRepositoryFilterSources; - } - - protected Map createRemoteRepositoryFilterSources() { - HashMap result = new HashMap<>(); - result.put( - GroupIdRemoteRepositoryFilterSource.NAME, - new GroupIdRemoteRepositoryFilterSource(getRepositorySystemLifecycle())); - result.put( - PrefixesRemoteRepositoryFilterSource.NAME, - new PrefixesRemoteRepositoryFilterSource(getRepositoryLayoutProvider())); - return result; - } - - private RemoteRepositoryFilterManager remoteRepositoryFilterManager; - - public final RemoteRepositoryFilterManager getRemoteRepositoryFilterManager() { - checkClosed(); - if (remoteRepositoryFilterManager == null) { - remoteRepositoryFilterManager = createRemoteRepositoryFilterManager(); - } - return remoteRepositoryFilterManager; - } - - protected RemoteRepositoryFilterManager createRemoteRepositoryFilterManager() { - return new DefaultRemoteRepositoryFilterManager(getRemoteRepositoryFilterSources()); - } - - private Map repositoryListeners; - - public final Map getRepositoryListeners() { - checkClosed(); - if (repositoryListeners == null) { - repositoryListeners = createRepositoryListeners(); - } - return repositoryListeners; - } - - protected Map createRepositoryListeners() { - return new HashMap<>(); - } - - private RepositoryEventDispatcher repositoryEventDispatcher; - - public final RepositoryEventDispatcher getRepositoryEventDispatcher() { - checkClosed(); - if (repositoryEventDispatcher == null) { - repositoryEventDispatcher = createRepositoryEventDispatcher(); - } - return repositoryEventDispatcher; - } - - protected RepositoryEventDispatcher createRepositoryEventDispatcher() { - return new DefaultRepositoryEventDispatcher(getRepositoryListeners()); - } - - private Map trustedChecksumsSources; - - public final Map getTrustedChecksumsSources() { - checkClosed(); - if (trustedChecksumsSources == null) { - trustedChecksumsSources = createTrustedChecksumsSources(); - } - return trustedChecksumsSources; - } - - protected Map createTrustedChecksumsSources() { - HashMap result = new HashMap<>(); - result.put( - SparseDirectoryTrustedChecksumsSource.NAME, - new SparseDirectoryTrustedChecksumsSource(getChecksumProcessor(), getLocalPathComposer())); - result.put( - SummaryFileTrustedChecksumsSource.NAME, - new SummaryFileTrustedChecksumsSource(getLocalPathComposer(), getRepositorySystemLifecycle())); - return result; - } - - private Map providedChecksumsSources; - - public final Map getProvidedChecksumsSources() { - checkClosed(); - if (providedChecksumsSources == null) { - providedChecksumsSources = createProvidedChecksumsSources(); - } - return providedChecksumsSources; - } - - protected Map createProvidedChecksumsSources() { - HashMap result = new HashMap<>(); - result.put( - TrustedToProvidedChecksumsSourceAdapter.NAME, - new TrustedToProvidedChecksumsSourceAdapter(getTrustedChecksumsSources())); - return result; - } - - private Map checksumExtractorStrategies; - - public final Map getChecksumExtractorStrategies() { - checkClosed(); - if (checksumExtractorStrategies == null) { - checksumExtractorStrategies = createChecksumExtractorStrategies(); - } - return checksumExtractorStrategies; - } - - protected Map createChecksumExtractorStrategies() { - HashMap result = new HashMap<>(); - result.put(XChecksumExtractor.NAME, new XChecksumExtractor()); - result.put(Nx2ChecksumExtractor.NAME, new Nx2ChecksumExtractor()); - return result; - } - - private ChecksumExtractor checksumExtractor; - - public final ChecksumExtractor getChecksumExtractor() { - checkClosed(); - if (checksumExtractor == null) { - checksumExtractor = createChecksumExtractor(); - } - return checksumExtractor; - } - - protected ChecksumExtractor createChecksumExtractor() { - return new DefaultChecksumExtractor(getChecksumExtractorStrategies()); - } - - private Map transporterFactories; - - public final Map getTransporterFactories() { - checkClosed(); - if (transporterFactories == null) { - transporterFactories = createTransporterFactories(); - } - return transporterFactories; - } - - protected Map createTransporterFactories() { - HashMap result = new HashMap<>(); - // result.put(FileTransporterFactory.NAME, new FileTransporterFactory()); - // result.put( - // ApacheTransporterFactory.NAME, - // new ApacheTransporterFactory(getChecksumExtractor(), getPathProcessor())); - return result; - } - - private TransporterProvider transporterProvider; - - public final TransporterProvider getTransporterProvider() { - checkClosed(); - if (transporterProvider == null) { - transporterProvider = createTransporterProvider(); - } - return transporterProvider; - } - - protected TransporterProvider createTransporterProvider() { - return new DefaultTransporterProvider(getTransporterFactories()); - } - - // private BasicRepositoryConnectorFactory basicRepositoryConnectorFactory; - // - // public final BasicRepositoryConnectorFactory getBasicRepositoryConnectorFactory() { - // checkClosed(); - // if (basicRepositoryConnectorFactory == null) { - // basicRepositoryConnectorFactory = createBasicRepositoryConnectorFactory(); - // } - // return basicRepositoryConnectorFactory; - // } - // - // protected BasicRepositoryConnectorFactory createBasicRepositoryConnectorFactory() { - // return new BasicRepositoryConnectorFactory( - // getTransporterProvider(), - // getRepositoryLayoutProvider(), - // getChecksumPolicyProvider(), - // getChecksumProcessor(), - // getProvidedChecksumsSources()); - // } - - private Map repositoryConnectorFactories; - - public final Map getRepositoryConnectorFactories() { - checkClosed(); - if (repositoryConnectorFactories == null) { - repositoryConnectorFactories = createRepositoryConnectorFactories(); - } - return repositoryConnectorFactories; - } - - protected Map createRepositoryConnectorFactories() { - HashMap result = new HashMap<>(); - // result.put(BasicRepositoryConnectorFactory.NAME, getBasicRepositoryConnectorFactory()); - return result; - } - - private RepositoryConnectorProvider repositoryConnectorProvider; - - public final RepositoryConnectorProvider getRepositoryConnectorProvider() { - checkClosed(); - if (repositoryConnectorProvider == null) { - repositoryConnectorProvider = createRepositoryConnectorProvider(); - } - return repositoryConnectorProvider; - } - - protected RepositoryConnectorProvider createRepositoryConnectorProvider() { - return new DefaultRepositoryConnectorProvider( - getRepositoryConnectorFactories(), getRemoteRepositoryFilterManager()); - } - - private Installer installer; - - public final Installer getInstaller() { - checkClosed(); - if (installer == null) { - installer = createInstaller(); - } - return installer; - } - - protected Installer createInstaller() { - return new DefaultInstaller( - getPathProcessor(), - getRepositoryEventDispatcher(), - getArtifactGeneratorFactories(), - getMetadataGeneratorFactories(), - getSyncContextFactory()); - } - - private Deployer deployer; - - public final Deployer getDeployer() { - checkClosed(); - if (deployer == null) { - deployer = createDeployer(); - } - return deployer; - } - - protected Deployer createDeployer() { - return new DefaultDeployer( - getPathProcessor(), - getRepositoryEventDispatcher(), - getRepositoryConnectorProvider(), - getRemoteRepositoryManager(), - getUpdateCheckManager(), - getArtifactGeneratorFactories(), - getMetadataGeneratorFactories(), - getSyncContextFactory(), - getOfflineController()); - } - - private Map dependencyCollectorDelegates; - - public final Map getDependencyCollectorDelegates() { - checkClosed(); - if (dependencyCollectorDelegates == null) { - dependencyCollectorDelegates = createDependencyCollectorDelegates(); - } - return dependencyCollectorDelegates; - } - - protected Map createDependencyCollectorDelegates() { - RemoteRepositoryManager remoteRepositoryManager = getRemoteRepositoryManager(); - ArtifactDescriptorReader artifactDescriptorReader = getArtifactDescriptorReader(); - VersionRangeResolver versionRangeResolver = getVersionRangeResolver(); - HashMap result = new HashMap<>(); - result.put( - DfDependencyCollector.NAME, - new DfDependencyCollector( - remoteRepositoryManager, - artifactDescriptorReader, - versionRangeResolver, - getArtifactDecoratorFactories())); - result.put( - BfDependencyCollector.NAME, - new BfDependencyCollector( - remoteRepositoryManager, - artifactDescriptorReader, - versionRangeResolver, - getArtifactDecoratorFactories())); - return result; - } - - private DependencyCollector dependencyCollector; - - public final DependencyCollector getDependencyCollector() { - checkClosed(); - if (dependencyCollector == null) { - dependencyCollector = createDependencyCollector(); - } - return dependencyCollector; - } - - protected DependencyCollector createDependencyCollector() { - return new DefaultDependencyCollector(getDependencyCollectorDelegates()); - } - - private Map artifactResolverPostProcessors; - - public final Map getArtifactResolverPostProcessors() { - checkClosed(); - if (artifactResolverPostProcessors == null) { - artifactResolverPostProcessors = createArtifactResolverPostProcessors(); - } - return artifactResolverPostProcessors; - } - - protected Map createArtifactResolverPostProcessors() { - HashMap result = new HashMap<>(); - result.put( - TrustedChecksumsArtifactResolverPostProcessor.NAME, - new TrustedChecksumsArtifactResolverPostProcessor( - getChecksumAlgorithmFactorySelector(), getTrustedChecksumsSources())); - return result; - } - - private ArtifactResolver artifactResolver; - - public final ArtifactResolver getArtifactResolver() { - checkClosed(); - if (artifactResolver == null) { - artifactResolver = createArtifactResolver(); - } - return artifactResolver; - } - - protected ArtifactResolver createArtifactResolver() { - return new DefaultArtifactResolver( - getPathProcessor(), - getRepositoryEventDispatcher(), - getVersionResolver(), - getUpdateCheckManager(), - getRepositoryConnectorProvider(), - getRemoteRepositoryManager(), - getSyncContextFactory(), - getOfflineController(), - getArtifactResolverPostProcessors(), - getRemoteRepositoryFilterManager()); - } - - private MetadataResolver metadataResolver; - - public final MetadataResolver getMetadataResolver() { - checkClosed(); - if (metadataResolver == null) { - metadataResolver = createMetadataResolver(); - } - return metadataResolver; - } - - protected MetadataResolver createMetadataResolver() { - return new DefaultMetadataResolver( - getRepositoryEventDispatcher(), - getUpdateCheckManager(), - getRepositoryConnectorProvider(), - getRemoteRepositoryManager(), - getSyncContextFactory(), - getOfflineController(), - getRemoteRepositoryFilterManager(), - getPathProcessor()); - } - - private VersionScheme versionScheme; - - public final VersionScheme getVersionScheme() { - checkClosed(); - if (versionScheme == null) { - versionScheme = createVersionScheme(); - } - return versionScheme; - } - - protected VersionScheme createVersionScheme() { - return new GenericVersionScheme(); - } - - private Map artifactGeneratorFactories; - - public final Map getArtifactGeneratorFactories() { - checkClosed(); - if (artifactGeneratorFactories == null) { - artifactGeneratorFactories = createArtifactGeneratorFactories(); - } - return artifactGeneratorFactories; - } - - protected Map createArtifactGeneratorFactories() { - // by default none, this is extension point - return new HashMap<>(); - } - - private Map artifactDecoratorFactories; - - public final Map getArtifactDecoratorFactories() { - checkClosed(); - if (artifactDecoratorFactories == null) { - artifactDecoratorFactories = createArtifactDecoratorFactories(); - } - return artifactDecoratorFactories; - } - - protected Map createArtifactDecoratorFactories() { - // by default none, this is extension point - return new HashMap<>(); - } - - // Maven provided - - private Map metadataGeneratorFactories; - - public final Map getMetadataGeneratorFactories() { - checkClosed(); - if (metadataGeneratorFactories == null) { - metadataGeneratorFactories = createMetadataGeneratorFactories(); - } - return metadataGeneratorFactories; - } - - protected Map createMetadataGeneratorFactories() { - // from maven-resolver-provider - HashMap result = new HashMap<>(); - result.put(PluginsMetadataGeneratorFactory.NAME, new PluginsMetadataGeneratorFactory()); - result.put(VersionsMetadataGeneratorFactory.NAME, new VersionsMetadataGeneratorFactory()); - result.put(SnapshotMetadataGeneratorFactory.NAME, new SnapshotMetadataGeneratorFactory()); - return result; - } - - private LinkedHashMap artifactRelocationSources; - - public final LinkedHashMap getMavenArtifactRelocationSources() { - checkClosed(); - if (artifactRelocationSources == null) { - artifactRelocationSources = createMavenArtifactRelocationSources(); - } - return artifactRelocationSources; - } - - protected LinkedHashMap createMavenArtifactRelocationSources() { - // from maven-resolver-provider - LinkedHashMap result = new LinkedHashMap<>(); - result.put(UserPropertiesArtifactRelocationSource.NAME, new UserPropertiesArtifactRelocationSource()); - result.put( - DistributionManagementArtifactRelocationSource.NAME, - new DistributionManagementArtifactRelocationSource()); - return result; - } - - private ArtifactDescriptorReader artifactDescriptorReader; - - public final ArtifactDescriptorReader getArtifactDescriptorReader() { - checkClosed(); - if (artifactDescriptorReader == null) { - artifactDescriptorReader = createArtifactDescriptorReader(); - } - return artifactDescriptorReader; - } - - protected ArtifactDescriptorReader createArtifactDescriptorReader() { - // from maven-resolver-provider - return new DefaultArtifactDescriptorReader( - getVersionResolver(), - getArtifactResolver(), - getModelBuilder(), - getRepositoryEventDispatcher(), - getMavenArtifactRelocationSources()); - } - - private VersionResolver versionResolver; - - public final VersionResolver getVersionResolver() { - checkClosed(); - if (versionResolver == null) { - versionResolver = createVersionResolver(); - } - return versionResolver; - } - - protected VersionResolver createVersionResolver() { - // from maven-resolver-provider - return new DefaultVersionResolver( - getMetadataResolver(), getSyncContextFactory(), getRepositoryEventDispatcher()); - } - - private VersionRangeResolver versionRangeResolver; - - public final VersionRangeResolver getVersionRangeResolver() { - checkClosed(); - if (versionRangeResolver == null) { - versionRangeResolver = createVersionRangeResolver(); - } - return versionRangeResolver; - } - - protected VersionRangeResolver createVersionRangeResolver() { - // from maven-resolver-provider - return new DefaultVersionRangeResolver( - getMetadataResolver(), getSyncContextFactory(), getRepositoryEventDispatcher(), getVersionScheme()); - } - - private ModelBuilder modelBuilder; - - public final ModelBuilder getModelBuilder() { - checkClosed(); - if (modelBuilder == null) { - modelBuilder = createModelBuilder(); - } - return modelBuilder; - } - - protected ModelBuilder createModelBuilder() { - // from maven-model-builder - DefaultModelProcessor modelProcessor = new DefaultModelProcessor(new DefaultModelXmlFactory(), List.of()); - return new DefaultModelBuilder( - modelProcessor, - new DefaultModelValidator(), - new DefaultModelNormalizer(), - new DefaultModelInterpolator( - new DefaultPathTranslator(), - new DefaultUrlNormalizer(), - new DefaultRootLocator(), - new DefaultInterpolator()), - new DefaultModelPathTranslator(new DefaultPathTranslator()), - new DefaultModelUrlNormalizer(new DefaultUrlNormalizer()), - new DefaultSuperPomProvider(modelProcessor), - new DefaultInheritanceAssembler(), - new DefaultProfileSelector(), - new DefaultProfileInjector(), - new DefaultPluginManagementInjector(), - new DefaultDependencyManagementInjector(), - new DefaultDependencyManagementImporter(), - new DefaultPluginConfigurationExpander(), - new ProfileActivationFilePathInterpolator( - new DefaultPathTranslator(), new DefaultRootLocator(), new DefaultInterpolator()), - new DefaultModelVersionParser(getVersionScheme()), - List.of(), - new DefaultModelCacheFactory(), - new DefaultModelResolver(), - new DefaultInterpolator()); - } - - private RepositorySystem repositorySystem; - - public final RepositorySystem getRepositorySystem() { - checkClosed(); - if (repositorySystem == null) { - repositorySystem = createRepositorySystem(); - } - return repositorySystem; - } - - protected RepositorySystem createRepositorySystem() { - return new DefaultRepositorySystem( - getVersionResolver(), - getVersionRangeResolver(), - getArtifactResolver(), - getMetadataResolver(), - getArtifactDescriptorReader(), - getDependencyCollector(), - getInstaller(), - getDeployer(), - getLocalRepositoryProvider(), - getSyncContextFactory(), - getRemoteRepositoryManager(), - getRepositorySystemLifecycle(), - getArtifactDecoratorFactories()); - } - - @Override - public RepositorySystem get() { - return getRepositorySystem(); - } -} diff --git a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvn/resident/ResidentMavenContext.java b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvn/resident/ResidentMavenContext.java index aa1102baa8..7d96785d4f 100644 --- a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvn/resident/ResidentMavenContext.java +++ b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvn/resident/ResidentMavenContext.java @@ -51,15 +51,9 @@ public class ResidentMavenContext extends MavenContext { shadow.interactive = interactive; shadow.localRepositoryPath = localRepositoryPath; - shadow.installationSettingsPath = installationSettingsPath; - shadow.projectSettingsPath = projectSettingsPath; - shadow.userSettingsPath = userSettingsPath; shadow.effectiveSettings = effectiveSettings; - shadow.mavenExecutionRequest = mavenExecutionRequest; shadow.eventSpyDispatcher = eventSpyDispatcher; - shadow.mavenExecutionRequestPopulator = mavenExecutionRequestPopulator; - shadow.modelProcessor = modelProcessor; shadow.maven = maven; return shadow; diff --git a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/spi/PropertyContributorsHolder.java b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/spi/PropertyContributorsHolder.java new file mode 100644 index 0000000000..f142210b31 --- /dev/null +++ b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/spi/PropertyContributorsHolder.java @@ -0,0 +1,42 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.maven.cling.invoker.spi; + +import java.util.Map; + +import org.apache.maven.api.di.Inject; +import org.apache.maven.api.di.Named; +import org.apache.maven.api.spi.PropertyContributor; + +/** + * Hack: to circumvent sisu/plexus/di stuff. + */ +@Named +public final class PropertyContributorsHolder { + private final Map propertyContributors; + + @Inject + public PropertyContributorsHolder(Map propertyContributors) { + this.propertyContributors = propertyContributors; + } + + public Map getPropertyContributors() { + return propertyContributors; + } +} diff --git a/impl/maven-core/src/main/java/org/apache/maven/internal/aether/DefaultRepositorySystemSessionFactory.java b/impl/maven-core/src/main/java/org/apache/maven/internal/aether/DefaultRepositorySystemSessionFactory.java index 037f8eca2c..235b68d222 100644 --- a/impl/maven-core/src/main/java/org/apache/maven/internal/aether/DefaultRepositorySystemSessionFactory.java +++ b/impl/maven-core/src/main/java/org/apache/maven/internal/aether/DefaultRepositorySystemSessionFactory.java @@ -127,8 +127,6 @@ public class DefaultRepositorySystemSessionFactory implements RepositorySystemSe private final VersionScheme versionScheme; - private final Map requestExtenders; - private final Map sessionExtenders; @SuppressWarnings("checkstyle:ParameterNumber") @@ -139,14 +137,12 @@ public class DefaultRepositorySystemSessionFactory implements RepositorySystemSe RuntimeInformation runtimeInformation, TypeRegistry typeRegistry, VersionScheme versionScheme, - Map requestExtenders, Map sessionExtenders) { this.repoSystem = repoSystem; this.eventSpyDispatcher = eventSpyDispatcher; this.runtimeInformation = runtimeInformation; this.typeRegistry = typeRegistry; this.versionScheme = versionScheme; - this.requestExtenders = requestExtenders; this.sessionExtenders = sessionExtenders; } @@ -159,11 +155,6 @@ public class DefaultRepositorySystemSessionFactory implements RepositorySystemSe public SessionBuilder newRepositorySessionBuilder(MavenExecutionRequest request) { requireNonNull(request, "request"); - // apply MavenExecutionRequestExtenders - for (MavenExecutionRequestExtender requestExtender : requestExtenders.values()) { - requestExtender.extend(request); - } - MavenSessionBuilderSupplier supplier = new MavenSessionBuilderSupplier(repoSystem); SessionBuilder sessionBuilder = supplier.get(); sessionBuilder.setArtifactTypeRegistry(new TypeRegistryAdapter(typeRegistry)); // dynamic diff --git a/impl/maven-core/src/main/java/org/apache/maven/internal/aether/PropertyContributorExtender.java b/impl/maven-core/src/main/java/org/apache/maven/internal/aether/PropertyContributorExtender.java deleted file mode 100644 index 1b967a12e0..0000000000 --- a/impl/maven-core/src/main/java/org/apache/maven/internal/aether/PropertyContributorExtender.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.maven.internal.aether; - -import javax.inject.Inject; -import javax.inject.Named; -import javax.inject.Singleton; - -import java.util.HashMap; -import java.util.Map; -import java.util.Properties; - -import org.apache.maven.api.services.Lookup; -import org.apache.maven.api.spi.PropertyContributor; -import org.apache.maven.execution.MavenExecutionRequest; - -/** - * Extender that manages {@link PropertyContributor}. - * - * @since 4.0.0 - */ -@Named -@Singleton -class PropertyContributorExtender implements MavenExecutionRequestExtender { - private final Lookup lookup; - - @Inject - PropertyContributorExtender(Lookup lookup) { - this.lookup = lookup; - } - - @SuppressWarnings({"rawtypes", "unchecked"}) - @Override - public void extend(MavenExecutionRequest mavenExecutionRequest) { - Map effectivePropertyContributors = lookup.lookupMap(PropertyContributor.class); - if (!effectivePropertyContributors.isEmpty()) { - HashMap userPropertiesMap = new HashMap<>((Map) mavenExecutionRequest.getUserProperties()); - for (PropertyContributor contributor : effectivePropertyContributors.values()) { - contributor.contribute(userPropertiesMap); - } - Properties newProperties = new Properties(); - newProperties.putAll(userPropertiesMap); - mavenExecutionRequest.setUserProperties(newProperties); - } - } -} diff --git a/impl/maven-core/src/test/java/org/apache/maven/internal/aether/DefaultRepositorySystemSessionFactoryTest.java b/impl/maven-core/src/test/java/org/apache/maven/internal/aether/DefaultRepositorySystemSessionFactoryTest.java index 3de57a0da6..875a5642ce 100644 --- a/impl/maven-core/src/test/java/org/apache/maven/internal/aether/DefaultRepositorySystemSessionFactoryTest.java +++ b/impl/maven-core/src/test/java/org/apache/maven/internal/aether/DefaultRepositorySystemSessionFactoryTest.java @@ -88,7 +88,6 @@ public class DefaultRepositorySystemSessionFactoryTest { information, defaultTypeRegistry, versionScheme, - Collections.emptyMap(), Collections.emptyMap()); MavenExecutionRequest request = new DefaultMavenExecutionRequest(); @@ -111,7 +110,6 @@ public class DefaultRepositorySystemSessionFactoryTest { information, defaultTypeRegistry, versionScheme, - Collections.emptyMap(), Collections.emptyMap()); MavenExecutionRequest request = new DefaultMavenExecutionRequest(); @@ -146,7 +144,6 @@ public class DefaultRepositorySystemSessionFactoryTest { information, defaultTypeRegistry, versionScheme, - Collections.emptyMap(), Collections.emptyMap()); PlexusConfiguration plexusConfiguration = (PlexusConfiguration) systemSessionFactory @@ -189,7 +186,6 @@ public class DefaultRepositorySystemSessionFactoryTest { information, defaultTypeRegistry, versionScheme, - Collections.emptyMap(), Collections.emptyMap()); Map headers = (Map) systemSessionFactory @@ -226,7 +222,6 @@ public class DefaultRepositorySystemSessionFactoryTest { information, defaultTypeRegistry, versionScheme, - Collections.emptyMap(), Collections.emptyMap()); int connectionTimeout = (Integer) systemSessionFactory @@ -267,7 +262,6 @@ public class DefaultRepositorySystemSessionFactoryTest { information, defaultTypeRegistry, versionScheme, - Collections.emptyMap(), Collections.emptyMap()); int connectionTimeout = (Integer) systemSessionFactory @@ -302,7 +296,6 @@ public class DefaultRepositorySystemSessionFactoryTest { information, defaultTypeRegistry, versionScheme, - Collections.emptyMap(), Collections.emptyMap()); int requestTimeout = (Integer) systemSessionFactory @@ -343,7 +336,6 @@ public class DefaultRepositorySystemSessionFactoryTest { information, defaultTypeRegistry, versionScheme, - Collections.emptyMap(), Collections.emptyMap()); int requestTimeout = (Integer) systemSessionFactory @@ -361,7 +353,6 @@ public class DefaultRepositorySystemSessionFactoryTest { information, defaultTypeRegistry, versionScheme, - Collections.emptyMap(), Collections.emptyMap()); MavenExecutionRequest request = new DefaultMavenExecutionRequest(); @@ -407,7 +398,6 @@ public class DefaultRepositorySystemSessionFactoryTest { information, defaultTypeRegistry, versionScheme, - Collections.emptyMap(), Collections.emptyMap()); MavenExecutionRequest request = new DefaultMavenExecutionRequest(); diff --git a/impl/maven-impl/src/main/java/org/apache/maven/internal/impl/DefaultSettingsBuilder.java b/impl/maven-impl/src/main/java/org/apache/maven/internal/impl/DefaultSettingsBuilder.java index 9f994856a4..ac434f2cd7 100644 --- a/impl/maven-impl/src/main/java/org/apache/maven/internal/impl/DefaultSettingsBuilder.java +++ b/impl/maven-impl/src/main/java/org/apache/maven/internal/impl/DefaultSettingsBuilder.java @@ -64,6 +64,8 @@ public class DefaultSettingsBuilder implements SettingsBuilder { private final SettingsMerger settingsMerger = new SettingsMerger(); + private final SettingsXmlFactory settingsXmlFactory; + private final Interpolator interpolator; private final SecDispatcher secDispatcher; @@ -73,14 +75,16 @@ public class DefaultSettingsBuilder implements SettingsBuilder { * Maven3 exposes decryption with other means. */ public DefaultSettingsBuilder() { - this(new DefaultInterpolator(), null); + this(new DefaultSettingsXmlFactory(), new DefaultInterpolator(), null); } /** * In Maven4 the {@link SecDispatcher} is injected and build settings are fully decrypted as well. */ @Inject - public DefaultSettingsBuilder(Interpolator interpolator, SecDispatcher secDispatcher) { + public DefaultSettingsBuilder( + SettingsXmlFactory settingsXmlFactory, Interpolator interpolator, SecDispatcher secDispatcher) { + this.settingsXmlFactory = settingsXmlFactory; this.interpolator = interpolator; this.secDispatcher = secDispatcher; } @@ -161,22 +165,18 @@ public class DefaultSettingsBuilder implements SettingsBuilder { try { try (InputStream is = settingsSource.openStream()) { - settings = request.getSession() - .getService(SettingsXmlFactory.class) - .read(XmlReaderRequest.builder() - .inputStream(is) - .location(settingsSource.getLocation()) - .strict(true) - .build()); + settings = settingsXmlFactory.read(XmlReaderRequest.builder() + .inputStream(is) + .location(settingsSource.getLocation()) + .strict(true) + .build()); } catch (XmlReaderException e) { try (InputStream is = settingsSource.openStream()) { - settings = request.getSession() - .getService(SettingsXmlFactory.class) - .read(XmlReaderRequest.builder() - .inputStream(is) - .location(settingsSource.getLocation()) - .strict(false) - .build()); + settings = settingsXmlFactory.read(XmlReaderRequest.builder() + .inputStream(is) + .location(settingsSource.getLocation()) + .strict(false) + .build()); Location loc = e.getCause() instanceof XMLStreamException xe ? xe.getLocation() : null; problems.add(new DefaultBuilderProblem( settingsSource.getLocation(), diff --git a/its/core-it-suite/src/test/java/org/apache/maven/it/MavenITmng8385PropertyContributoSPITest.java b/its/core-it-suite/src/test/java/org/apache/maven/it/MavenITmng8385PropertyContributoSPITest.java new file mode 100644 index 0000000000..62a311e491 --- /dev/null +++ b/its/core-it-suite/src/test/java/org/apache/maven/it/MavenITmng8385PropertyContributoSPITest.java @@ -0,0 +1,58 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.maven.it; + +import java.io.File; + +import org.apache.maven.shared.verifier.util.ResourceExtractor; +import org.junit.jupiter.api.Test; + +/** + * This is a test set for MNG-8385. + */ +class MavenITmng8385PropertyContributoSPITest extends AbstractMavenIntegrationTestCase { + + MavenITmng8385PropertyContributoSPITest() { + super("[4.0.0-beta-6,)"); + } + + /** + * Verify that PropertyContributorSPI is used and does it's job + */ + @Test + void testIt() throws Exception { + File testDir = ResourceExtractor.simpleExtractResources(getClass(), "/mng-8385") + .getAbsoluteFile(); + Verifier verifier; + + verifier = newVerifier(new File(testDir, "spi-extension").getAbsolutePath()); + verifier.addCliArgument("install"); + verifier.execute(); + verifier.verifyErrorFreeLog(); + + verifier = newVerifier(new File(testDir, "spi-consumer").getAbsolutePath()); + verifier.addCliArgument("validate"); + verifier.addCliArgument("-X"); + verifier.setForkJvm(true); // TODO: remove when we updated MavenCLI+Invoker + verifier.execute(); + verifier.verifyErrorFreeLog(); + + verifier.verifyTextInLog("washere!"); + } +} diff --git a/its/core-it-suite/src/test/java/org/apache/maven/it/TestSuiteOrdering.java b/its/core-it-suite/src/test/java/org/apache/maven/it/TestSuiteOrdering.java index be444bac9d..7aebc18250 100644 --- a/its/core-it-suite/src/test/java/org/apache/maven/it/TestSuiteOrdering.java +++ b/its/core-it-suite/src/test/java/org/apache/maven/it/TestSuiteOrdering.java @@ -100,6 +100,7 @@ public class TestSuiteOrdering implements ClassOrderer { * the tests are to finishing. Newer tests are also more likely to fail, so this is * a fail fast technique as well. */ + suite.addTestSuite(MavenITmng8385PropertyContributoSPITest.class); suite.addTestSuite(MavenITmng8383UnknownTypeDependenciesTest.class); suite.addTestSuite(MavenITmng8379SettingsDecryptTest.class); suite.addTestSuite(MavenITmng8336UnknownPackagingTest.class); diff --git a/its/core-it-suite/src/test/resources/mng-8385/spi-consumer/.mvn/extensions.xml b/its/core-it-suite/src/test/resources/mng-8385/spi-consumer/.mvn/extensions.xml new file mode 100644 index 0000000000..7bb92bc461 --- /dev/null +++ b/its/core-it-suite/src/test/resources/mng-8385/spi-consumer/.mvn/extensions.xml @@ -0,0 +1,7 @@ + + + org.apache.maven.its.mng8385 + spi-extension + 1.0.0-SNAPSHOT + + diff --git a/its/core-it-suite/src/test/resources/mng-8385/spi-consumer/pom.xml b/its/core-it-suite/src/test/resources/mng-8385/spi-consumer/pom.xml new file mode 100644 index 0000000000..0ef562eb2c --- /dev/null +++ b/its/core-it-suite/src/test/resources/mng-8385/spi-consumer/pom.xml @@ -0,0 +1,32 @@ + + + + org.apache.maven.its.mng8385 + spi-consumer + 1.0.0-SNAPSHOT + jar + + + + + org.apache.maven.plugins + maven-antrun-plugin + 3.1.0 + + + test + + run + + validate + + + + + + + + + + + diff --git a/its/core-it-suite/src/test/resources/mng-8385/spi-extension/pom.xml b/its/core-it-suite/src/test/resources/mng-8385/spi-extension/pom.xml new file mode 100644 index 0000000000..c31f9165c9 --- /dev/null +++ b/its/core-it-suite/src/test/resources/mng-8385/spi-extension/pom.xml @@ -0,0 +1,29 @@ + + + + org.apache.maven.its.mng8385 + spi-extension + 1.0.0-SNAPSHOT + jar + + + + org.apache.maven + maven-api-spi + 4.0.0-beta-5 + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.13.0 + + full + + + + + diff --git a/impl/maven-core/src/main/java/org/apache/maven/internal/aether/MavenExecutionRequestExtender.java b/its/core-it-suite/src/test/resources/mng-8385/spi-extension/src/main/java/org/apache/maven/its/mng8385/CustomPropertyContributor.java similarity index 68% rename from impl/maven-core/src/main/java/org/apache/maven/internal/aether/MavenExecutionRequestExtender.java rename to its/core-it-suite/src/test/resources/mng-8385/spi-extension/src/main/java/org/apache/maven/its/mng8385/CustomPropertyContributor.java index 7026e263fd..d4ed7a5024 100644 --- a/impl/maven-core/src/main/java/org/apache/maven/internal/aether/MavenExecutionRequestExtender.java +++ b/its/core-it-suite/src/test/resources/mng-8385/spi-extension/src/main/java/org/apache/maven/its/mng8385/CustomPropertyContributor.java @@ -16,16 +16,17 @@ * specific language governing permissions and limitations * under the License. */ -package org.apache.maven.internal.aether; +package org.apache.maven.its.mng8385; -import org.apache.maven.execution.MavenExecutionRequest; +import java.util.Map; -/** - * Strictly internal component able to "extend" {@link MavenExecutionRequest} in some way before it is used to - * construct resolver session. - * - * @since 4.0.0 - */ -interface MavenExecutionRequestExtender { - void extend(MavenExecutionRequest mavenExecutionRequest); +import org.apache.maven.api.di.Named; +import org.apache.maven.api.spi.PropertyContributor; + +@Named +public class CustomPropertyContributor implements PropertyContributor { + @Override + public void contribute(Map userProperties) { + userProperties.put("mng8385", "washere!"); + } }