You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by sl...@apache.org on 2023/01/16 20:39:35 UTC

[maven-dependency-plugin] branch master updated: [MDEP-843] Use GAV fields to support them as parameters

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 9ba72639 [MDEP-843] Use GAV fields to support them as parameters
9ba72639 is described below

commit 9ba726395a6a63a14033c9df6fcb4fa23a756911
Author: Piotrek Żygieło <pz...@users.noreply.github.com>
AuthorDate: Mon Jan 9 20:46:13 2023 +0100

    [MDEP-843] Use GAV fields to support them as parameters
---
 .../maven/plugins/dependency/ListClassesMojo.java  | 19 +++++++--
 .../plugins/dependency/TestListClassesMojo.java    | 48 ++++++++++++++++++++++
 2 files changed, 63 insertions(+), 4 deletions(-)

diff --git a/src/main/java/org/apache/maven/plugins/dependency/ListClassesMojo.java b/src/main/java/org/apache/maven/plugins/dependency/ListClassesMojo.java
index 18b11b0d..13a879d2 100644
--- a/src/main/java/org/apache/maven/plugins/dependency/ListClassesMojo.java
+++ b/src/main/java/org/apache/maven/plugins/dependency/ListClassesMojo.java
@@ -192,13 +192,24 @@ public class ListClassesMojo extends AbstractMojo {
         }
     }
 
+    boolean hasGAVSpecified() {
+        return artifact != null || (groupId != null && artifactId != null && version != null);
+    }
+
     private ProjectBuildingRequest makeBuildingRequest() throws MojoExecutionException, MojoFailureException {
-        if (artifact == null) {
-            throw new MojoFailureException("You must specify an artifact, "
-                    + "e.g. -Dartifact=org.apache.maven.plugins:maven-downloader-plugin:1.0");
+        if (!hasGAVSpecified()) {
+            throw new MojoFailureException("You must specify an artifact OR GAV separately, "
+                    + "e.g. -Dartifact=org.apache.maven.plugins:maven-downloader-plugin:1.0 OR "
+                    + "-DgroupId=org.apache.maven.plugins -DartifactId=maven-downloader-plugin -Dversion=1.0");
         }
 
-        String[] tokens = artifact.split(":");
+        String[] tokens = artifact != null
+                ? artifact.split(":")
+                : classifier != null
+                        ? new String[] {groupId, artifactId, version, packaging, classifier}
+                        : packaging != null
+                                ? new String[] {groupId, artifactId, version, packaging}
+                                : new String[] {groupId, artifactId, version};
         if (tokens.length < 3 || tokens.length > 5) {
             throw new MojoFailureException("Invalid artifact, you must specify "
                     + "groupId:artifactId:version[:packaging[:classifier]] " + artifact);
diff --git a/src/test/java/org/apache/maven/plugins/dependency/TestListClassesMojo.java b/src/test/java/org/apache/maven/plugins/dependency/TestListClassesMojo.java
index 88cc4c16..f16d4aa2 100644
--- a/src/test/java/org/apache/maven/plugins/dependency/TestListClassesMojo.java
+++ b/src/test/java/org/apache/maven/plugins/dependency/TestListClassesMojo.java
@@ -82,6 +82,30 @@ public class TestListClassesMojo extends AbstractDependencyMojoTestCase {
         Assert.assertEquals(expectedLogArgs, infoArgsCaptor.getAllValues());
     }
 
+    public void testListClassesNotTransitiveByGAV() throws Exception {
+        Path path = Paths.get("src/test/resources/unit/list-test/testListClassesNotTransitive.txt");
+        List<String> expectedLogArgs = Files.readAllLines(path);
+        ArgumentCaptor<String> infoArgsCaptor = ArgumentCaptor.forClass(String.class);
+
+        setVariableValueToObject(
+                mojo,
+                "remoteRepositories",
+                "central::default::https://repo.maven.apache.org/maven2,"
+                        + "central::::https://repo.maven.apache.org/maven2," + "https://repo.maven.apache.org/maven2");
+        setVariableValueToObject(mojo, "groupId", "org.apache.commons");
+        setVariableValueToObject(mojo, "artifactId", "commons-lang3");
+        setVariableValueToObject(mojo, "version", "3.6");
+        setVariableValueToObject(mojo, "transitive", Boolean.FALSE);
+
+        Log log = Mockito.mock(Log.class);
+        mojo.setLog(log);
+
+        mojo.execute();
+
+        Mockito.verify(log, Mockito.times(expectedLogArgs.size())).info(infoArgsCaptor.capture());
+        Assert.assertEquals(expectedLogArgs, infoArgsCaptor.getAllValues());
+    }
+
     public void testListClassesTransitive() throws Exception {
         Path path = Paths.get("src/test/resources/unit/list-test/testListClassesTransitive.txt");
         List<String> expectedLogArgs = Files.readAllLines(path);
@@ -103,4 +127,28 @@ public class TestListClassesMojo extends AbstractDependencyMojoTestCase {
         Mockito.verify(log, Mockito.times(expectedLogArgs.size())).info(infoArgsCaptor.capture());
         Assert.assertEquals(expectedLogArgs, infoArgsCaptor.getAllValues());
     }
+
+    public void testListClassesTransitiveByGAV() throws Exception {
+        Path path = Paths.get("src/test/resources/unit/list-test/testListClassesTransitive.txt");
+        List<String> expectedLogArgs = Files.readAllLines(path);
+        ArgumentCaptor<String> infoArgsCaptor = ArgumentCaptor.forClass(String.class);
+
+        setVariableValueToObject(
+                mojo,
+                "remoteRepositories",
+                "central::default::https://repo.maven.apache.org/maven2,"
+                        + "central::::https://repo.maven.apache.org/maven2," + "https://repo.maven.apache.org/maven2");
+        setVariableValueToObject(mojo, "groupId", "org.apache.commons");
+        setVariableValueToObject(mojo, "artifactId", "commons-lang3");
+        setVariableValueToObject(mojo, "version", "3.6");
+        setVariableValueToObject(mojo, "transitive", Boolean.TRUE);
+
+        Log log = Mockito.mock(Log.class);
+        mojo.setLog(log);
+
+        mojo.execute();
+
+        Mockito.verify(log, Mockito.times(expectedLogArgs.size())).info(infoArgsCaptor.capture());
+        Assert.assertEquals(expectedLogArgs, infoArgsCaptor.getAllValues());
+    }
 }