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

Java 如何使用Gradle下载依赖项及其源文件并将它们全部放在一个目录中?,java,gradle,jar,Java,Gradle,Jar,我想使用Gradle下载依赖项及其源文件,并将它们全部放在一个目录中。我在下面找到了这个答案,它告诉我如何处理依赖项本身,但我也想获得源文件。我该怎么做 我知道Eclipse插件可以抓取源文件,但我不知道它把它们放在哪里 这很有效 apply plugin: 'java' repositories { ... } dependencies { compile 'foo:bar:1.0' runtime 'foo:baz:1.0' } task download {

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

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

这很有效

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"
        }
    }
}
apply插件:“java”
存储库{…}
依赖关系{
编译“foo:bar:1.0”
运行时“foo:baz:1.0”
}
任务下载{
inputs.files configurations.runtime
outputs.dir“${buildDir}/下载”
多拉斯特{
def componentIds=configurations.runtime.incoming.resolutionResult.allDependencies.collect{it.selected.id}
ArtifactResolutionResult=依赖项。createArtifactResolutionQuery()
.forComponents(组件ID)
.withArtifacts(JvmLibrary,SourcesArtifact)
.execute()
def sourceArtifacts=[]
result.resolvedComponents.each{ComponentArtifactsResult component->
Set sources=component.getArtifacts(SourcesArtifact)
println“找到${component.id}的${sources.size()}源”
sources.each{ArtifactResult ar->
if(解析数据的ar实例IFactResult){
sourceArtifacts这很有效

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"
        }
    }
}
apply插件:“java”
存储库{…}
依赖关系{
编译“foo:bar:1.0”
运行时“foo:baz:1.0”
}
任务下载{
inputs.files configurations.runtime
outputs.dir“${buildDir}/下载”
多拉斯特{
def componentIds=configurations.runtime.incoming.resolutionResult.allDependencies.collect{it.selected.id}
ArtifactResolutionResult=依赖项。createArtifactResolutionQuery()
.forComponents(组件ID)
.withArtifacts(JvmLibrary,SourcesArtifact)
.execute()
def sourceArtifacts=[]
result.resolvedComponents.each{ComponentArtifactsResult component->
Set sources=component.getArtifacts(SourcesArtifact)
println“找到${component.id}的${sources.size()}源”
sources.each{ArtifactResult ar->
if(解析数据的ar实例IFactResult){


sourceArtifacts感谢您的回答。但是,如果它找不到任何依赖项的源代码,并且没有将任何文件复制到所需目录,那么它似乎会失败。它似乎只检查列出的第一个存储库,如果在那里找不到源代码,它只会放弃而不检查其他存储库。我认为gradle将继续检查存储库,直到找到GAV(组、工件、版本)的描述符(如pom或ivy)。您可以先尝试声明源代码存储库,然后再声明不包含源代码的存储库。必须存在某些依赖项,这些依赖项在任何存储库中都没有源jar。有没有办法让它忽略根本找不到源jar的情况,而不是失败?尽管我应该注意,当它失败时,它只会我发现它在列出的第一个存储库中找不到源jar,所以我不确定,根据您的配置,当它查找源时,它是否使用了所有的存储库。无论如何,如果它找不到源jar,让它不失败将是向前迈出的一大步。感谢这个伟大的答案。但是,它似乎是如果它找不到任何依赖项的源,并且没有将任何文件复制到所需的目录,则会失败。它似乎只检查列出的第一个存储库,如果在那里找不到源,它只会放弃,而不检查其他存储库。我想gradle会继续检查存储库,直到找到描述符为止(例如pom或常春藤)用于GAV(组、工件、版本)。您可以先尝试声明源代码存储库,然后再声明不包含源代码的存储库。必须存在某些依赖项,这些依赖项在任何存储库中都没有源jar。有没有办法让它忽略根本找不到源jar的情况,而不是失败?尽管我应该注意,当它失败时,它只会注意到它在列出的第一个存储库中找不到源jar,所以我不确定它在根据您的配置查找源代码时是否使用了所有存储库。无论如何,如果它找不到源jar,让它不会失败将是一个很大的进步。感谢您的第二次尝试。build.gradle compilee在“is”上失败。如果我将“is”更改为“instanceof”,它将编译并运行,但只获取类文件JAR。我尝试了您以前使用的配置和未使用的配置。更新为使用
。withArtifacts(JvmLibrary,SourcesArtifact)
(灵感来源)这应该是公认的答案。效果很好:)是否有等效于:mvn dependency:sources?感谢您的第二次尝试,您可以使用dependency export插件。build.gradle编译在“is”上失败。如果我将“is”更改为“instanceof”它可以编译和运行,但只抓取类文件JAR。我尝试了您之前使用的配置和未使用的配置。更新为使用
。withArtifacts(JvmLibrary,SourcesArtifact)
(灵感来源于)这应该是公认的答案。就像一个咒语:)有等效于:mvn dependency:sources吗?你可以使用dependency export插件