Run gradle in offline mode

Sometimes it’s necessary to run `gradle` in offline mode. E.g. when having no internet connection or when you want to prevent `gradle` to download files in case you have a limited download volume. This describes how to run gradle in offline mode.

In the end I describe two possible ways here. The first is the standard way you can find in the `gradle` documentation. The second one a more improved one that imho is more convenient.

Using a parameter

The most easiest way is to simply add the parameter `–offline`. This prevents that `gradle` is using the network for any communication. The disadvantage of that solution is that you have to add that parameter to every command.

The following code snippet shows that you can do that by simply adding the parameter to the `gradle` command.

# running the build task
gradle build --offline

That’s it.

Using init.gradle

A much more convenient way is to have an `init.gradle` file that can be set globally for all `gradle` builds.

In that case simply add a file called `init.gradle` in the folder `.gradle` of the user home directory. That file will be executed for every `gradle` build. This might look like the following:

startParameter.offline=true

// adding a hint so you remember when gradle is running in offline mode
if(startParameter.offline)
{
        println "======================"
        println "gradle in offline mode"
        println "======================"
}

Having the hint there prints you `gradle in offline mode` before the build so you don’t forget the offline mode is active.

So you only have to set the boolean value to `true` or `false` depending on whether you want to run offline or online.

Sources

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.