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

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

本文介绍了我如何使用Gradle下载依赖项及其源文件并将它们全部放在一个目录中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

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

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

推荐答案

此作品

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"
        }
    }
}

这篇关于我如何使用Gradle下载依赖项及其源文件并将它们全部放在一个目录中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 07:49