gpt4 book ai didi

Gradle 5.0 在 buid.gradle.kts 中使用 Kotlin DSL 时导致错误 Val cannot be reassigned

转载 作者:行者123 更新时间:2023-12-04 05:04:21 27 4
gpt4 key购买 nike

这个错误确实 不是 发生在 gradle 版本 4.10.2

classDirectories = files(filesToCover)导致 gradle 抛出错误:

* What went wrong:
Script compilation error:

Line 80: classDirectories = files(filesToCover)
^ Val cannot be reassigned

但是当你查看底层 Java 类时 JacocoReportBase您可以清楚地看到有一个 setter 并且类属性不是最终的。
public abstract class JacocoReportBase extends JacocoBase {
private FileCollection executionData;
private FileCollection sourceDirectories;
private FileCollection classDirectories;
private FileCollection additionalClassDirs;
private FileCollection additionalSourceDirs;

...

public void setClassDirectories(FileCollection classDirectories) {
this.classDirectories = classDirectories;
}

build.gradle.kts
tasks {
withType<KotlinCompile<KotlinJvmOptions>> {
kotlinOptions.freeCompilerArgs = listOf("-Xjsr305=strict")
kotlinOptions.jvmTarget = "1.8"
}

withType<JacocoReport> {
reports {
xml.isEnabled = false
csv.isEnabled = false
html.destination = file("$buildDir/jacocoHtml")
}

afterEvaluate {
val filesToAvoidForCoverage = listOf(
"/dto",
"/config",
"AuthenticationMicroServiceKt.class"
)
val filesToCover = mutableListOf<String>()
File("build/classes/kotlin/main/app/example/core/")
.walkTopDown()
.mapNotNull { file ->
var match = false
filesToAvoidForCoverage.forEach {
if (file.absolutePath.contains(it)) {
match = true
}
}
return@mapNotNull if (!match) {
file.absolutePath
} else {
null
}
}
.filter { it.contains(".class") }
.toCollection(filesToCover)

classDirectories = files(filesToCover) // error
}
}
}

我还在 Github here 上发布了一个问题

最佳答案

来看看the source code :

/**
* Source sets that coverage should be reported for.
*/
@PathSensitive(PathSensitivity.RELATIVE)
@InputFiles
public ConfigurableFileCollection getClassDirectories() {
return classDirectories;
}

/**
* Classes that coverage should be reported for.
* @deprecated Use {@code getClassDirectories().setFrom(...)}
*/
@Deprecated
public void setClassDirectories(FileCollection classDirectories) {
DeprecationLogger.nagUserOfDiscontinuedMethod("JacocoReportBase.setClassDirectories(FileCollection)", "Use getClassDirectories().from(...)");
this.classDirectories.setFrom(classDirectories);
}

如您所见, getClassDirectories返回类型为 ConfigurableFileCollectionsetClassDirectories接受 FileCollection .所以这不是一个有效的 Java Beans 属性,因为 getter 和 setter 对不同的类型进行操作。所以 Kotlin 只能看到 getter,实际上它是 val .

您可以尝试调用 setClassDirectories直接( setClassDirectories(…) 而不是 classDirectories = … ),或使用 classDirectories.setFrom ,正如 Javadocs 所建议的那样。

它在 4.10.2 中工作,因为它 was a property at that time (getter 和 setter 类型相同)。 this commit 中的情况发生了变化.

关于Gradle 5.0 在 buid.gradle.kts 中使用 Kotlin DSL 时导致错误 Val cannot be reassigned,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53520246/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com