This is an automated email from the ASF dual-hosted git repository.

slachiewicz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-dependency-plugin.git


The following commit(s) were added to refs/heads/master by this push:
     new c8493ad5 Move project-less artifact-coordinate resolution from 
copy-dependencies to copy
c8493ad5 is described below

commit c8493ad5c0ae795208689de2dc8c2e5cf2b7fb83
Author: Arturo Bernal <[email protected]>
AuthorDate: Sat Nov 14 13:14:00 2026 +0100

    Move project-less artifact-coordinate resolution from copy-dependencies to 
copy
    
    requiresProject=false on copy-dependencies made Maven's lifecycle task
    calculator treat the whole goal as an aggregator
    (isAggregator() || !isProjectRequired()), so mvn 
dependency:copy-dependencies
    started running once at the reactor root and skipping every module in a
    multi-module build, breaking the existing project-based use case for
    everyone, not just callers of the new -Dartifact mode. CI caught this via
    the mdep-744 IT.
    
    Revert copy-dependencies entirely and add the capability to copy, which
    has been requiresProject=false and resolved a single -Dartifact coordinate
    since 3.12.0 already. A new transitive parameter resolves that artifact's
    full dependency graph via the repository system, the same way
    dependency:get already does, and copies every resolved file instead of
    just the one artifact.
    
    Fixes #1239
---
 .../AbstractFromConfigurationMojo.java             |  2 +-
 .../dependency/fromConfiguration/CopyMojo.java     | 80 ++++++++++++++++++++++
 .../dependency/fromConfiguration/TestCopyMojo.java | 66 ++++++++++++++++++
 3 files changed, 147 insertions(+), 1 deletion(-)

diff --git 
a/src/main/java/org/apache/maven/plugins/dependency/fromConfiguration/AbstractFromConfigurationMojo.java
 
b/src/main/java/org/apache/maven/plugins/dependency/fromConfiguration/AbstractFromConfigurationMojo.java
index 1336b682..7788359e 100644
--- 
a/src/main/java/org/apache/maven/plugins/dependency/fromConfiguration/AbstractFromConfigurationMojo.java
+++ 
b/src/main/java/org/apache/maven/plugins/dependency/fromConfiguration/AbstractFromConfigurationMojo.java
@@ -378,7 +378,7 @@ public abstract class AbstractFromConfigurationMojo extends 
AbstractDependencyMo
         }
     }
 
