This is an automated email from the ASF dual-hosted git repository. cstamas pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/maven-resolver.git
The following commit(s) were added to refs/heads/master by this push: new 6f70e524 [MRESOLVER-644] Artifact Transformer SPI (#605) 6f70e524 is described below commit 6f70e524289b741c1c6c992c99fa44cb07ae7523 Author: Tamas Cservenak <ta...@cservenak.net> AuthorDate: Thu Dec 12 17:45:48 2024 +0100 [MRESOLVER-644] Artifact Transformer SPI (#605) A SPI for artifact install/deploy request transformation. --- https://issues.apache.org/jira/browse/MRESOLVER-644 --- .../aether/internal/impl/DefaultDeployer.java | 8 ++++ .../aether/internal/impl/DefaultInstaller.java | 8 ++++ .../aether/internal/impl/DefaultDeployerTest.java | 1 + .../aether/internal/impl/DefaultInstallerTest.java | 2 + .../artifact/transformer/ArtifactTransformer.java | 53 ++++++++++++++++++++++ .../aether/supplier/RepositorySystemSupplier.java | 17 +++++++ .../aether/supplier/RepositorySystemSupplier.java | 17 +++++++ 7 files changed, 106 insertions(+) diff --git a/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultDeployer.java b/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultDeployer.java index c98cb556..873d121d 100644 --- a/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultDeployer.java +++ b/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultDeployer.java @@ -60,6 +60,7 @@ import org.eclipse.aether.repository.RemoteRepository; import org.eclipse.aether.repository.RepositoryPolicy; import org.eclipse.aether.spi.artifact.generator.ArtifactGenerator; import org.eclipse.aether.spi.artifact.generator.ArtifactGeneratorFactory; +import org.eclipse.aether.spi.artifact.transformer.ArtifactTransformer; import org.eclipse.aether.spi.connector.ArtifactUpload; import org.eclipse.aether.spi.connector.MetadataDownload; import org.eclipse.aether.spi.connector.MetadataUpload; @@ -99,6 +100,8 @@ public class DefaultDeployer implements Deployer { private final Map<String, MetadataGeneratorFactory> metadataFactories; + private final Map<String, ArtifactTransformer> artifactTransformers; + private final SyncContextFactory syncContextFactory; private final OfflineController offlineController; @@ -113,6 +116,7 @@ public class DefaultDeployer implements Deployer { UpdateCheckManager updateCheckManager, Map<String, ArtifactGeneratorFactory> artifactFactories, Map<String, MetadataGeneratorFactory> metadataFactories, + Map<String, ArtifactTransformer> artifactTransformers, SyncContextFactory syncContextFactory, OfflineController offlineController) { this.pathProcessor = requireNonNull(pathProcessor, "path processor cannot be null"); @@ -125,6 +129,7 @@ public class DefaultDeployer implements Deployer { this.updateCheckManager = requireNonNull(updateCheckManager, "update check manager cannot be null"); this.artifactFactories = Collections.unmodifiableMap(artifactFactories); this.metadataFactories = Collections.unmodifiableMap(metadataFactories); + this.artifactTransformers = Collections.unmodifiableMap(artifactTransformers); this.syncContextFactory = requireNonNull(syncContextFactory, "sync context factory cannot be null"); this.offlineController = requireNonNull(offlineController, "offline controller cannot be null"); } @@ -142,6 +147,9 @@ public class DefaultDeployer implements Deployer { e); } + for (ArtifactTransformer transformer : artifactTransformers.values()) { + request = transformer.transformDeployArtifacts(session, request); + } try (SyncContext syncContext = syncContextFactory.newInstance(session, true)) { return deploy(syncContext, session, request); } diff --git a/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultInstaller.java b/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultInstaller.java index 06b7a18d..fba22928 100644 --- a/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultInstaller.java +++ b/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultInstaller.java @@ -52,6 +52,7 @@ import org.eclipse.aether.repository.LocalMetadataRegistration; import org.eclipse.aether.repository.LocalRepositoryManager; import org.eclipse.aether.spi.artifact.generator.ArtifactGenerator; import org.eclipse.aether.spi.artifact.generator.ArtifactGeneratorFactory; +import org.eclipse.aether.spi.artifact.transformer.ArtifactTransformer; import org.eclipse.aether.spi.io.PathProcessor; import org.eclipse.aether.spi.synccontext.SyncContextFactory; import org.slf4j.Logger; @@ -74,6 +75,8 @@ public class DefaultInstaller implements Installer { private final Map<String, MetadataGeneratorFactory> metadataFactories; + private final Map<String, ArtifactTransformer> artifactTransformers; + private final SyncContextFactory syncContextFactory; @Inject @@ -82,12 +85,14 @@ public class DefaultInstaller implements Installer { RepositoryEventDispatcher repositoryEventDispatcher, Map<String, ArtifactGeneratorFactory> artifactFactories, Map<String, MetadataGeneratorFactory> metadataFactories, + Map<String, ArtifactTransformer> artifactTransformers, SyncContextFactory syncContextFactory) { this.pathProcessor = requireNonNull(pathProcessor, "path processor cannot be null"); this.repositoryEventDispatcher = requireNonNull(repositoryEventDispatcher, "repository event dispatcher cannot be null"); this.artifactFactories = Collections.unmodifiableMap(artifactFactories); this.metadataFactories = Collections.unmodifiableMap(metadataFactories); + this.artifactTransformers = Collections.unmodifiableMap(artifactTransformers); this.syncContextFactory = requireNonNull(syncContextFactory, "sync context factory cannot be null"); } @@ -95,6 +100,9 @@ public class DefaultInstaller implements Installer { public InstallResult install(RepositorySystemSession session, InstallRequest request) throws InstallationException { requireNonNull(session, "session cannot be null"); requireNonNull(request, "request cannot be null"); + for (ArtifactTransformer transformer : artifactTransformers.values()) { + request = transformer.transformInstallArtifacts(session, request); + } try (SyncContext syncContext = syncContextFactory.newInstance(session, false)) { return install(syncContext, session, request); } diff --git a/maven-resolver-impl/src/test/java/org/eclipse/aether/internal/impl/DefaultDeployerTest.java b/maven-resolver-impl/src/test/java/org/eclipse/aether/internal/impl/DefaultDeployerTest.java index 7b2ee78e..988d6294 100644 --- a/maven-resolver-impl/src/test/java/org/eclipse/aether/internal/impl/DefaultDeployerTest.java +++ b/maven-resolver-impl/src/test/java/org/eclipse/aether/internal/impl/DefaultDeployerTest.java @@ -92,6 +92,7 @@ public class DefaultDeployerTest { new StaticUpdateCheckManager(true), Collections.emptyMap(), Collections.emptyMap(), + Collections.emptyMap(), new StubSyncContextFactory(), new DefaultOfflineController()); diff --git a/maven-resolver-impl/src/test/java/org/eclipse/aether/internal/impl/DefaultInstallerTest.java b/maven-resolver-impl/src/test/java/org/eclipse/aether/internal/impl/DefaultInstallerTest.java index a7222c5b..6b0ee49f 100644 --- a/maven-resolver-impl/src/test/java/org/eclipse/aether/internal/impl/DefaultInstallerTest.java +++ b/maven-resolver-impl/src/test/java/org/eclipse/aether/internal/impl/DefaultInstallerTest.java @@ -88,6 +88,7 @@ public class DefaultInstallerTest { new StubRepositoryEventDispatcher(), Collections.emptyMap(), Collections.emptyMap(), + Collections.emptyMap(), new StubSyncContextFactory()); request = new InstallRequest(); listener = new RecordingRepositoryListener(); @@ -339,6 +340,7 @@ public class DefaultInstallerTest { new StubRepositoryEventDispatcher(), Collections.emptyMap(), Collections.emptyMap(), + Collections.emptyMap(), new StubSyncContextFactory()); request = new InstallRequest(); diff --git a/maven-resolver-spi/src/main/java/org/eclipse/aether/spi/artifact/transformer/ArtifactTransformer.java b/maven-resolver-spi/src/main/java/org/eclipse/aether/spi/artifact/transformer/ArtifactTransformer.java new file mode 100644 index 00000000..cc87c675 --- /dev/null +++ b/maven-resolver-spi/src/main/java/org/eclipse/aether/spi/artifact/transformer/ArtifactTransformer.java @@ -0,0 +1,53 @@ +/* + * 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.eclipse.aether.spi.artifact.transformer; + +import org.eclipse.aether.RepositorySystemSession; +import org.eclipse.aether.deployment.DeployRequest; +import org.eclipse.aether.installation.InstallRequest; + +/** + * Install and deploy artifact transformer. This component can mangle install and deploy requests, replace artifacts, + * add new artifacts and so on. + * + * @since 2.0.5 + */ +public interface ArtifactTransformer { + /** + * Transform install artifacts. + * + * @param session never {@code null} + * @param request never {@code null} + * @return the transformed request, never {@code null} + */ + default InstallRequest transformInstallArtifacts(RepositorySystemSession session, InstallRequest request) { + return request; + } + + /** + * Transform deploy artifacts. + * + * @param session never {@code null} + * @param request never {@code null} + * @return the transformed request, never {@code null} + */ + default DeployRequest transformDeployArtifacts(RepositorySystemSession session, DeployRequest request) { + return request; + } +} diff --git a/maven-resolver-supplier-mvn3/src/main/java/org/eclipse/aether/supplier/RepositorySystemSupplier.java b/maven-resolver-supplier-mvn3/src/main/java/org/eclipse/aether/supplier/RepositorySystemSupplier.java index b8189e21..a1616deb 100644 --- a/maven-resolver-supplier-mvn3/src/main/java/org/eclipse/aether/supplier/RepositorySystemSupplier.java +++ b/maven-resolver-supplier-mvn3/src/main/java/org/eclipse/aether/supplier/RepositorySystemSupplier.java @@ -114,6 +114,7 @@ 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.artifact.transformer.ArtifactTransformer; import org.eclipse.aether.spi.checksums.ProvidedChecksumsSource; import org.eclipse.aether.spi.checksums.TrustedChecksumsSource; import org.eclipse.aether.spi.connector.RepositoryConnectorFactory; @@ -728,6 +729,7 @@ public class RepositorySystemSupplier implements Supplier<RepositorySystem> { getRepositoryEventDispatcher(), getArtifactGeneratorFactories(), getMetadataGeneratorFactories(), + getArtifactTransformers(), getSyncContextFactory()); } @@ -750,6 +752,7 @@ public class RepositorySystemSupplier implements Supplier<RepositorySystem> { getUpdateCheckManager(), getArtifactGeneratorFactories(), getMetadataGeneratorFactories(), + getArtifactTransformers(), getSyncContextFactory(), getOfflineController()); } @@ -911,6 +914,20 @@ public class RepositorySystemSupplier implements Supplier<RepositorySystem> { // Maven provided + private Map<String, ArtifactTransformer> artifactTransformers; + + public final Map<String, ArtifactTransformer> getArtifactTransformers() { + checkClosed(); + if (artifactTransformers == null) { + artifactTransformers = createArtifactTransformers(); + } + return artifactTransformers; + } + + protected Map<String, ArtifactTransformer> createArtifactTransformers() { + return new HashMap<>(); + } + private Map<String, MetadataGeneratorFactory> metadataGeneratorFactories; public final Map<String, MetadataGeneratorFactory> getMetadataGeneratorFactories() { diff --git a/maven-resolver-supplier-mvn4/src/main/java/org/eclipse/aether/supplier/RepositorySystemSupplier.java b/maven-resolver-supplier-mvn4/src/main/java/org/eclipse/aether/supplier/RepositorySystemSupplier.java index 917f2d68..ca3f1ac6 100644 --- a/maven-resolver-supplier-mvn4/src/main/java/org/eclipse/aether/supplier/RepositorySystemSupplier.java +++ b/maven-resolver-supplier-mvn4/src/main/java/org/eclipse/aether/supplier/RepositorySystemSupplier.java @@ -118,6 +118,7 @@ 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.artifact.transformer.ArtifactTransformer; import org.eclipse.aether.spi.checksums.ProvidedChecksumsSource; import org.eclipse.aether.spi.checksums.TrustedChecksumsSource; import org.eclipse.aether.spi.connector.RepositoryConnectorFactory; @@ -732,6 +733,7 @@ public class RepositorySystemSupplier implements Supplier<RepositorySystem> { getRepositoryEventDispatcher(), getArtifactGeneratorFactories(), getMetadataGeneratorFactories(), + getArtifactTransformers(), getSyncContextFactory()); } @@ -754,6 +756,7 @@ public class RepositorySystemSupplier implements Supplier<RepositorySystem> { getUpdateCheckManager(), getArtifactGeneratorFactories(), getMetadataGeneratorFactories(), + getArtifactTransformers(), getSyncContextFactory(), getOfflineController()); } @@ -915,6 +918,20 @@ public class RepositorySystemSupplier implements Supplier<RepositorySystem> { // Maven provided + private Map<String, ArtifactTransformer> artifactTransformers; + + public final Map<String, ArtifactTransformer> getArtifactTransformers() { + checkClosed(); + if (artifactTransformers == null) { + artifactTransformers = createArtifactTransformers(); + } + return artifactTransformers; + } + + protected Map<String, ArtifactTransformer> createArtifactTransformers() { + return new HashMap<>(); + } + private Map<String, MetadataGeneratorFactory> metadataGeneratorFactories; public final Map<String, MetadataGeneratorFactory> getMetadataGeneratorFactories() {