Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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).

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading