Gradle 使用工件名称解压渐变依赖项

Gradle 使用工件名称解压渐变依赖项,gradle,dependencies,unzip,Gradle,Dependencies,Unzip,我正在寻找一种方法,将特定配置的项目依赖项提取到工作区文件夹中。由于可能存在多个依赖项,我希望将每个工件提取到一个带有工件名称的文件夹中。我试图在python的上下文中解决这个问题,但这个问题实际上与python无关 当前我的gradle文件如下所示: configurations { python } dependencies { python group: 'github.dpeger', name: 'py-utils', version: '1.6', ext: 'zip' p

我正在寻找一种方法,将特定配置的项目依赖项提取到工作区文件夹中。由于可能存在多个依赖项,我希望将每个工件提取到一个带有工件名称的文件夹中。我试图在python的上下文中解决这个问题,但这个问题实际上与python无关

当前我的gradle文件如下所示:

configurations { python }

dependencies {
  python group: 'github.dpeger', name: 'py-utils', version: '1.6', ext: 'zip'
  python group: 'github.dpeger', name: 'py-test', version: '1.6', ext: 'zip'
}

task cleanPythonDependencies(type: Delete) { delete 'lib/python' }
tasks.clean.dependsOn cleanPythonDependencies

task importPythonDependencies(type: Copy) {
  dependsOn cleanPythonDependencies
  from {
    configurations.python.collect { zipTree(it) }
  }
  into 'lib/python'
}
但是,这会将
python
配置中的所有依赖项提取到文件夹
lib\pyhton
中,而不使用工件的名称


我想要的是
py-utils
被提取到
lib\pyhton\py-utils
py-test
lib\pyhton\py-test
假设您想要py-utils被提取到lib\pyhton\py-utils,py-test到lib\pyhton\py-test,这应该可以完成以下工作:

task importPythonDependencies() {
  dependsOn cleanPythonDependencies

  String collectDir = 'lib/python'
  outputs.dir collectDir

  doLast {
    configurations.python.resolvedConfiguration.resolvedArtifacts.each { artifact ->
      copy {
        from zipTree( artifact.getFile() )
        into collectDir + '/' + artifact.name
      }
    }
  }
}

很好,谢谢。不过,我稍微修改了您的答案(请参见问题的编辑),因为在您的任务中,工件将在gradle的配置阶段复制到lib文件夹中。这很可能会导致清理出现问题,因为依赖任务
cleanPythonDependencies
将在配置阶段之后执行,因此会再次删除所有提取的工件……很高兴它有所帮助。我修改了答案以反映您的评论。当我尝试时,我没有考虑清理。尽管如此,它不应该像您指出的那样是配置阶段的一部分。