Skip to content

Commit 2dbc5ee

Browse files
authored
Fix #393: stop reading LATEST from the deprecated Artifact constant (#424)
* Fix #393: stop reading LATEST from the deprecated Artifact constant getAetherArtifact() filled in a missing version from org.apache.maven.artifact.Artifact.LATEST_VERSION, which Maven 4 deprecates, and that import was the only thing the class used it for. The value stays "LATEST", now a private constant. Passing null instead, as the issue suggests, would not work: Aether's DefaultArtifact normalises a null version to the empty string, so two-part coordinates would resolve against g:a:pom: rather than the newest version. Adds a test for the coordinate parsing, which had none. * Inline the LATEST literal instead of a single-use constant
1 parent f32d61a commit 2dbc5ee

2 files changed

Lines changed: 71 additions & 2 deletions

File tree

‎src/main/java/org/apache/maven/plugins/help/AbstractHelpMojo.java‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import java.io.Writer;
2424
import java.nio.file.Files;
2525

26-
import org.apache.maven.artifact.Artifact;
2726
import org.apache.maven.execution.MavenSession;
2827
import org.apache.maven.model.building.ModelBuildingRequest;
2928
import org.apache.maven.plugin.AbstractMojo;
@@ -48,6 +47,7 @@
4847
* @since 2.1
4948
*/
5049
public abstract class AbstractHelpMojo extends AbstractMojo {
50+
5151
/** The maximum length of a display line. */
5252
protected static final int LINE_LENGTH = 79;
5353

@@ -144,7 +144,8 @@ protected org.eclipse.aether.artifact.Artifact getAetherArtifact(String artifact
144144
case 2:
145145
groupId = artifactParts[0];
146146
artifactId = artifactParts[1];
147-
version = Artifact.LATEST_VERSION;
147+
// Not null: Aether normalises a null version to "", not to a metaversion.
148+
version = "LATEST";
148149
break;
149150
case 3:
150151
groupId = artifactParts[0];
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.maven.plugins.help;
20+
21+
import org.apache.maven.plugin.MojoExecutionException;
22+
import org.eclipse.aether.artifact.Artifact;
23+
import org.junit.jupiter.api.Test;
24+
25+
import static org.junit.jupiter.api.Assertions.assertEquals;
26+
import static org.junit.jupiter.api.Assertions.assertThrows;
27+
28+
/**
29+
* Tests the coordinate parsing in {@link AbstractHelpMojo#getAetherArtifact(String, String)}.
30+
*/
31+
class AbstractHelpMojoTest {
32+
33+
private final AbstractHelpMojo mojo = new AbstractHelpMojo(null, null) {
34+
@Override
35+
public void execute() {}
36+
};
37+
38+
@Test
39+
void coordinatesWithoutAVersionGetTheLatestMetaversion() throws Exception {
40+
Artifact artifact = mojo.getAetherArtifact("org.apache.maven:maven-core", "pom");
41+
42+
assertEquals("org.apache.maven", artifact.getGroupId());
43+
assertEquals("maven-core", artifact.getArtifactId());
44+
assertEquals("pom", artifact.getExtension());
45+
// The literal, not the constant: leaving the version out has to keep producing a
46+
// metaversion the resolver recognises. A null version would arrive here as "" instead.
47+
assertEquals("LATEST", artifact.getVersion());
48+
}
49+
50+
@Test
51+
void coordinatesWithAVersionKeepIt() throws Exception {
52+
Artifact artifact = mojo.getAetherArtifact("org.apache.maven:maven-core:3.9.16", "pom");
53+
54+
assertEquals("3.9.16", artifact.getVersion());
55+
}
56+
57+
@Test
58+
void coordinatesWithTooManyPartsAreRejected() {
59+
assertThrows(
60+
MojoExecutionException.class,
61+
() -> mojo.getAetherArtifact("org.apache.maven:maven-core:3.9.16:extra", "pom"));
62+
}
63+
64+
@Test
65+
void emptyCoordinatesAreRejected() {
66+
assertThrows(IllegalArgumentException.class, () -> mojo.getAetherArtifact("", "pom"));
67+
}
68+
}

0 commit comments

Comments
 (0)