如何使用 Gradle 下载依赖项及其源文件并将它们全部放在一个目录中?

abr*_*mwl 1 java jar gradle

我想使用 Gradle 下载依赖项及其源文件,并将它们全部放在一个目录中。我在下面找到了这个答案,它告诉我如何为依赖项本身做这件事,但我也想获取源文件。我怎么做?

我知道 Eclipse 插件可以抓取源文件,但我不知道它把它们放在哪里。

如何使用 Gradle 只下载 JAR?

lan*_*ava 5

这有效

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"
        }
    }
}
Run Code Online (Sandbox Code Playgroud)