这无疑是一个摇摇欲坠的新手问题,但它的确击败了我。我的源代码树如下所示:
|-- master
buildSrc
build.gradle
|-- comp1
!-- filea
!-- fileb
|-- comp2
!-- file1
!-- file2
!-- etc
我正在
build.gradle
目录中运行master
,该目录使用自定义任务(在prepBundle中调用(未显示))来生成需要在zip文件中的文件列表。列表中包含各种外部处理,因此无法使用任何基本的包含或闭包来生成列表。这就是我所拥有的:
task showFilelist(dependsOn:prepBundle){
prepBundle.outputs.files.each{
println "including file: ${it}"
}
}
task createBundle(type:Zip,dependsOn:prepBundle){
inputs.files(prepBundle.outputs.files)
outputs.file(archiveName)
baseName = "somebundle"
from ".."
include prepBundle.outputs.files
}
如果我只运行showFilelist,则可以获取要压缩的文件的正确解析路径:
<snip>
:showFilelist
including file: C:\fullpath\master\build.gradle
including file: C:\fullpath\comp1\filea
including file: C:\fullpath\comp2\file1
但是当我执行捆绑任务时,它会爆炸:
:createBundle
FAILURE: Build aborted because of an internal error.
* What went wrong:
Build aborted because of an unexpected internal error. Please file an issue at: http://forums.gradle.org.
* Try:
Run with --debug option to get additional debug info.
* Exception is:
org.gradle.api.UncheckedIOException: java.io.IOException: The process cannot access the file because another process has locked a portion of the file
at org.gradle.util.hash.HashUtil.createHash(HashUtil.java:56)
at org.gradle.util.hash.HashUtil.createHash(HashUtil.java:34)
at org.gradle.api.internal.changedetection.state.DefaultHasher.hash(DefaultHasher.java:24)
at org.gradle.api.internal.changedetection.state.CachingHasher.hash(CachingHasher.java:45)
at org.gradle.api.internal.changedetection.state.DefaultFileSnapshotter$1.run(DefaultFileSnapshotter.j
ava:48)
at org.gradle.internal.Factories$1.create(Factories.java:22)
at org.gradle.cache.internal.DefaultCacheAccess.useCache(DefaultCacheAccess.java:143)
at org.gradle.cache.internal.DefaultCacheAccess.useCache(DefaultCacheAccess.java:131)
at org.gradle.cache.internal.DefaultPersistentDirectoryStore.useCache(DefaultPersistentDirectoryStore.
在prepBundle中,我迭代文件列表,并且其他进程没有使用任何文件。我不认为这是一个难题,我认为zip任务的配置是我做错了。如果我稍微改变
createBundle
来删除输入和输出,它看起来像这样:task createBundle(type:Zip,dependsOn:prepBundle){
baseName = "somebundle"
include prepBundle.outputs.files
}
但是然后什么也不做:
:createBundle UP-TO-DATE
BUILD SUCCESSFUL
当我只有一个文件列表并且这些文件的父根目录不在我的当前项目下时,如何将这个zip放在一起?
谢谢你的帮助!
最佳答案
我不确定背后的具体问题是什么,但我认为这与
inputs.files(prepBundle.outputs.files)
和/或(这是多余的,可能会破坏东西)
outputs.file(archiveName)
我想,您想将
prepBundle
中的所有文件添加到存档中。当您使用
from
任务的继承的Copy
任务的Zip
属性时,可以轻松实现这一点。使用
from
属性会将您的任务更改为task createBundle(type:Zip,dependsOn:prepBundle){
prepBundle.outputs.files.each {
from it.getPath() // adds every single file to the archive
}
baseName = "somebundle"
from ".." // I assume that you add another path here not ".."
}
如您所见,我也跳过了
include
部分和output.file(..)
,因为当将include
与特定文件一起使用时,from
属性是多余的。 output.file(..)
是多余的,因为Zip
任务已经定义了它。