I need to pass a JVM arg to task desktopRun of typ...
# gradle
f
I need to pass a JVM arg to task desktopRun of type KotlinRunJvm. But I'm struggling to think of how, I tried to find the tasks with type, but get this error. This API is not intended to be used outside of JetBrains / outside the Kotlin Gradle Plugin
Copy code
tasks.withType<KotlinJvmRun> {
    jvmArgs = "-DmainClass=MainKt"
}
v
tasks.withType<...> { ... }
is bad anyway as you break task-configuration avoidance, but instead
tasks.withType<...>.configureEach { ... }
should be used. Besides that, if you shouldn't use
KotlinJvmRun
, you should probably instead use its supertype
JavaExec
. Besides that with both you configure all tasks of that type, so you should probably more do
tasks.withType<JavaExec>().named { it == "desktopRun" }.configureEach { ... }
f
That's perfect 🙂 but I've tried many things and args are not being passed through, it works when invoking gradle with args though
v
What do you mean by "it works when invoking gradle with args though"?
f
shared:desktopRun -DmainClass=com.timerx.MainKt
v
Well, that is not nearly the same thing. With that you are setting a system property for Gradle. With the other config you are setting a system property for the JVM started by that task. Those have nothing to do with each other besides that one starts the other.
f
Ah gotcha, I tried this with no luck
Copy code
tasks.withType<JavaExec>().named { it == "desktopRun" }.configureEach {
        systemProperty("mainClass", "MainKt")
v
Yeah, same thing
You set a system property for the process run by
desktopRun
just that you now use the DSL
This still has nothing to do with the system property you set for the Gradlebuild on the commandline
And don't now get the idea to do
System.setProperty
in the
configureEach
, even if that works somehow, it will not really do what you want and just make your build flaky
f
Just tried it for fun, and it didn't work, so not that much fun in the end
Oh it does work hahahaha
v
But not really
or, not properly, however you want to call it
Don't do it
f
What are the drawbacks?
v
That it does not work as you imagine
f
Awesome, thanks for the help 🙂 I do appreciate it ... I won't do it
v
You set this system property now whenever the
desktopRun
task is configured. For example imagine that you also have a
desktopRun2
task where you need a different value, so you do the same non-sense. Then you run Gradle like
./gradlew desktopRun desktopRun2
. Both task get configured. The System property is set to the value that was configured while configuring the second task and is like that for the whole Gradle invocation.
🙌 1
f
Thanks for the explanation, makes lots of sense
👌 1
Solution is obvious in the end haha thanks @Vampire tasks.withType<JavaExec>().named { it == "desktopRun" }.configureEach { mainClass = "MainKt"}
👌 1