how to tell gradle to download all the source jars

Ideally, we would like to add a task for downloading all the source jars for the first level and transitive dependencies of our project. Is there a way to do that?

If not, is there a command line option to supply like maven has to get all the sources downloaded onto our machines?

It seems like that should just be the default these days at least for first level dependencies as it gives you the javadoc in eclipse then which is very nice when doing the code completion stuff.


The eclipse task can be configured with downloadSources. Following is an example of that configuration

apply plugin: 'java'
apply plugin: 'eclipse'

eclipse {
    classpath {
       downloadSources=true
    }
}

So run

gradle cleanEclipse eclipse

to have it download sources.


If you use Eclipse and want to navigate the source code of your dependencies there, then the Eclipse plugin does this for you.

Install the eclipse plugin by adding apply plugin: "eclipse" to your build.gradle file. Then run gradle eclipse to generate the Eclipse .project, .classpath and .settings files. The plugin will download all available sources automatically and add references them in the .classpath file (see the sourcepath attribute of the classpathentry element).

To import the project into Eclipse, choose File > Import... > Existing Projects into Workspace and select your project.

(I'm not sure whether the Idea plugin does the same for Idea users, but it may do).


Another catch not mentioned in other answers is when you are using mavenLocal() repository in your gradle.build file. If there are downloaded jar in that local maven repo but no downloaded sources or javadocs in that repo, then gradle will not even try to download javadocs or sources for you. Even with enabled eclipse.classpath.downloadJavadoc and eclipse.classpath.downloadSources.

The solution is to remove mavenLocal() from repositories or place it to bottom of the list. Or you can setup maven to download sources and javadocs and clean your maven local repository (~/.m2/repository).

A more detailed description of the problem is here.


Here is how to add the required configuration in Gradle using the IDEs' plugins:

For Eclipse:

apply plugin: 'java'
apply plugin: 'eclipse'

eclipse {
    classpath {
        downloadJavadoc = true
        downloadSources = true
    }
}

For IntelliJ

apply plugin: 'java'
apply plugin: 'idea'

idea {
    module {
        downloadJavadoc = true
        downloadSources = true
    }
}

To run these plugins:

gradle cleanEclipse eclipse
gradle cleanIdea idea