Warm tip: This article is reproduced from serverfault.com, please click

How can I specify local gradle.properties per project?

发布于 2021-07-21 16:49:07

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?

Questioner
Daniikk1012
Viewed
11
mikhail.sharashin 2021-07-22 18:10:11

One possible solution:

  1. Define an alias for custom JDK, e.g.: jdk-8, jdk-11
  2. All contributors should specify paths to those JDK via build environment (any way from Gradle Docs). For example:
// 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.

  1. Configure 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")