我在我的mutliproject Gradle构建中遇到了一种奇怪的行为。有问题的项目是开源的,所以那些想要深入了解构建的人欢迎在分支开发上查看here。
在父项目中,我定义了fatJar
任务,如下所示:
task fatJar(type: Jar, dependsOn: subprojects.compileJava) {
manifest {
attributes 'Implementation-Title': 'Alchemist',
'Implementation-Version': rootProject.version,
'Main-Class': 'it.unibo.alchemist.Alchemist'
}
baseName = "${rootProject.name}-redist"
from(configurations.runtime.collect { it.isDirectory() ? it : zipTree(it) }) {
// remove all signature files
exclude "META-INF/*.SF"
exclude "META-INF/*.DSA"
exclude "META-INF/*.RSA"
exclude 'build'
exclude '.gradle'
exclude 'build.gradle'
exclude 'gradle'
exclude 'gradlew'
exclude 'gradlew.bat'
}
with jar
}
生成失败的一个子项目有点奇怪,因为它使用了ANTLR(我省略了依赖关系和其他我认为不需要理解手头问题的代码)。
apply plugin: 'antlr'
sourceSets {
antlr
}
generateGrammarSource {
arguments += ["-visitor", "-package", "it.unibo.alchemist.biochemistrydsl"]
def target = new File('src/antlr/java/')
if ((target.exists() && target.isDirectory()) || target.mkdirs()) {
outputDirectory = target
} else {
throw new IllegalStateException("$target is not an existing directory and could not be created as such.")
}
}
compileAntlrJava.dependsOn('generateGrammarSource')
compileJava.dependsOn('compileAntlrJava')
现在,当从父项目中运行./gradlew clean && ./gradlew fatJar
时,我遇到了一个失败,出现以下异常(为简洁起见省略了一些部分):
> Failed to create MD5 hash for file '[my project folder]/alchemist/alchemist-incarnation-biochemistry/build/classes/java/antlr' as it does not exist.
* Exception is:
org.gradle.api.UncheckedIOException: Failed to capture snapshot of input files for task ':fatJar' property 'rootSpec$1$1' during up-to-date check.
at org.gradle.api.internal.changedetection.state.CacheBackedTaskHistoryRepository.snapshotTaskFiles(CacheBackedTaskHistoryRepository.java:331)
<snip>
Caused by: org.gradle.api.UncheckedIOException: Failed to create MD5 hash for file '[my project folder]/alchemist/alchemist-incarnation-biochemistry/build/classes/java/antlr' as it does not exist.
at org.gradle.internal.hash.DefaultFileHasher.hash(DefaultFileHasher.java:45)
<snip>
Caused by: java.io.FileNotFoundException: [my project folder]/alchemist/alchemist-incarnation-biochemistry/build/classes/java/antlr (Is a directory)
at org.gradle.internal.hash.DefaultFileHasher.hash(DefaultFileHasher.java:38)
系统抱怨的文件实际上是文件夹,实际上是一个文件夹,但它确实应该是。
现在,让我感到困惑的是,如果第二次调用./gradlew fatJar
,它就会成功完成。
这在不同的系统上是可重现的,改变了Gradle版本(保留在版本4.x)和操作系统(Arch Linux和MacOS X)。