How do I force gradle to download dependency sources

Title says it all. How do I force gradle to download dependency sources

If you’re using eclipse or intellij plugins there’s support for doing this. Eg

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

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

Or you can use an ArtifactResolutionQuery to do it programmatically

task copyJavadocsAndSources {
    inputs.files configurations.runtime
    outputs.dir "${buildDir}/download"
    doLast {
        def componentIds = configurations.runtime.incoming.resolutionResult.allDependencies.collect { it.selected.id }
        ArtifactResolutionResult result = dependencies.createArtifactResolutionQuery()
            .forComponents(componentIds)
            .withArtifacts(JvmLibrary, SourcesArtifact, JavadocArtifact)
            .execute()
        def sourceArtifacts = []
        result.resolvedComponents.each { ComponentArtifactsResult component ->
            Set<ArtifactResult> sources = component.getArtifacts(SourcesArtifact)
            println "Found ${sources.size()} sources for ${component.id}"
            sources.each { ArtifactResult ar ->
                if (ar instanceof ResolvedArtifactResult) {
                    sourceArtifacts << ar.file
                }
            }
        }

        copy {            
            from sourceArtifacts
            into "${buildDir}/download"
        }
    }
} 

See https://stackoverflow.com/questions/39975780/how-can-i-use-gradle-to-download-dependencies-and-their-source-files-and-place-t/39981143#39981143

2 Likes

I tried both. It doesn’t work :frowning: . IntelliJ still decompiles it when I try to look at code of library.

I can only guess that you didn’t actually run the tasks after adding the snippets.

Eg gradlew eclipse or gradlew copyJavadocsAndSources

Did it now. Still the same thing.

I just realised you are using intellij. Try gradle cleanIdea idea

Task “cleanIdea” not found.

Did you

apply plugin: 'idea'

Yes. It doesn’t work.

Can you tell a bit more about the project. Is it a multi-module build? Where are you applying the idea plugin? Are you only doing it for the root project? or are you applying it for all the java projects?

It’s not multi module project. I’m applying it right after plugins have been declared e.g

plugins {
id ‘something’
// you know
}

apply plugin: ‘idea’