I have a Kotlin/JVM application. I want to pass a ...
# community-support
a
I have a Kotlin/JVM application. I want to pass a lazily system property in using CommandLineArgumentProvider. But for some reason the property doesn't get set correctly and it's not accessible in the application. Properties set using
systemProperty("...", "...")
work, but these are eager and I want to avoid them. Weirdly when I print all properties in the application they appear under a different property -
sun.java.command=org.example.AppKt -DsecretFile=/Users/dev/my-project/secret-file
. I think it's weird there's an inconsistency. I would expect both approaches to work the same. Also, I think it's weird that logging
commandLine
returns a
null
as the first element:
[:run] running task [null, -DsecretFile=/Users/dev/my-project/secret-file]
Copy code
plugins {
  kotlin("jvm") version "2.1.20"
  application
}

tasks.run.configure {
  val secretFile = layout.projectDirectory.file("secret-file")
  inputs.file(secretFile)

  argumentProviders += CommandLineArgumentProvider {
    listOf("-DsecretFile=${secretFile.asFile.absoluteFile.invariantSeparatorsPath}")
  }
  systemProperty("secretFileWorks", secretFile.asFile.absoluteFile.invariantSeparatorsPath)
  doFirst {
    println("[$path] running task $commandLine")
  }
}
Gradle version is 8.13
it's gotta be a bug, so I made an issue https://github.com/gradle/gradle/issues/33231
v
Nah, that's not a bug but PEBCAK. :-)
argumentProviders
is for arguments to your main method, you will find it in your
args
. What you wanted to use is
jvmArgumentProviders
. :-)