Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MDEP-843] Use GAV fields to support them as parameters #282

Merged
merged 1 commit into from
Jan 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<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);
Expand All @@ -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<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());
}
}