You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何消除Gradle compileJava任务的预览特性警告?

如何消除Java预览特性的编译警告?

你当前的Gradle Kotlin DSL配置如下:

tasks {
    val preview = "--enable-preview"

    withType<JavaCompile> {
        options.compilerArgs.add(preview)
        options.compilerArgs.add("-Xlint:preview")
    }

    withType<Test> {
        useJUnitPlatform()
        jvmArgs = jvmArgs!! + preview
    }

    withType<JavaExec> {
        jvmArgs = jvmArgs!! + preview
    }
}

编译时收到警告:

> Task :compileJava
warning: [preview] string templates are a preview feature and may be removed in a future release.

解决方案

你添加的-Xlint:preview参数是主动开启预览特性相关的lint警告,这正是你看到警告的原因。要消除该警告,只需将这个参数改为-Xlint:-preview(通过减号关闭预览特性的lint检查),或者直接移除该参数。

修改后的JavaCompile配置示例:

withType<JavaCompile> {
    options.compilerArgs.add(preview)
    // 关闭预览特性的lint警告
    options.compilerArgs.add("-Xlint:-preview")
}

如果不需要额外的lint检查,也可以直接移除-Xlint相关参数,仅保留--enable-preview即可正常启用预览特性且不显示警告。

内容的提问来源于stack exchange,提问作者pixel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 08:52:04