You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by sj...@apache.org on 2026/05/07 21:45:46 UTC

(maven-dependency-plugin) branch master updated: Added ability to provide arbitrary dependencies to properties mojo (#1561)

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

slawekjaranowski 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 963fd3c8 Added ability to provide arbitrary dependencies to properties mojo (#1561)
963fd3c8 is described below

commit 963fd3c87b5933e07c131ca19b1eef4a12839740
Author: treilhes <p....@free.fr>
AuthorDate: Thu May 7 23:45:41 2026 +0200

    Added ability to provide arbitrary dependencies to properties mojo (#1561)
---
 .../maven/plugins/dependency/PropertiesMojo.java   | 71 +++++++++++++++++++++-
 .../plugins/dependency/TestPropertiesMojo.java     | 45 +++++++++++++-
 2 files changed, 111 insertions(+), 5 deletions(-)

diff --git a/src/main/java/org/apache/maven/plugins/dependency/PropertiesMojo.java b/src/main/java/org/apache/maven/plugins/dependency/PropertiesMojo.java
index 22f0d835..7e67274a 100644
--- a/src/main/java/org/apache/maven/plugins/dependency/PropertiesMojo.java
+++ b/src/main/java/org/apache/maven/plugins/dependency/PropertiesMojo.java
@@ -20,6 +20,7 @@ package org.apache.maven.plugins.dependency;
 
 import javax.inject.Inject;
 
+import java.util.List;
 import java.util.Set;
 
 import org.apache.maven.artifact.Artifact;
@@ -29,7 +30,10 @@ 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.annotations.ResolutionScope;
+import org.apache.maven.plugins.dependency.utils.ParamArtifact;
+import org.apache.maven.plugins.dependency.utils.ResolverUtil;
 import org.apache.maven.project.MavenProject;
+import org.eclipse.aether.resolution.ArtifactResolutionException;
 
 /**
  * Goal that sets a property pointing to the artifact file for each project dependency. For each dependency (direct and
@@ -53,9 +57,12 @@ public class PropertiesMojo extends AbstractMojo {
      */
     private final MavenProject project;
 
+    private final ResolverUtil resolverUtil;
+
     @Inject
-    public PropertiesMojo(MavenProject project) {
+    public PropertiesMojo(MavenProject project, ResolverUtil resolverUtil) {
         this.project = project;
+        this.resolverUtil = resolverUtil;
     }
 
     /**
@@ -67,8 +74,31 @@ public class PropertiesMojo extends AbstractMojo {
     private boolean skip;
 
     /**
-     * Main entry into mojo. Gets the list of dependencies and iterates through setting a property for each artifact.
+     * Extra artifacts that can be provided to the plugin. For each artifact in this list a property will be set
+     * similar to the project artifacts discovered. This allows callers to supply additional
+     * resolved dependencies for which properties pointing to the artifact file shall be created.
      *
+     * Example usage in plugin configuration:
+     * <pre>
+     * &lt;extraArtifacts&gt;
+     *   &lt;extraArtifact&gt;
+     *      &lt;artifact&gt;org.example:my-artifact:jar:1.0&lt;/artifact&gt;
+     *   &lt;/extraArtifact&gt;
+     *   &lt;extraArtifact&gt;
+     *      &lt;groupId&gt;org.example&lt;/groupId&gt;
+     *      &lt;artifactId&gt;my-artifact&lt;/artifactId&gt;
+     *      &lt;version&gt;1.0&lt;/version&gt;
+     *   &lt;/extraArtifact&gt;
+     * &lt;/extraArtifacts&gt;
+     * </pre>
+     * @since 3.10.1
+     */
+    @Parameter
+    private List<ParamArtifact> extraArtifacts;
+
+    /**
+     * Main entry into mojo. Gets the list of dependencies and iterates through setting a property for each artifact.
+     * Gets the list of declared plugin's extra artifacts and iterates through setting a property for each artifact.
      * @throws MojoExecutionException with a message if an error occurs
      */
     @Override
@@ -86,6 +116,28 @@ public class PropertiesMojo extends AbstractMojo {
                             artifact.getDependencyConflictId(),
                             artifact.getFile().getAbsolutePath());
         }
+
+        if (extraArtifacts != null) {
+            try {
+                for (ParamArtifact paramArtifact : extraArtifacts) {
+
+                    if (!paramArtifact.isDataSet()) {
+                        throw new MojoExecutionException("You must specify an artifact OR GAV separately");
+                    }
+
+                    org.eclipse.aether.artifact.Artifact artifact =
+                            resolverUtil.createArtifactFromParams(paramArtifact);
+                    artifact = resolverUtil.resolveArtifact(artifact, project.getRemoteProjectRepositories());
+
+                    this.project
+                            .getProperties()
+                            .setProperty(
+                                    toConflictId(artifact), artifact.getFile().getAbsolutePath());
+                }
+            } catch (ArtifactResolutionException e) {
+                throw new MojoExecutionException("Couldn't download artifact: " + e.getMessage(), e);
+            }
+        }
     }
 
     /**
@@ -94,4 +146,19 @@ public class PropertiesMojo extends AbstractMojo {
     public boolean isSkip() {
         return skip;
     }
+
+    private String toConflictId(org.eclipse.aether.artifact.Artifact artifact) {
+        // The conflict ID is the same as the one used by Maven's Artifact class, which is
+        // groupId:artifactId:type[:classifier]
+        StringBuilder sb = new StringBuilder();
+        sb.append(artifact.getGroupId())
+                .append(":")
+                .append(artifact.getArtifactId())
+                .append(":")
+                .append(artifact.getExtension());
+        if (artifact.getClassifier() != null && !artifact.getClassifier().isEmpty()) {
+            sb.append(":").append(artifact.getClassifier());
+        }
+        return sb.toString();
+    }
 }
diff --git a/src/test/java/org/apache/maven/plugins/dependency/TestPropertiesMojo.java b/src/test/java/org/apache/maven/plugins/dependency/TestPropertiesMojo.java
index 3b9037f9..3d163d5c 100644
--- a/src/test/java/org/apache/maven/plugins/dependency/TestPropertiesMojo.java
+++ b/src/test/java/org/apache/maven/plugins/dependency/TestPropertiesMojo.java
@@ -23,27 +23,30 @@ import javax.inject.Inject;
 import java.io.File;
 import java.util.Arrays;
 import java.util.HashSet;
+import java.util.List;
 
 import org.apache.maven.api.plugin.testing.InjectMojo;
 import org.apache.maven.api.plugin.testing.MojoExtension;
 import org.apache.maven.api.plugin.testing.MojoTest;
 import org.apache.maven.artifact.Artifact;
+import org.apache.maven.plugins.dependency.utils.ParamArtifact;
 import org.apache.maven.project.MavenProject;
+import org.codehaus.plexus.util.ReflectionUtils;
 import org.junit.jupiter.api.Test;
 import org.mockito.Mockito;
 
 import static org.junit.jupiter.api.Assertions.assertTrue;
 import static org.mockito.Mockito.when;
 
-@MojoTest
-public class TestPropertiesMojo {
+@MojoTest(realRepositorySession = true)
+class TestPropertiesMojo {
 
     @Inject
     private MavenProject project;
 
     @Test
     @InjectMojo(goal = "properties")
-    public void testSetProperties(PropertiesMojo mojo) throws Exception {
+    void testSetProperties(PropertiesMojo mojo) throws Exception {
 
         Artifact artifact1 = Mockito.mock(Artifact.class);
         Artifact artifact2 = Mockito.mock(Artifact.class);
@@ -63,4 +66,40 @@ public class TestPropertiesMojo {
         assertTrue(project.getProperties().getProperty("group1:artifact1").endsWith("artifact1.jar"));
         assertTrue(project.getProperties().getProperty("group2:artifact2").endsWith("artifact2.jar"));
     }
+
+    /**
+     * tests the proper discovery and configuration of the mojo for extra artifacts
+     * Each artifact should be resolved and set a property in the project
+     * @throws Exception in case of errors
+     */
+    @Test
+    @InjectMojo(goal = "properties")
+    void testSetPropertiesForExtractArtifacts(PropertiesMojo mojo) throws Exception {
+
+        String depId1 = "org.apache.commons:commons-lang3:jar";
+        String depId2 = "org.apache.commons:commons-collections4:jar";
+
+        ParamArtifact a1 = new ParamArtifact();
+        a1.setGroupId("org.apache.commons");
+        a1.setArtifactId("commons-lang3");
+        a1.setVersion("3.6");
+
+        ParamArtifact a2 = new ParamArtifact();
+        a2.setGroupId("org.apache.commons");
+        a2.setArtifactId("commons-collections4");
+        a2.setVersion("4.4");
+
+        List<ParamArtifact> artifacts = Arrays.asList(a1, a2);
+
+        // 3. Inject into private field
+        ReflectionUtils.setVariableValueInObject(mojo, "extraArtifacts", artifacts);
+
+        mojo.execute();
+
+        assertTrue(project.getProperties().containsKey(depId1));
+        assertTrue(new File(project.getProperties().getProperty(depId1)).exists());
+
+        assertTrue(project.getProperties().containsKey(depId2));
+        assertTrue(new File(project.getProperties().getProperty(depId1)).exists());
+    }
 }