Skip to content

Commit 6e634bf

Browse files
authored
Maven version parse xml rather than html (#2665)
1 parent 3d284af commit 6e634bf

File tree

9 files changed

+198
-145
lines changed

9 files changed

+198
-145
lines changed

apm-agent-attach-cli/src/main/java/co/elastic/apm/attach/AgentDownloader.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
*/
4444
public class AgentDownloader {
4545

46-
private static final String VERSION_EXTRACTION_REGEX = "<a href.+>([0-9]+.[0-9.]+.[0-9.]+)/</a>";
46+
private static final Pattern VERSION_EXTRACTION_REGEX = Pattern.compile("<version>(.+?)</version>");
4747
private static final String AGENT_GROUP_ID = "co.elastic.apm";
4848
private static final String AGENT_ARTIFACT_ID = "elastic-apm-agent";
4949
private static final String CLI_JAR_VERSION;
@@ -220,25 +220,27 @@ void verifyPgpSignature(final Path agentJarFile, final String mavenAgentUrlStrin
220220
}
221221

222222
static String findLatestVersion() throws Exception {
223-
String agentArtifactMavenBaseUrl = getAgentArtifactMavenBaseUrl();
224-
HttpURLConnection httpURLConnection = openConnection(agentArtifactMavenBaseUrl);
225-
TreeSet<Version> versions = parseMavenArtifactHtml(httpURLConnection.getInputStream());
223+
String agentArtifactMavenMetadatUrl = getAgentArtifactMavenBaseUrl() + "/maven-metadata.xml";
224+
HttpURLConnection httpURLConnection = openConnection(agentArtifactMavenMetadatUrl);
225+
TreeSet<Version> versions = parseMavenMetadataXml(httpURLConnection.getInputStream());
226226
if (versions.isEmpty()) {
227-
throw new IllegalStateException("Failed to parse agent versions from the contents of " + agentArtifactMavenBaseUrl);
227+
throw new IllegalStateException("Failed to parse agent versions from the contents of " + agentArtifactMavenMetadatUrl);
228228
}
229229
return versions.last().toString();
230230
}
231231

232-
static TreeSet<Version> parseMavenArtifactHtml(InputStream htmlInputStream) throws IOException {
232+
static TreeSet<Version> parseMavenMetadataXml(InputStream htmlInputStream) throws IOException {
233233
TreeSet<Version> versions = new TreeSet<>();
234234
BufferedReader versionsHtmlReader = new BufferedReader(new InputStreamReader(htmlInputStream));
235-
Pattern pattern = Pattern.compile(VERSION_EXTRACTION_REGEX);
236235
String line;
237236
while ((line = versionsHtmlReader.readLine()) != null) {
238237
try {
239-
Matcher matcher = pattern.matcher(line);
238+
Matcher matcher = VERSION_EXTRACTION_REGEX.matcher(line);
240239
if (matcher.find()) {
241-
versions.add(Version.of(matcher.group(1)));
240+
Version version = Version.of(matcher.group(1));
241+
if (!version.hasSuffix()) {
242+
versions.add(version);
243+
}
242244
}
243245
} catch (Exception e) {
244246
// ignore, probably a regex false positive

apm-agent-attach-cli/src/test/java/co/elastic/apm/attach/AgentDownloaderTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,11 @@ void testLatestVersion() throws Exception {
104104

105105
@Test
106106
void testAgentArtifactMavenPageParsing() throws IOException {
107-
TreeSet<Version> versions = AgentDownloader.parseMavenArtifactHtml(AgentDownloaderTest.class.getResourceAsStream(
108-
"/MavenCentral_agent_artifact.html"));
107+
TreeSet<Version> versions = AgentDownloader.parseMavenMetadataXml(AgentDownloaderTest.class.getResourceAsStream(
108+
"/maven-metadata.xml"));
109109
assertThat(versions).hasSize(50);
110110
assertThat(versions.first().toString()).isEqualTo("0.5.1");
111111
assertThat(versions.last().toString()).isEqualTo("1.31.0");
112+
assertThat(versions).doesNotContain(Version.of("1.0.0.RC1"));
112113
}
113114
}

apm-agent-attach-cli/src/test/resources/MavenCentral_agent_artifact.html

Lines changed: 0 additions & 84 deletions
This file was deleted.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<metadata>
2+
<groupId>co.elastic.apm</groupId>
3+
<artifactId>elastic-apm-agent</artifactId>
4+
<versioning>
5+
<latest>1.31.0</latest>
6+
<release>1.31.0</release>
7+
<versions>
8+
<version>0.5.1</version>
9+
<version>0.6.0</version>
10+
<version>0.6.1</version>
11+
<version>0.6.2</version>
12+
<version>0.7.0</version>
13+
<version>0.7.1</version>
14+
<version>1.0.0.RC1</version>
15+
<version>1.0.0</version>
16+
<version>1.0.1</version>
17+
<version>1.1.0</version>
18+
<version>1.2.0</version>
19+
<version>1.3.0</version>
20+
<version>1.4.0</version>
21+
<version>1.5.0</version>
22+
<version>1.6.0</version>
23+
<version>1.6.1</version>
24+
<version>1.7.0</version>
25+
<version>1.8.0</version>
26+
<version>1.9.0</version>
27+
<version>1.10.0</version>
28+
<version>1.10.1</version>
29+
<version>1.11.0</version>
30+
<version>1.12.0</version>
31+
<version>1.13.0</version>
32+
<version>1.14.0</version>
33+
<version>1.15.0</version>
34+
<version>1.16.0</version>
35+
<version>1.17.0</version>
36+
<version>1.18.0.RC1</version>
37+
<version>1.18.0</version>
38+
<version>1.18.1</version>
39+
<version>1.19.0</version>
40+
<version>1.20.0</version>
41+
<version>1.21.0</version>
42+
<version>1.22.0</version>
43+
<version>1.23.0</version>
44+
<version>1.24.0</version>
45+
<version>1.25.0</version>
46+
<version>1.26.0</version>
47+
<version>1.26.1</version>
48+
<version>1.26.2</version>
49+
<version>1.27.0</version>
50+
<version>1.27.1</version>
51+
<version>1.28.0</version>
52+
<version>1.28.1</version>
53+
<version>1.28.2</version>
54+
<version>1.28.3</version>
55+
<version>1.28.4</version>
56+
<version>1.29.0</version>
57+
<version>1.30.0</version>
58+
<version>1.30.1</version>
59+
<version>1.31.0</version>
60+
<version>1.32.0-RC1</version>
61+
<version>1.32.0.RC1</version>
62+
</versions>
63+
<lastUpdated>20220517064838</lastUpdated>
64+
</versioning>
65+
</metadata>

0 commit comments

Comments
 (0)