Build.gradle.kts, proper properties?

Continuing the discussion from Dotted properties in Gradle:

am wondering two things, (first) how do you recommend to do properties nowadays, if not dotted? and (second) how would you recommend to put and read them so there is a default, and one can override from the command line? i try to do what is one line in maven, setting the --release option of javac:

    <maven.compiler.release>8</maven.compiler.release>

mvn clean package -Dmaven.compiler.release=17

in gradle this sounds a little complicated with editing 2 files like this, so i might do something wrong:

gradle.properties:
javacRelease = 8

build.gradle.kts:
val javacRelease: String by project
...
tasks.compileJava {
	options.release.set(Integer.parseInt(javacRelease))
}

to get on the command line:
gradle clean build -PjavacRelease=17
1 Like

Dotted properties are fine. It is really up to you how you want to do them. The convention is dot-separated segments with camel-case used for the segments. But, it will not blow up if you don’t follow this.

Here is how you can fetch a property from the command line with a default if it is not provided:

val javacRelease = (project.findProperty("javacRelease") ?: "11") as String

This should be easy to convert to Groovy if you are using that DSL.

Actually for your concrete example my recommendation would be:
Just don’t do it.
I don’t see any reason why one should specify that property from the command line.
The project defines which minimum Java version it supports.
Any newer Java version can use it, so why should you set it to a newer version from the command line?

Besides that, I would also not use options.release nowadays, but the Java toolchains feature.
Then you can even build a project with minimum version 17 while running Gradle with 8, as it will then auto-provision a matching JDK and use that for compilation and so on.

1 Like