67

I am using Gradle 2.0. What should I write in build.gradle so that the javadocs and sources are also downloaded along with the jars?

khateeb
  • 5,265
  • 15
  • 58
  • 114
  • 1
    If you have access to the source, you can do this without gradle. When you open your class file in the IDE for which you want the source, select `download sources`. – IgorGanapolsky Dec 26 '18 at 21:34
  • @IgorGanapolskyigra it is not possible to download docs that way. https://youtrack.jetbrains.com/issue/IDEA-286107 – Željko Trogrlić Jan 04 '22 at 09:17

5 Answers5

150

I guess your question is related to a dev workspace, here are links explaining 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 & Android Studio

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

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

To run these plugins:

gradle cleanEclipse eclipse
gradle cleanIdea idea
Tayfun Yaşar
  • 422
  • 2
  • 9
  • 24
Paul Podgorsek
  • 2,416
  • 3
  • 19
  • 22
  • 63
    While this works good, I find it's a horrible that your information about your IDE is leaked into build system. Why build system should even care what you are using? – Igor Konoplyanko Mar 27 '17 at 12:03
  • 9
    You must see it the other way round: the Gradle build file is the reference when it comes to knowing how a project must be built. The IDEs are simply relying on the Gradle configuration instead of duplicating that information on their side, they have no influence on the build itself. – Paul Podgorsek Mar 27 '17 at 12:51
  • 1
    This is documented in the Gradle DSL reference for [EclipseClasspath](https://docs.gradle.org/current/dsl/org.gradle.plugins.ide.eclipse.model.EclipseClasspath.html). – Rahul Khimasia May 31 '17 at 21:48
  • 2
    And it is also documented in the Gradle DSL reference for [IdeaModule](https://docs.gradle.org/current/dsl/org.gradle.plugins.ide.idea.model.IdeaModule.html). – Rahul Khimasia Jul 21 '17 at 21:57
  • 1
    The issue is not that it is in the gradle build file. Ideally, it should look like `anyIDE { downloadJavadoc = true, downloadSources = true }`, in order to make it independent from the specific IDE used. – 3VYZkz7t Jul 17 '20 at 03:33
  • With the Kotlin DSL (Gradle 6.5.1), this fails with `Cannot access 'downloadJavadoc': it is private in 'IdeaModule'`. See fix here: https://stackoverflow.com/a/58028838/5666171 – CLOVIS Aug 02 '20 at 13:45
  • 11
    @PaulPodgorsek 3 years later... The issue is that the IDE should have the setting to determine whether to download javadoc or sources of dependent libs, not the project. It's a user preference whether they want to see javadoc and sources. – Planky Sep 14 '20 at 14:01
  • 2
    @Planky Actually, I changed my mind on this debate and tend to also see it like you now: Gradle is the reference for what has to be built and how, while the IDE is for user-interactions where showing JavaDoc and sources makes sense. – Paul Podgorsek Sep 15 '20 at 15:13
20

In addition to previous question, here is Gradle Kotlin DSL version:

plugins {
    id("idea")
}

idea {
    module {
        isDownloadJavadoc = true
        isDownloadSources = true
    }
}
Alexander Davliatov
  • 723
  • 2
  • 8
  • 13
6

You can write an ArtifactResolutionQuery that copies all SourcesArtifact and JavadocArtifact for each of your dependencies

See here for an example and here for the gradle source code which does it for the Eclipse/Intellij plugins

lance-java
  • 25,497
  • 4
  • 59
  • 101
  • looks like they refactored `IdeDependenciesExtractor` in the gradle sources. I fixed the link so that it points to `v4.1` sources rather than current – lance-java Apr 03 '18 at 08:34
1

If you are using Kotlin script (.kts) instead of Gradle script (.gradle):

plugins {
   idea
}

idea {
   module {
       isDownloadJavadoc = true
       isDownloadSources = true
   }
}
Domenico
  • 1,331
  • 18
  • 22
-4

Add the javadoc or sources classifies as dependencies:

dependencies {
    compile "commons-codec:commons-codec:1.10:sources"
    compile "commons-codec:commons-codec:1.10:javadoc"
}
lance-java
  • 25,497
  • 4
  • 59
  • 101
cmcginty
  • 113,384
  • 42
  • 163
  • 163
  • 64
    This will add the sources and javadoc jars to your application classpath. Surely not what you want. – eskatos Jul 16 '15 at 09:37
  • 1
    True, obviously you'll want to place them in the correct dependency configuration. The code was only an example as it was not specified how they were to be used. – cmcginty Jul 21 '15 at 19:48
  • 6
    Alternatively, `compile group: 'commons-codec', name: 'commons-codec', version: '1.10', classifier: 'sources'` See https://docs.gradle.org/current/userguide/dependency_management.html#sub:classifiers – Paul Feb 23 '17 at 20:33
  • 1
    Gradle Wrapper users, see this answer: https://stackoverflow.com/a/46596203/924597 – Shorn Oct 05 '17 at 23:34
  • What does Apache Commons have to do with Gradle? – IgorGanapolsky Dec 26 '18 at 21:35
  • 1
    @IgorGanapolsky the apache jar is used as a working example to go with the answer provided. – cmcginty Dec 28 '18 at 00:13
  • If your project has the [`compileOnly` configuration](https://blog.gradle.org/introducing-compile-only-dependencies), you can use that instead of `compile` as a hack to force a source download without including it in the build product. – David Moles Sep 25 '19 at 18:21
  • Note also that the trailing string after the last `:` is taken literally when looking for the JAR, so if you're not getting what you expect, check that -- e.g. you need to specify `src` instead of `sources` for [Velocity Tools 2.0](https://repo1.maven.org/maven2/org/apache/velocity/velocity-tools/2.0/). – David Moles Sep 25 '19 at 18:23
  • This did not work for me with testCompile . It only works with compile – djangofan Mar 24 '20 at 21:47