how to tell gradle to download all the source jars

Gradle

Gradle Problem Overview


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.

Gradle Solutions


Solution 1 - Gradle

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.

Solution 2 - Gradle

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

Solution 3 - Gradle

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.

Solution 4 - Gradle

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

Solution 5 - Gradle

Piohen's comment above should be it's own answer (since it was the only solution that worked for me)

  1. Right click your project, then select "Build Path" --> "Configure Build Path";
  2. Select "Order and export"
  3. Select "Web App Libraries", and click "Bottom" button, then the "Web App Libraries" will be on the bottom;

And to get this into the Gradle Eclipse plugin (so you don't need to do it manually every time):

https://stackoverflow.com/questions/12836089/why-is-eclipse-not-attaching-3rd-party-libs-source-files-to-a-wtp-faceted-gradle

Solution 6 - Gradle

There is only one problem here. This only works if you are generating NEW projects. If you are working on mature projects that can't be re-generated using Gradle, the above suggestions will not work.

One thing I should add is that the version of Gradle/Builsdhip plugin strongly depends on the version of Java you use to start Eclipse. They must all be compatible. I had to switch my Eclipse (current version) from Java 11 back to Java 8 to fix Buildship (3.0.1) errors. We are, and have been, stuck on Gradle 4.2.1 for some time due to API changes in Gradle breaking our build. So to move to Java 11 we have to move to a new version of Gradle, Buildship, and Eclipse. Ugh! Oh yeah, this also fixed the issue mentioned in this thread. Source is now avalable again.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDean HillerView Question on Stackoverflow
Solution 1 - GradleskipyView Answer on Stackoverflow
Solution 2 - GradleMartin DowView Answer on Stackoverflow
Solution 3 - GradleRuslan StelmachenkoView Answer on Stackoverflow
Solution 4 - GradlePaul PodgorsekView Answer on Stackoverflow
Solution 5 - GradlejasopView Answer on Stackoverflow
Solution 6 - GradleK. TaylorView Answer on Stackoverflow