-    private List<RemoteRepository> getRemoteRepositories() {
+    protected List<RemoteRepository> getRemoteRepositories() {
         if (remoteRepositories == null || remoteRepositories.isEmpty()) {
             return getProject().getRemoteProjectRepositories();
         }
diff --git 
a/src/main/java/org/apache/maven/plugins/dependency/fromConfiguration/CopyMojo.java
 
b/src/main/java/org/apache/maven/plugins/dependency/fromConfiguration/CopyMojo.java
index ead5e163..621a43b6 100644
--- 
a/src/main/java/org/apache/maven/plugins/dependency/fromConfiguration/CopyMojo.java
+++ 
b/src/main/java/org/apache/maven/plugins/dependency/fromConfiguration/CopyMojo.java
@@ -24,6 +24,7 @@ import java.io.File;
 import java.io.IOException;
 import java.util.List;
 
+import org.apache.maven.RepositoryUtils;
 import org.apache.maven.artifact.Artifact;
 import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
 import org.apache.maven.execution.MavenSession;
@@ -33,11 +34,14 @@ import org.apache.maven.plugins.annotations.LifecyclePhase;
 import org.apache.maven.plugins.annotations.Mojo;
 import org.apache.maven.plugins.annotations.Parameter;
 import org.apache.maven.plugins.dependency.utils.CopyUtil;
+import org.apache.maven.plugins.dependency.utils.DependencyUtil;
 import org.apache.maven.plugins.dependency.utils.ResolverUtil;
 import org.apache.maven.plugins.dependency.utils.filters.ArtifactItemFilter;
 import org.apache.maven.plugins.dependency.utils.filters.DestFileFilter;
 import org.apache.maven.project.MavenProject;
 import org.eclipse.aether.RepositorySystem;
+import org.eclipse.aether.repository.RemoteRepository;
+import org.eclipse.aether.resolution.DependencyResolutionException;
 import org.sonatype.plexus.build.incremental.BuildContext;
 
 /**
@@ -94,6 +98,19 @@ public class CopyMojo extends AbstractFromConfigurationMojo {
     @Parameter(property = "artifact")
     private String artifact;
 
+    /**
+     * Resolve the single artifact identified by {@link #artifact} (or by a 
single-entry {@link #artifactItems})
+     * together with its transitive dependencies, directly through the 
repository system, and copy all of them.
+     * Lets this goal be used like {@code dependency:get}, but copying the 
resolved files instead of only
+     * downloading them into the local repository. Not supported together with 
more than one artifact item.
+     *
+     * @since 3.12.0
+     */
+    @Parameter(property = "transitive", defaultValue = "false")
+    private boolean transitive = false;
+
+    private final ResolverUtil resolverUtil;
+
     @Inject
     public CopyMojo(
             MavenSession session,
@@ -105,6 +122,7 @@ public class CopyMojo extends AbstractFromConfigurationMojo 
{
             ResolverUtil resolverUtil) {
         super(session, buildContext, project, artifactHandlerManager, 
repositorySystem, resolverUtil);
         this.copyUtil = copyUtil;
+        this.resolverUtil = resolverUtil;
     }
 
     /**
@@ -121,9 +139,19 @@ public class CopyMojo extends 
AbstractFromConfigurationMojo {
         verifyRequirements();
         verifyExplicitDependencies();
 
+        if (transitive && getArtifactItems().size() != 1) {
+            throw new MojoFailureException("transitive requires a single 
artifact, not "
+                    + getArtifactItems().size() + " artifactItems");
+        }
+
         List<ArtifactItem> theArtifactItems = getProcessedArtifactItems(
                 new ProcessArtifactItemsRequest(stripVersion, prependGroupId, 
useBaseVersion, stripClassifier));
 
+        if (transitive) {
+            copyWithTransitiveDependencies(theArtifactItems.get(0));
+            return;
+        }
+
         for (ArtifactItem artifactItem : theArtifactItems) {
             if (artifactItem.isNeedsProcessing()) {
                 copyArtifact(artifactItem);
@@ -133,6 +161,44 @@ public class CopyMojo extends 
AbstractFromConfigurationMojo {
         }
     }
 
+    /**
+     * Resolves {@code rootItem}'s artifact together with its transitive 
dependencies, directly through the
+     * repository system, and copies every one of them to {@code rootItem}'s 
output directory.
+     *
+     * @param rootItem the single configured artifact
+     * @throws MojoExecutionException if the artifact or one of its 
dependencies cannot be resolved, or if one of
+     *                                 them cannot be copied
+     */
+    private void copyWithTransitiveDependencies(ArtifactItem rootItem) throws 
MojoExecutionException {
+        org.eclipse.aether.artifact.Artifact rootArtifact = 
RepositoryUtils.toArtifact(rootItem.getArtifact());
+        List<RemoteRepository> repositories = getRemoteRepositories();
+
+        List<org.eclipse.aether.artifact.Artifact> resolved;
+        try {
+            resolved = resolverUtil.resolveDependencies(rootArtifact, 
repositories);
+        } catch (DependencyResolutionException e) {
+            throw new MojoExecutionException("Failed to resolve " + 
rootArtifact + " with its dependencies", e);
+        }
+
+        for (org.eclipse.aether.artifact.Artifact resolvedArtifact : resolved) 
{
+            Artifact artifactToCopy = 
RepositoryUtils.toArtifact(resolvedArtifact);
+            String destFileName = DependencyUtil.getFormattedFileName(
+                    artifactToCopy, stripVersion, prependGroupId, 
useBaseVersion, stripClassifier);
+            File destFile = new File(rootItem.getOutputDirectory(), 
destFileName);
+            if (destFile.exists()) {
+                getLog().warn("Overwriting " + destFile);
+            }
+            try {
+                copyUtil.copyArtifactFile(artifactToCopy, destFile);
+            } catch (IOException e) {
+                throw new MojoExecutionException(
+                        "Failed to copy artifact '" + artifactToCopy + "' (" + 
artifactToCopy.getFile() + ") to "
+                                + destFile,
+                        e);
+            }
+        }
+    }
+
     /**
      * Resolves the artifact from the repository and copies it to the 
specified location.
      *
@@ -205,4 +271,18 @@ public class CopyMojo extends 
AbstractFromConfigurationMojo {
     public void setUseBaseVersion(boolean useBaseVersion) {
         this.useBaseVersion = useBaseVersion;
     }
+
+    /**
+     * @return returns the transitive
+     */
+    public boolean isTransitive() {
+        return this.transitive;
+    }
+
+    /**
+     * @param transitive the transitive to set
+     */
+    public void setTransitive(boolean transitive) {
+        this.transitive = transitive;
+    }
 }
diff --git 
a/src/test/java/org/apache/maven/plugins/dependency/fromConfiguration/TestCopyMojo.java
 
b/src/test/java/org/apache/maven/plugins/dependency/fromConfiguration/TestCopyMojo.java
index ec5abca6..3cb811ac 100644
--- 
a/src/test/java/org/apache/maven/plugins/dependency/fromConfiguration/TestCopyMojo.java
+++ 
b/src/test/java/org/apache/maven/plugins/dependency/fromConfiguration/TestCopyMojo.java
@@ -25,6 +25,7 @@ import java.io.IOException;
 import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.List;
@@ -238,6 +239,7 @@ class TestCopyMojo {
         assertFalse(mojo.isStripVersion());
         assertFalse(mojo.isSkip());
         assertFalse(mojo.isStripClassifier());
+        assertFalse(mojo.isTransitive());
     }
 
     @Test
@@ -784,6 +786,70 @@ class TestCopyMojo {
         assertTrue(new File(mojo.getOutputDirectory(), 
"only-1.0.jar").isFile());
     }
 
+    /**
+     * Installs a minimal jar and POM for {@code groupId:artifactId:version} 
under {@code repo}, in the repository's
+     * default layout, so the artifact can be resolved (together with the 
dependencies declared in
+     * {@code pomDependencies}, if any) from a {@code file:} remote repository 
pointed at {@code repo}.
+     */
+    private void installRemoteArtifactWithPom(
+            File repo, String groupId, String artifactId, String version, 
String pomDependencies) throws IOException {
+        File dir = new File(repo, groupId + "/" + artifactId + "/" + version);
+        Files.createDirectories(dir.toPath());
+        String base = artifactId + "-" + version;
+        Files.write(new File(dir, base + ".jar").toPath(), 
"content".getBytes(StandardCharsets.UTF_8));
+        String pom = "<project xmlns=\"http://maven.apache.org/POM/4.0.0\";>"
+                + "<modelVersion>4.0.0</modelVersion>"
+                + "<groupId>" + groupId + "</groupId>"
+                + "<artifactId>" + artifactId + "</artifactId>"
+                + "<version>" + version + "</version>"
+                + "<packaging>jar</packaging>"
+                + (pomDependencies == null ? "" : "<dependencies>" + 
pomDependencies + "</dependencies>")
+                + "</project>";
+        Files.write(new File(dir, base + ".pom").toPath(), 
pom.getBytes(StandardCharsets.UTF_8));
+    }
+
+    @Test
+    @InjectMojo(goal = "copy")
+    @MojoParameter(name = "transitive", value = "true")
+    void testTransitiveCopiesArtifactAndItsDependencies(CopyMojo mojo) throws 
Exception {
+        File repo = new File(tempDir, "transitive-remote-repo");
+        installRemoteArtifactWithPom(repo, "transitive-test", "leaf", "1.0", 
null);
+        installRemoteArtifactWithPom(
+                repo,
+                "transitive-test",
+                "root",
+                "1.0",
+                
"<dependency><groupId>transitive-test</groupId><artifactId>leaf</artifactId>"
+                        + "<version>1.0</version></dependency>");
+
+        mojo.setArtifact("transitive-test:root:1.0");
+        
mojo.setRemoteRepositories(Collections.singletonList("transitive-remote-repo::default::"
 + repo.toURI()));
+
+        mojo.execute();
+
+        assertTrue(new File(mojo.getOutputDirectory(), 
"root-1.0.jar").isFile());
+        assertTrue(new File(mojo.getOutputDirectory(), 
"leaf-1.0.jar").isFile());
+    }
+
+    @Test
+    @InjectMojo(goal = "copy")
+    @MojoParameter(name = "transitive", value = "true")
+    void testTransitiveRejectsMultipleArtifactItems(CopyMojo mojo) {
+        ArtifactItem first = new ArtifactItem();
+        first.setGroupId("transitive-test");
+        first.setArtifactId("root");
+        first.setVersion("1.0");
+        ArtifactItem second = new ArtifactItem();
+        second.setGroupId("transitive-test");
+        second.setArtifactId("leaf");
+        second.setVersion("1.0");
+        mojo.setArtifactItems(Arrays.asList(first, second));
+
+        MojoFailureException e = assertThrows(MojoFailureException.class, 
mojo::execute);
+
+        assertTrue(e.getMessage().contains("transitive"), e.getMessage());
+    }
+
     @Test
     @InjectMojo(goal = "copy")
     void testArtifactNotFound(CopyMojo mojo) throws Exception {

Reply via email to