Skip to content

Commit ecbc886

Browse files
committed
feat: auto-generate reflection info for nativeCompile
1 parent 7a8f7cf commit ecbc886

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

cli/build.gradle

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,15 +94,26 @@ application {
9494
archivesBaseName = 'spotless-cli'
9595
}
9696

97+
98+
def nativeCompileMetaDir = project.layout.buildDirectory.dir('nativeCompile/src/main/resources/native-image/' + project.group + '/' + project.name)
99+
97100
// use tasks 'nativeCompile' and 'nativeRun' to compile and run the native image
98101
graalvmNative {
99102
agent {
100103
enabled = true
101104
defaultMode = "standard"
102105
metadataCopy {
103106
inputTaskNames.add('test')
104-
mergeWithExisting = true
107+
mergeWithExisting = false
108+
outputDirectories.add(nativeCompileMetaDir.get().asFile.path)
105109
}
110+
tasksToInstrumentPredicate = new java.util.function.Predicate<Task>() {
111+
@Override
112+
boolean test(Task task) {
113+
println ("Instrumenting task: " + task.name + " " + task.name == 'test')
114+
return task.name == 'test'
115+
}
116+
}
106117
}
107118
binaries {
108119
main {
@@ -130,12 +141,38 @@ graalvmNative {
130141
}
131142
}
132143

144+
145+
tasks.named('metadataCopy') {
146+
dependsOn('test')
147+
}
148+
133149
tasks.named('nativeCompile') {
134150
dependsOn('shadowJar')
135151
classpathJar = tasks.shadowJar.archiveFile.get().asFile
136152
}
137153

154+
155+
tasks.named('shadowJar', com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) {
156+
dependsOn('metadataCopy') // produces graalvm agent info
157+
from(nativeCompileMetaDir.get().asFile.path) {
158+
into('META-INF/native-image/' + project.group + '/' + project.name)
159+
}
160+
}
161+
162+
gradle.taskGraph.whenReady { graph ->
163+
if (graph.hasTask('nativeCompile') || graph.hasTask('metadataCopy') || graph.hasTask('shadowJar')) {
164+
// enable graalvm agent using property here instead of command line `-Pagent=standard`
165+
// this collects information about reflective access and resources used by the application (e.g. GJF)
166+
project.property('agent', 'standard')
167+
}
168+
}
169+
138170
tasks.withType(Test).configureEach {
171+
if (it.name == 'test') {
172+
if (project.hasProperty('agent')) {
173+
it.inputs.property('agent', project.property('agent')) // make sure to re-run tests if agent changes
174+
}
175+
}
139176
if (it.name == 'testCliProcess') {
140177
it.dependsOn('shadowJar')
141178
it.systemProperty 'spotless.cli.shadowJar', tasks.shadowJar.archiveFile.get().asFile

0 commit comments

Comments
 (0)