|
1 | 1 | package datadog.trace.instrumentation.maven3; |
2 | 2 |
|
3 | 3 | import datadog.trace.util.MethodHandles; |
| 4 | +import datadog.trace.util.Strings; |
4 | 5 | import java.io.File; |
5 | 6 | import java.lang.invoke.MethodHandle; |
6 | 7 | import java.lang.reflect.Method; |
7 | 8 | import java.nio.file.Path; |
8 | 9 | import java.nio.file.Paths; |
| 10 | +import java.util.HashMap; |
9 | 11 | import java.util.Iterator; |
10 | 12 | import java.util.List; |
| 13 | +import java.util.Map; |
11 | 14 | import java.util.Objects; |
12 | 15 | import java.util.Properties; |
13 | 16 | import javax.annotation.Nullable; |
|
20 | 23 | import org.apache.maven.plugin.MavenPluginManager; |
21 | 24 | import org.apache.maven.plugin.Mojo; |
22 | 25 | import org.apache.maven.plugin.MojoExecution; |
| 26 | +import org.apache.maven.plugin.PluginParameterExpressionEvaluator; |
23 | 27 | import org.apache.maven.plugin.descriptor.MojoDescriptor; |
24 | 28 | import org.apache.maven.plugin.descriptor.PluginDescriptor; |
25 | 29 | import org.apache.maven.project.MavenProject; |
| 30 | +import org.apache.maven.toolchain.Toolchain; |
| 31 | +import org.apache.maven.toolchain.ToolchainManager; |
26 | 32 | import org.codehaus.plexus.PlexusContainer; |
27 | 33 | import org.codehaus.plexus.classworlds.realm.ClassRealm; |
| 34 | +import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator; |
28 | 35 | import org.codehaus.plexus.configuration.PlexusConfiguration; |
29 | 36 | import org.codehaus.plexus.configuration.xml.XmlPlexusConfiguration; |
30 | 37 | import org.codehaus.plexus.logging.Logger; |
@@ -330,6 +337,9 @@ public static Path getForkedJvmPath( |
330 | 337 | return null; |
331 | 338 | } |
332 | 339 | String forkedJvm = getEffectiveJvm(session, mojoExecution, mavenPluginManager); |
| 340 | + if (forkedJvm == null) { |
| 341 | + forkedJvm = getEffectiveJvmFallback(session, mojoExecution); |
| 342 | + } |
333 | 343 | return forkedJvm != null ? Paths.get(forkedJvm) : null; |
334 | 344 | } |
335 | 345 |
|
@@ -396,7 +406,70 @@ private static MethodHandle findGetEffectiveJvmMethod( |
396 | 406 | } |
397 | 407 | mojoClass = mojoClass.getSuperclass(); |
398 | 408 | } while (mojoClass != null); |
399 | | - return methodHandles.method( |
400 | | - "org.apache.maven.plugin.surefire.AbstractSurefireMojo", "getEffectiveJvm"); |
| 409 | + return null; |
| 410 | + } |
| 411 | + |
| 412 | + /** Fallback method that attempts to recreate the logic used by Maven Surefire plugin */ |
| 413 | + private static String getEffectiveJvmFallback(MavenSession session, MojoExecution mojoExecution) { |
| 414 | + try { |
| 415 | + PlexusConfiguration configuration = getPomConfiguration(mojoExecution); |
| 416 | + PlexusConfiguration jvm = configuration.getChild("jvm"); |
| 417 | + if (jvm != null) { |
| 418 | + String value = jvm.getValue(); |
| 419 | + if (Strings.isNotBlank(value)) { |
| 420 | + ExpressionEvaluator expressionEvaluator = |
| 421 | + new PluginParameterExpressionEvaluator(session, mojoExecution); |
| 422 | + Object evaluatedValue = expressionEvaluator.evaluate(value); |
| 423 | + if (evaluatedValue != null && Strings.isNotBlank(String.valueOf(evaluatedValue))) { |
| 424 | + return String.valueOf(evaluatedValue); |
| 425 | + } |
| 426 | + } |
| 427 | + } |
| 428 | + |
| 429 | + PlexusConfiguration jdkToolchain = configuration.getChild("jdkToolchain"); |
| 430 | + if (jdkToolchain != null) { |
| 431 | + ExpressionEvaluator expressionEvaluator = |
| 432 | + new PluginParameterExpressionEvaluator(session, mojoExecution); |
| 433 | + Map<String, String> toolchainConfig = new HashMap<>(); |
| 434 | + for (PlexusConfiguration child : jdkToolchain.getChildren()) { |
| 435 | + Object value = expressionEvaluator.evaluate(child.getValue()); |
| 436 | + if (value != null && Strings.isNotBlank(String.valueOf(value))) { |
| 437 | + toolchainConfig.put(child.getName(), String.valueOf(value)); |
| 438 | + } |
| 439 | + } |
| 440 | + |
| 441 | + if (!toolchainConfig.isEmpty()) { |
| 442 | + PlexusContainer container = MavenUtils.getContainer(session); |
| 443 | + ToolchainManager toolchainManager = container.lookup(ToolchainManager.class); |
| 444 | + |
| 445 | + MojoDescriptor mojoDescriptor = mojoExecution.getMojoDescriptor(); |
| 446 | + PluginDescriptor pluginDescriptor = mojoDescriptor.getPluginDescriptor(); |
| 447 | + ClassRealm pluginRealm = pluginDescriptor.getClassRealm(); |
| 448 | + MethodHandles methodHandles = new MethodHandles(pluginRealm); |
| 449 | + MethodHandle getToolchains = |
| 450 | + methodHandles.method( |
| 451 | + ToolchainManager.class, |
| 452 | + "getToolchains", |
| 453 | + MavenSession.class, |
| 454 | + String.class, |
| 455 | + Map.class); |
| 456 | + List<Toolchain> toolchains = |
| 457 | + methodHandles.invoke( |
| 458 | + getToolchains, toolchainManager, session, "jdk", toolchainConfig); |
| 459 | + if (toolchains.isEmpty()) { |
| 460 | + LOGGER.debug("Could not find toolchains for {}", toolchainConfig); |
| 461 | + return null; |
| 462 | + } |
| 463 | + |
| 464 | + Toolchain toolchain = toolchains.iterator().next(); |
| 465 | + return toolchain.findTool("java"); |
| 466 | + } |
| 467 | + } |
| 468 | + return null; |
| 469 | + |
| 470 | + } catch (Exception e) { |
| 471 | + LOGGER.debug("Error while getting effective JVM for mojo {}", mojoExecution, e); |
| 472 | + return null; |
| 473 | + } |
401 | 474 | } |
402 | 475 | } |
0 commit comments