Skip to content

Commit fd9e34a

Browse files
Add a DSL option that enables the native-image agent (#263)
2 parents cd1d951 + 48be6b8 commit fd9e34a

File tree

6 files changed

+58
-49
lines changed

6 files changed

+58
-49
lines changed

docs/src/docs/asciidoc/gradle-plugin.adoc

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ If you do this, the plugin will search for 2 environment variables: `GRAALVM_HOM
156156
If one of them is set, it will assume that it points to a valid GraalVM installation and completely bypass toolchain selection.
157157
Therefore, it becomes your responsibility to make sure that the environment variable points to a JDK that is compatible with your build script requirements (in particular, the language version).
158158

159+
[[configuration-options]]
159160
==== Configuration options
160161

161162
The following configuration options are available for building images:
@@ -349,18 +350,10 @@ The plugin will also substitute `{output_dir}` in the agent options to point to
349350
=== Configuring agent options
350351

351352
The native agent can be configured https://www.graalvm.org/reference-manual/native-image/Agent/[with additional options].
352-
This can be done using the `agent` configuration block:
353+
This can be done using the `agent` configuration block.
354+
Each agent option has a corresponding field in the DSL.
355+
See <<configuration-options>> for the full list of available options.
353356

354-
.Configuring agent options
355-
[source, groovy, role="multi-language-sample"]
356-
----
357-
include::../snippets/gradle/groovy/build.gradle[tags=add-agent-options]
358-
----
359-
360-
[source, kotlin, role="multi-language-sample"]
361-
----
362-
include::../snippets/gradle/kotlin/build.gradle.kts[tags=add-agent-options]
363-
----
364357

365358
[[metadata-support]]
366359
== GraalVM Reachability Metadata Support

docs/src/docs/snippets/gradle/groovy/build.gradle

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ if (providers.environmentVariable("DISABLE_TOOLCHAIN").isPresent()) {
6767

6868
// tag::all-config-options[]
6969
graalvmNative {
70-
// Injects the native-image-agent into supported tasks if `-Pagent` is specified
7170
agent {
7271
defaultMode = "standard" // Default agent mode if one isn't specified using `-Pagent=mode_name`
72+
enabled = true // Enables the agent
7373

7474
modes {
7575
// The standard agent mode generates metadata without conditions.
@@ -78,7 +78,7 @@ graalvmNative {
7878
// The conditional agent mode generates metadata with conditions.
7979
conditional {
8080
userCodeFilterPath = "path-to-filter.json" // Path to a filter file that determines classes which will be used in the metadata conditions.
81-
extrFilterPath = "path-to-another-filter.json" // Optional, extra filter used to further filter the collected metadata.
81+
extraFilterPath = "path-to-another-filter.json" // Optional, extra filter used to further filter the collected metadata.
8282
}
8383
// The direct agent mode allows users to directly pass options to the agent.
8484
direct {
@@ -165,33 +165,6 @@ graalvmNative {
165165
}
166166
// end::disable-test-support[]
167167

168-
// tag::add-agent-options[]
169-
graalvmNative {
170-
binaries.configureEach {
171-
agent {
172-
options.add("experimental-class-loader-support")
173-
}
174-
}
175-
}
176-
// end::add-agent-options[]
177-
178-
// tag::add-agent-options-individual[]
179-
graalvmNative {
180-
binaries {
181-
main {
182-
agent {
183-
options.add("experimental-class-loader-support")
184-
}
185-
}
186-
test {
187-
agent {
188-
options.add("access-filter-file=${projectDir}/src/test/resources/access-filter.json")
189-
}
190-
}
191-
}
192-
}
193-
// end::add-agent-options-individual[]
194-
195168
// tag::enable-metadata-repository[]
196169
graalvmNative {
197170
metadataRepository {

docs/src/docs/snippets/gradle/kotlin/build.gradle.kts

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,49 @@ if (providers.environmentVariable("DISABLE_TOOLCHAIN").isPresent()) {
6868

6969
// tag::all-config-options[]
7070
graalvmNative {
71+
agent {
72+
defaultMode.set("standard") // Default agent mode if one isn't specified using `-Pagent=mode_name`
73+
enabled.set(true) // Enables the agent
74+
75+
modes {
76+
// The standard agent mode generates metadata without conditions.
77+
standard {
78+
}
79+
// The conditional agent mode generates metadata with conditions.
80+
conditional {
81+
userCodeFilterPath.set("path-to-filter.json") // Path to a filter file that determines classes which will be used in the metadata conditions.
82+
extraFilterPath.set("path-to-another-filter.json") // Optional, extra filter used to further filter the collected metadata.
83+
}
84+
// The direct agent mode allows users to directly pass options to the agent.
85+
direct {
86+
// {output_dir} is a special string expanded by the plugin to where the agent files would usually be output.
87+
options.add("config-output-dir={output_dir}")
88+
options.add("experimental-configuration-with-origins")
89+
}
90+
}
91+
92+
callerFilterFiles.from("filter.json")
93+
accessFilterFiles.from("filter.json")
94+
builtinCallerFilter.set(true)
95+
builtinHeuristicFilter.set(true)
96+
enableExperimentalPredefinedClasses.set(false)
97+
enableExperimentalUnsafeAllocationTracing.set(false)
98+
trackReflectionMetadata.set(true)
99+
100+
// Copies metadata collected from tasks into the specified directories.
101+
metadataCopy {
102+
inputTaskNames.add("test") // Tasks previously executed with the agent attached.
103+
outputDirectories.add("src/main/resources/META-INF/native-image")
104+
mergeWithExisting.set(true) // Instead of copying, merge with existing metadata in the output directories.
105+
}
106+
107+
/*
108+
By default, if `-Pagent` is specified, all tasks that extend JavaForkOptions are instrumented.
109+
This can be limited to only specific tasks that match this predicate.
110+
*/
111+
tasksToInstrumentPredicate.set(t -> true)
112+
}
113+
71114
binaries {
72115
named("main") {
73116
// Main options
@@ -135,14 +178,6 @@ graalvmNative {
135178
}
136179
// end::custom-binary[]
137180

138-
// tag::add-agent-options[]
139-
graalvmNative {
140-
agent {
141-
enableExperimentalPredefinedClasses = true
142-
}
143-
}
144-
// end::add-agent-options[]
145-
146181
// tag::enable-metadata-repository[]
147182
graalvmNative {
148183
metadataRepository {

native-gradle-plugin/src/main/java/org/graalvm/buildtools/gradle/NativeImagePlugin.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ private static Provider<String> agentProperty(Project project, AgentOptions opti
523523
}
524524
return options.getDefaultMode().get();
525525
})
526-
.orElse(project.provider(() -> "disabled"));
526+
.orElse(options.getEnabled().map(enabled -> enabled ? options.getDefaultMode().get() : "disabled"));
527527
}
528528

529529
private static void registerServiceProvider(Project project, Provider<NativeImageService> nativeImageServiceProvider) {

native-gradle-plugin/src/main/java/org/graalvm/buildtools/gradle/dsl/agent/AgentOptions.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,13 @@ default void modes(Action<? super AgentModeOptions> spec) {
7272
@Optional
7373
Property<String> getDefaultMode();
7474

75+
/**
76+
* Enables the agent.
77+
*/
78+
@Input
79+
@Optional
80+
Property<Boolean> getEnabled();
81+
7582
/**
7683
* Caller-filter files that will be passed to the agent.
7784
*/

native-gradle-plugin/src/main/java/org/graalvm/buildtools/gradle/internal/DefaultGraalVmExtension.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ public DefaultGraalVmExtension(NamedDomainObjectContainer<NativeImageOptions> na
7878
AgentOptions agentOpts = getAgent();
7979
agentOpts.getTasksToInstrumentPredicate().convention(t -> true);
8080
agentOpts.getDefaultMode().convention("standard");
81+
agentOpts.getEnabled().convention(false);
8182
agentOpts.getModes().getConditional().getParallel().convention(true);
8283
agentOpts.getMetadataCopy().getMergeWithExisting().convention(false);
8384
agentOpts.getFilterableEntries().convention(Arrays.asList("org.gradle.", "java.", "org.junit."));

0 commit comments

Comments
 (0)