Skip to content

Commit 62c288b

Browse files
committed
Preserve test runner argLine when configuring agent
1 parent 2491c2c commit 62c288b

3 files changed

Lines changed: 21 additions & 5 deletions

File tree

native-maven-plugin/docs/functional/tracing-agent.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ the destination. Application agent runs are attached to the `exec-maven-plugin`
2525
the native plugin's `<agentExecutionId>` configuration value; the default execution ID is
2626
`java-agent` so existing POMs keep working. Generated test-agent arguments derived from project paths
2727
must remain a single JVM argument when passed through Maven test runners, including when those paths
28-
contain spaces.
28+
contain spaces. When adding that argument, Maven must preserve the test runner's existing `argLine`
29+
options.
2930
When Maven configures an instrumented test or application execution, normal build
3031
output must report the Maven-managed agent output directory so users can find collected metadata
3132
without debug logging, aligning with

native-maven-plugin/src/main/java/org/graalvm/buildtools/maven/NativeExtension.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,13 @@ static String quoteAgentArgumentForArgLine(String agentArgument) {
137137
return agentArgument;
138138
}
139139

140+
static String appendAgentArgument(String existingArgLine, String agentArgument) {
141+
String quotedAgentArgument = quoteAgentArgumentForArgLine(agentArgument);
142+
return existingArgLine == null || existingArgLine.isBlank()
143+
? quotedAgentArgument
144+
: existingArgLine + " " + quotedAgentArgument;
145+
}
146+
140147
static String agentOutputDirectoryFor(String baseDir, Context context) {
141148
return (baseDir + "/native/agent-output/" + context).replace('/', File.separatorChar);
142149
}
@@ -293,11 +300,10 @@ private static boolean configureAgentForPlugin(Plugin plugin, String agentArgume
293300
Xpp3Dom systemPropertyVariables = findOrAppend(configuration, SYSTEM_PROPERTY_VARIABLES);
294301
Xpp3Dom agent = findOrAppend(systemPropertyVariables, NATIVEIMAGE_IMAGECODE);
295302
agent.setValue("agent");
296-
Xpp3Dom argLine = new Xpp3Dom("argLine");
303+
Xpp3Dom argLine = findOrAppend(configuration, "argLine");
297304
// Surefire/Failsafe parse argLine as one command-line string.
298-
// Keep spaced output paths as one JVM argument. §FS-tracing-agent.3.
299-
argLine.setValue(quoteAgentArgumentForArgLine(agentArgument));
300-
configuration.addChild(argLine);
305+
// Preserve user-supplied JVM options while keeping spaced output paths as one argument. §FS-tracing-agent.3.
306+
argLine.setValue(appendAgentArgument(argLine.getValue(), agentArgument));
301307
findOrAppend(configuration, "jvm").setValue(getGraalvmJava());
302308
});
303309
return true;

native-maven-plugin/src/test/groovy/org/graalvm/buildtools/maven/NativeExtensionTest.groovy

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,15 @@ class NativeExtensionTest extends Specification {
139139
NativeExtension.deleteSessionAgentConfigDirectory(second)
140140
}
141141

142+
def "appends the test agent argument to an existing test runner argLine"() {
143+
given:
144+
def agentArgument = "-agentlib:native-image-agent=config-output-dir=target/native/agent-output/test"
145+
146+
expect:
147+
NativeExtension.appendAgentArgument("-javaagent:existing-agent.jar", agentArgument) ==
148+
"-javaagent:existing-agent.jar " + agentArgument
149+
}
150+
142151
private static Plugin plugin(String artifactId) {
143152
def plugin = new Plugin()
144153
plugin.artifactId = artifactId

0 commit comments

Comments
 (0)