Skip to content

Commit bd05bf0

Browse files
Implement a fallback method for getting effective JVM for Maven Surefire executions (#7493)
1 parent 1efda00 commit bd05bf0

File tree

2 files changed

+76
-3
lines changed

2 files changed

+76
-3
lines changed

dd-java-agent/instrumentation/maven-3.2.1/src/main/java/datadog/trace/instrumentation/maven3/MavenLifecycleParticipant.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ private Collection<MavenTestExecution> getTestExecutionsByJvmPath(
200200
if (forkedJvmPath == null) {
201201
Plugin plugin = mojoExecution.getPlugin();
202202
String pluginKey = plugin.getKey();
203-
LOGGER.warn(
203+
LOGGER.debug(
204204
"Could not determine forked JVM path for plugin {} execution {} in project {}",
205205
pluginKey,
206206
mojoExecution.getExecutionId(),

dd-java-agent/instrumentation/maven-3.2.1/src/main/java/datadog/trace/instrumentation/maven3/MavenUtils.java

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
package datadog.trace.instrumentation.maven3;
22

33
import datadog.trace.util.MethodHandles;
4+
import datadog.trace.util.Strings;
45
import java.io.File;
56
import java.lang.invoke.MethodHandle;
67
import java.lang.reflect.Method;
78
import java.nio.file.Path;
89
import java.nio.file.Paths;
10+
import java.util.HashMap;
911
import java.util.Iterator;
1012
import java.util.List;
13+
import java.util.Map;
1114
import java.util.Objects;
1215
import java.util.Properties;
1316
import javax.annotation.Nullable;
@@ -20,11 +23,15 @@
2023
import org.apache.maven.plugin.MavenPluginManager;
2124
import org.apache.maven.plugin.Mojo;
2225
import org.apache.maven.plugin.MojoExecution;
26+
import org.apache.maven.plugin.PluginParameterExpressionEvaluator;
2327
import org.apache.maven.plugin.descriptor.MojoDescriptor;
2428
import org.apache.maven.plugin.descriptor.PluginDescriptor;
2529
import org.apache.maven.project.MavenProject;
30+
import org.apache.maven.toolchain.Toolchain;
31+
import org.apache.maven.toolchain.ToolchainManager;
2632
import org.codehaus.plexus.PlexusContainer;
2733
import org.codehaus.plexus.classworlds.realm.ClassRealm;
34+
import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
2835
import org.codehaus.plexus.configuration.PlexusConfiguration;
2936
import org.codehaus.plexus.configuration.xml.XmlPlexusConfiguration;
3037
import org.codehaus.plexus.logging.Logger;
@@ -330,6 +337,9 @@ public static Path getForkedJvmPath(
330337
return null;
331338
}
332339
String forkedJvm = getEffectiveJvm(session, mojoExecution, mavenPluginManager);
340+
if (forkedJvm == null) {
341+
forkedJvm = getEffectiveJvmFallback(session, mojoExecution);
342+
}
333343
return forkedJvm != null ? Paths.get(forkedJvm) : null;
334344
}
335345

@@ -396,7 +406,70 @@ private static MethodHandle findGetEffectiveJvmMethod(
396406
}
397407
mojoClass = mojoClass.getSuperclass();
398408
} 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+
}
401474
}
402475
}

0 commit comments

Comments
 (0)