Unpacking Dependency Sources

I am using ctags with vim to be able to jump around source files. One good usage of that (usually during debugging) is to be able to jump to a dependency source.

With maven, I could do something like:

mvn dependency:unpack-dependencies

to extract all the dependency sources. After which I can generate ctags (‘ctags -R’) and have all the sources available for the exploration

How do I do the same thing with Gradle => extract/unpack dependency sources?

Thank you!

Hi there,

There’s no very nice way to do this right now out of the box. Below is a Gradle build script that does this though that you can use for inspiration. I have only tested it on Gradle 1.0-milestone-9.

At some point in the future we will package this up in a nicer way as a plugin, but if you need this in the meantime you can use the code below.

apply plugin: 'java'
  repositories {
  dependencies {
    compile "junit:junit:4.9"
  task wrapper(type: Wrapper) {
    gradleVersion "1.0-milestone-9"
  import org.gradle.api.internal.artifacts.dependencies.DefaultExternalModuleDependency
ext.sourcesFor = { configuration ->
    def addChildren
    addChildren = { Collection deps, Set allDeps = new LinkedHashSet() ->
        deps.each { ResolvedDependency resolvedDependency ->
            def notSeenBefore = allDeps.add(resolvedDependency)
            if (notSeenBefore) { // defend against circular dependencies
                addChildren(resolvedDependency.children, allDeps)
      def dependencies = new LinkedHashSet()
    addChildren(configuration.resolvedConfiguration.getFirstLevelModuleDependencies({ it instanceof ExternalDependency } as Spec), dependencies)
      def sourceDependencies = dependencies.collect { ResolvedDependency resolvedDependency ->
        def dependency = new DefaultExternalModuleDependency(
            resolvedDependency.moduleGroup, resolvedDependency.moduleName,
             resolvedDependency.moduleVersion, resolvedDependency.configuration
        dependency.transitive = false
        dependency.artifact { artifact ->
            artifact.name = dependency.name
            artifact.type = 'source'
            artifact.extension = 'jar'
            artifact.classifier = 'sources'
      project.configurations.detachedConfiguration(sourceDependencies as Dependency[])
  task configureExtractSources {
    doLast {
        sourcesFor(configurations.compile).each { File sourcesJar ->
            extractSources.from(zipTree(sourcesJar)) {
                into sourcesJar.name - "-sources.jar"
  task extractSources(type: Copy, dependsOn: configureExtractSources) {
    into "$buildDir/sources"

Thanks Luke. I’ll try that.

Sorry to resurrect an old thread, but does anyone know if there’s now a plugin with similar functionality?

1 Like