How to set a build property (the `-P` commandline argument) to null in Gradle?

568 Views Asked by At

When I execute gradle with the following command:

./gradlew publishToMavenLocal -PsplainVersion=null

I got the following error:

...
> Could not resolve all files for configuration ':graph-commons:scalaCompilerPlugins'.
   > Could not resolve io.tryp:splain_2.13.6:null.

It appears that null is not parsed properly as a control token, instead it becomes a string. Is there a method to help Gradle to understand it as a real null value?

1

There are 1 best solutions below

1
On BEST ANSWER

Gradle will effectively treat anything as an argument of some kind. There is no null checking whatsoever.

So if you need to ensure a null argument is not given to your build script, then you need to validate the argument is not null where it is being used.

tasks.register("verifyNonNull") {
    onlyIf {
        property("splainVersion") != 'null'
    }
}

If you are curious of Gradle's logic, check out the source of CommandLineParser.