51

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.

simont
  • 68,704
  • 18
  • 117
  • 136
Dean Hiller
  • 19,235
  • 25
  • 129
  • 212
  • :) Just wanted to draw your attention to [this section](http://stackoverflow.com/faq#signatures) of the FAQ with regards to using signatures; your questions and answers on StackOverflow are already "signed" with your user card, so in-post signatures are unnecessary. – simont Apr 14 '12 at 20:41
  • 1
    well, that just sucks...a signature is something that shows up IN your post not something you have to click a link to get to...the rate at which the signature is read goes way down because of the barrier of that link(in fact, it probably drops by 90% or so :( which is very unfortunate as stackoverflow could help alot of people out with REAL signatures). – Dean Hiller Sep 13 '12 at 01:44
  • See this answer if you're using the Gradle wrapper: https://stackoverflow.com/a/46596203/924597 – Shorn Oct 05 '17 at 23:32

6 Answers6

42

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.

skipy
  • 4,032
  • 2
  • 23
  • 19
  • 10
    This is the default behavior according to http://gradle.org/docs/current/dsl/org.gradle.plugins.ide.eclipse.model.EclipseClasspath.html#org.gradle.plugins.ide.eclipse.model.EclipseClasspath:downloadSources – KARASZI István Oct 15 '12 at 11:31
  • 1
    Maybe so, but my `gradle cleanEclipse eclipse` did not download source jars until I after added the indicated config to `build.gradle`. Thanks @skipy ! – BalRog Mar 14 '19 at 17:36
29

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

Martin Dow
  • 5,273
  • 4
  • 30
  • 43
  • Yep, in my experience, Gradle downloads the source .jar and also links the source in the build path settings so that you can debug the source code. – djangofan Feb 21 '13 at 19:07
  • 3
    One thing to note: after I cleaned my gradle cache I had to `gradle cleanEclipse eclipse` and then close and reopen the eclipse project to get eclipse to see the source again. – iain Jul 24 '13 at 11:23
  • What about Android Studio? After small research, I think it can't be done… – Nicolas Siver Aug 05 '13 at 07:04
  • 2
    Sometimes you don't see the sources in Eclipse WTP although they're downloaded by gradle. In such case you need to manually push Web App Libraries to the bottom of build path or use [the solution from Andreas Kuhrwahl](http://stackoverflow.com/questions/12836089/why-is-eclipse-not-attaching-3rd-party-libs-source-files-to-a-wtp-faceted-gradle) – Piohen Sep 10 '13 at 11:56
  • thanks. in my case, gradle does download sources, but eclipse does not link the sources and the project... :-( – OhadR Mar 20 '16 at 09:49
26

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.

Community
  • 1
  • 1
Ruslan Stelmachenko
  • 4,987
  • 2
  • 36
  • 51
16

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
Community
  • 1
  • 1
Paul Podgorsek
  • 2,416
  • 3
  • 19
  • 22
6

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

Why is Eclipse not attaching 3rd party libs source files to a WTP-faceted Gradle project?

Community
  • 1
  • 1
jasop
  • 892
  • 11
  • 13
0

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.

K. Taylor
  • 431
  • 5
  • 8