53

It appears that the eclipse plugin uses the following mechanism to do this:

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

eclipse {
    classpath {
       downloadSources=true
    }
}

but I can't find a corresponding option for the idea plugin. What am I missing?

Here's the build.gradle file:

apply plugin: 'groovy'
apply plugin: 'idea'

repositories {
    mavenCentral()
    mavenRepo name: "Grails", url: "http://repo.grails.org/grails/repo/"
}

dependencies {
    groovy 'org.codehaus.groovy:groovy-all:2.0.4'
    compile 'org.slf4j:slf4j-log4j12:1.6.6', 'postgresql:postgresql:9.1-901.jdbc4', 'net.sourceforge.nekohtml:nekohtml:1.9.16'
    ['core', 'hibernate', 'plugin-datasource', 'plugin-domain-class'].each { plugin ->
        compile "org.grails:grails-$plugin:2.1.0"
    }
}

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

// Fat Jar Option (http://docs.codehaus.org/display/GRADLE/Cookbook#Cookbook-Creatingafatjar)
jar {
    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
}

task wrapper(type: Wrapper) {
    gradleVersion = '1.0'
}
WXB13
  • 1,046
  • 3
  • 11
  • 17
  • Your question answered my question of how to do that in Eclipse. Thanks! – qwertzguy Oct 09 '15 at 12:36
  • Gradle wrapper users, see this answer: https://stackoverflow.com/a/46596203/924597 – Shorn Oct 05 '17 at 23:33
  • 5
    @Shorn No, https://stackoverflow.com/a/46596203/924597 only concerns the _Gradle_ sources, but not the sources of the project's compile dependencies. – jschreiner Nov 10 '17 at 15:21

4 Answers4

39

I've got problems with the following configuration:

idea {
    module {
        // if you hate browsing Javadoc
        downloadJavadoc = false

        // and love reading sources :)
        downloadSources = true
    }
}

repositories {
    mavenLocal()
    mavenCentral()
}

When removed mavenLocal() sources were downloaded and attached.

naXa stands with Ukraine
  • 35,493
  • 19
  • 190
  • 259
  • 6
    I had the problem with missing sources too and I found out, that if the artifact was already in the local maven repository, then gradle just left it as it was and did not try to load the sources. When I removed the artifacts from local maven repo, gradle re-downloaded them with sources (but did not place them in the local maven repo, but inside it's own cache). – Michal Dec 11 '15 at 11:54
  • 2
    Thank you, @Michal! Deleting `/Users/anton/.gradle/wrapper/dists/gradle-7.3.3-all`, running ./gradlew cleanIdea idea and reloading all gradle projects worked! Now I can browse the Gradle sources via IntelliJ. – Anton Petrov Mar 08 '23 at 08:15
24

For Kotlin DSL:

plugins {
    idea
}

idea {
    module {
        isDownloadJavadoc = true
        isDownloadSources = true
    }
}
radistao
  • 14,889
  • 11
  • 66
  • 92
  • According to the [Documentation of the plugin](https://docs.gradle.org/current/dsl/org.gradle.plugins.ide.idea.model.IdeaModule.html#org.gradle.plugins.ide.idea.model.IdeaModule:downloadSources), the properties do not use the `isXXX` prefix, so just `downloadJavadoc = true` will do. Also, `downloadSources` is `true` by default. – László van den Hoek May 17 '22 at 13:16
  • 1
    in 7.4.2: Script compilation errors: downloadJavadoc = true ^ Cannot access 'downloadJavadoc': it is private in 'IdeaModule' `downloadJavadoc` is private (https://github.com/gradle/gradle/blob/v7.4.2/subprojects/ide/src/main/java/org/gradle/plugins/ide/idea/model/IdeaModule.java#L172), `isDownloadJavadoc` a conventional Kotlin property name as i know (https://github.com/gradle/gradle/blob/v7.4.2/subprojects/ide/src/main/java/org/gradle/plugins/ide/idea/model/IdeaModule.java#L302-L315). – radistao May 17 '22 at 20:28
3

I noticed that when you already have a jar downloaded then its sources and not downloading after changing build.gradle.

Removing .m2 from my home directory and calling gradle helped.

Krzysztof Kot
  • 648
  • 1
  • 10
  • 15
3

If you need to keep mavenLocal() repository due to dependencies on snapshot jars (or some other reasons), you can get the source jar using maven.

mvn dependency:get -Dartifact=GROUP_ID:ARTIFACT_ID:VERSION:jar:sources

For example,

mvn dependency:get -Dartifact=org.springframework:spring-core:5.1.2.RELEASE:jar:sources
fahad.shaon
  • 39
  • 1
  • 2