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 18b11b0d1..13a879d20 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 @@ private void printClassesFromArtifactResult(ArtifactResult result) throws IOExce } } + 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 88cc4c16f..f16d4aa22 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 void testListClassesNotTransitive() throws Exception { Assert.assertEquals(expectedLogArgs, infoArgsCaptor.getAllValues()); } + public void testListClassesNotTransitiveByGAV() throws Exception { + Path path = Paths.get("src/test/resources/unit/list-test/testListClassesNotTransitive.txt"); + List expectedLogArgs = Files.readAllLines(path); + ArgumentCaptor 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 expectedLogArgs = Files.readAllLines(path); @@ -103,4 +127,28 @@ public void testListClassesTransitive() throws Exception { 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 expectedLogArgs = Files.readAllLines(path); + ArgumentCaptor 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()); + } }