1010import io .opentelemetry .sdk .autoconfigure .spi .ResourceProvider ;
1111import io .opentelemetry .sdk .resources .Resource ;
1212import 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
1620public 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}
0 commit comments