Skip to content

Commit 4cd6a0a

Browse files
Maven 4.0 compatibility. Stop using o.a.m.rtinfo.internal.DefaultRuntimeInformation (open-telemetry#1679)
1 parent a334a79 commit 4cd6a0a

File tree

2 files changed

+64
-6
lines changed

2 files changed

+64
-6
lines changed

maven-extension/src/main/java/io/opentelemetry/maven/resources/MavenResourceProvider.java

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,22 @@
1010
import io.opentelemetry.sdk.autoconfigure.spi.ResourceProvider;
1111
import io.opentelemetry.sdk.resources.Resource;
1212
import io.opentelemetry.semconv.ServiceAttributes;
13-
import org.apache.maven.rtinfo.RuntimeInformation;
14-
import org.apache.maven.rtinfo.internal.DefaultRuntimeInformation;
13+
import java.io.IOException;
14+
import java.io.InputStream;
15+
import java.util.Properties;
16+
import org.apache.maven.Maven;
17+
import org.slf4j.Logger;
18+
import org.slf4j.LoggerFactory;
1519

1620
public class MavenResourceProvider implements ResourceProvider {
1721

22+
private static final Logger logger = LoggerFactory.getLogger(MavenResourceProvider.class);
23+
1824
@Override
1925
public Resource createResource(ConfigProperties config) {
20-
// TODO verify if there is solution to retrieve the RuntimeInformation instance loaded by the
21-
// Maven Plexus Launcher
22-
RuntimeInformation runtimeInformation = new DefaultRuntimeInformation();
2326
return Resource.builder()
2427
.put(ServiceAttributes.SERVICE_NAME, MavenOtelSemanticAttributes.SERVICE_NAME_VALUE)
25-
.put(ServiceAttributes.SERVICE_VERSION, runtimeInformation.getMavenVersion())
28+
.put(ServiceAttributes.SERVICE_VERSION, getMavenRuntimeVersion())
2629
.put(
2730
MavenOtelSemanticAttributes.TELEMETRY_DISTRO_NAME,
2831
MavenOtelSemanticAttributes.TELEMETRY_DISTRO_NAME_VALUE)
@@ -31,4 +34,40 @@ public Resource createResource(ConfigProperties config) {
3134
MavenOtelSemanticAttributes.TELEMETRY_DISTRO_VERSION_VALUE)
3235
.build();
3336
}
37+
38+
/**
39+
* Recopy of <a
40+
* href="https://github.com/apache/maven/blob/maven-4.0.0-rc-2/compat/maven-compat/src/main/java/org/apache/maven/execution/DefaultRuntimeInformation.java">
41+
* <code>org.apache.maven.rtinfo.internal.DefaultRuntimeInformation#getMavenVersion()</code></a>
42+
* that is not available in Maven 4.0+
43+
*/
44+
static String getMavenRuntimeVersion() {
45+
String mavenVersion;
46+
Properties props = new Properties();
47+
String resource = "META-INF/maven/org.apache.maven/maven-core/pom.properties";
48+
49+
try (InputStream is = Maven.class.getResourceAsStream("/" + resource)) {
50+
if (is != null) {
51+
props.load(is);
52+
} else {
53+
logger.warn(
54+
"Could not locate {} on classpath, Maven runtime information not available", resource);
55+
}
56+
} catch (IOException e) {
57+
String msg = "Could not parse " + resource + ", Maven runtime information not available";
58+
if (logger.isDebugEnabled()) {
59+
logger.warn(msg, e);
60+
} else {
61+
logger.warn(msg);
62+
}
63+
}
64+
65+
String version = props.getProperty("version", "").trim();
66+
if (!version.startsWith("${")) {
67+
mavenVersion = version;
68+
} else {
69+
mavenVersion = "";
70+
}
71+
return mavenVersion;
72+
}
3473
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.maven.resources;
7+
8+
import static org.assertj.core.api.Assertions.assertThat;
9+
10+
import org.junit.jupiter.api.Test;
11+
12+
class MavenResourceProviderTest {
13+
14+
@Test
15+
void testGetMavenVersion() {
16+
String mavenVersion = MavenResourceProvider.getMavenRuntimeVersion();
17+
assertThat(mavenVersion).isEqualTo("3.5.0");
18+
}
19+
}

0 commit comments

Comments
 (0)