2

I would like to use Gradle to download dependencies and their source files and place them all in one directory. I found this answer below that tells me how to do it for the dependencies themselves, but I would like to also get the source files. How do I do that?

I know that the Eclipse plugin can grab source files, but I don't know where it places them.

How can I use Gradle to just download JARs?

Community
  • 1
  • 1
abrahamwl
  • 35
  • 1
  • 3

2 Answers2

6

This works

apply plugin: 'java'

repositories { ... }

dependencies {
    compile 'foo:bar:1.0'
    runtime 'foo:baz:1.0'
}

task download {
    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)
            .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 configurations.runtime
            from sourceArtifacts
            into "${buildDir}/download"
        }
    }
}
lance-java
  • 25,497
  • 4
  • 59
  • 101
  • Thanks for your second attempt. build.gradle compile fails on "is". If I change "is" to "instanceof" it compiles and runs, but only grabs the class file jars. I tried it both with the configuration you used before and without. – abrahamwl Oct 12 '16 at 06:45
  • Updated to use `.withArtifacts(JvmLibrary, SourcesArtifact)` (inspiration taken from [IdeDependenciesExtractor](https://github.com/gradle/gradle/blob/master/subprojects/ide/src/main/java/org/gradle/plugins/ide/internal/IdeDependenciesExtractor.java)) – lance-java Oct 12 '16 at 08:55
  • This should be the accepted answer. Works like a charm :) – Ajay Oct 09 '17 at 19:21
  • is there equivalent to: mvn dependency:sources ? – razor Apr 12 '19 at 09:00
  • You can use dependency export plugin https://github.com/uklance/gradle-dependency-export – lance-java Apr 12 '19 at 19:47
  • Gradle 7.5 logs: ```unknown property 'runtime' for configuration container of type org.gradle.api.internal.artifacts.configurations.DefaultConfigurationContainer``` - Similar error for `runtimeClasspath`. – Top-Master Apr 01 '23 at 10:44
2
apply plugin: 'java'

configurations {
    runtimeSources
}

dependencies {
    compile 'foo:bar:1.0'
    runtime 'foo:baz:1.0'

    configurations.runtime.resolvedConfiguration.resolvedArtifacts.each { ResolvedArtifact ra ->
        ModuleVersionIdentifier id = ra.moduleVersion.id
        runtimeSources "${id.group}:${id.name}:${id.version}:sources"
    }
}

task download(type: Copy) {
    from configurations.runtime
    from configurations.runtimeSources
    into "${buildDir}/download"
}
lance-java
  • 25,497
  • 4
  • 59
  • 101
  • Thanks for this great answer. However, it seems to fail if it can't find source for any of the dependencies and doesn't end up copying ANY files to the desired directory. – abrahamwl Oct 11 '16 at 14:53
  • 1
    It seems like it only checks the first repository listed, and if it doesn't find the source there, it just gives up without checking other repositories. – abrahamwl Oct 11 '16 at 15:00
  • I think gradle will keep checking repositories until it finds a descriptor (eg pom or ivy) for the GAV (group, artifact, version). You could try declaring the sources repository first, before the one that doesn't contain sources. – lance-java Oct 11 '16 at 15:13
  • There must be some dependencies that don't have a source jar in any repository. Is there a way to cause it to just ignore situations where it can't find the source jar at all, instead of failing? – abrahamwl Oct 12 '16 at 06:53
  • Although I should note that when it fails, it only reports that it could not find a source jar in the first repository listed, so I am not sure that, when it's looking for source, based on your configuration, that it does use all the repositories. In any case, getting it to not fail if it can't find the source jar somehow would be a big step forward. – abrahamwl Oct 12 '16 at 06:56
  • Here's the error message: `* What went wrong: Could not resolve all dependencies for configuration ':runtimeSources'. > Could not find annotations-sources.jar (com.google.code.findbugs:annotations:2.0.1). Searched in the following locations: https://repo1.maven.org/maven2/com/google/code/findbugs/annotations/2.0.1/annotations-2.0.1-sources.jar` – abrahamwl Oct 12 '16 at 07:03
  • Please see my other answer which downloads sources if they exist but doesn't fail for missing sources – lance-java Oct 12 '16 at 09:43
  • sry, but it doesnt work, see output: * What went wrong: A problem occurred evaluating root project 'train-server-mvb-composite'. > Cannot change strategy of configuration ':runtime' after it has been resolved. – user1722245 Sep 12 '17 at 06:01
  • is there equivalent to: mvn dependency:sources ? – razor Apr 12 '19 at 09:00