繁体   English   中英

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

[英]How can I use Gradle to download dependencies and their source files and place them all in one directory?

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

我知道Eclipse插件可以抓取源文件,但是我不知道它在何处放置它们。

如何使用Gradle下载JAR?

这有效

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM