How to create a "local" Gradle task?

I am new to Gradle.

Is there any way to make something similar to local.properties but for tasks? I wanna have a task that I am going to use only in my local environment for debugging purposes, but it should be ignored by the version control system. Is there any adequate way to do that?

I came up with an idea to create a local.gradle file and add apply from: 'local.gradle' to the build.gradle, and then adding local.gradle to .gitignore. It works on my local machine but fails on CI (because VCS knows nothing about local.gradle).

Thank you in advance for any ideas.

>Solution :

I see two ways to approach that:

  1. Use Initialization Scripts. Those are per-user (i.e. local to the user, not the project) scripts that are executed before loading the actual gradle build script or

  2. Add a small stub to your "real" build.gradle (or build.gradle.kts if you’re using Kotlin) to load a local build file if it’s present.:

    if (file("./local.gradle").exists()) {
      apply from: "./local.gradle"
    }
    

    Then add local.gradle to your .gitignore and it will be loaded when present, but ignored when it’s missing.

Using Initialization scripts has the advantage that you don’t need to do anything at all to your projects "real" files to use it, but might be an oversized hammer: those scripts will apply to all gradle builds you run, not just the ones of the current project.

Leave a Reply