gradle.properties
file is usually included in version control so all contributors can have a consistent environment, so you cannot store user-defined properties in them, and user-defined properties are applied to all projects at the same time. I want to set org.gradle.java.home
property for projects incompatible with my default JDK, but I can't use project-level gradle.properties
because path to JDK is not the same on different machines and I only want the JDK path to be changed for specific projects, not all of them.
Is there a convenient way to specify local Gradle properties for a single project?
One possible solution:
jdk-8
, jdk-11
// file ~/.gradle/gradle.properties
jdk-8=/path/to/jdk8
jdk-11=/path/to/jdk11
jdk-8
and jdk-11
will be resolved as properties by Gradle in any build.
javaCompile
task for specific projects in root build.gradle.kts
:fun Project.setupCustomJdk(jdkAlias: String) {
findProperty(jdkAlias)?.toString()?.trim()?.let { customJdkPath ->
tasks.withType<JavaCompile> {
options.isFork = true
options.forkOptions.javaHome = File(customJdkPath)
logger.info("Task javaCompile using JDK from $customJdkPath")
}
} ?: logger.warn("Unknown property '$jdkAlias'")
}
project(":app").setupCustomJdk("jdk-11")
project(":lib").setupCustomJdk("jdk-8")