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

如何让Spotless代码格式化工具忽略指定代码段?

配置Spotless忽略特定代码段的方法

Spotless支持类似Prettier的注释忽略功能,具体操作分以下几种情况:

通用忽略方式(适用于多数格式化插件)

在需要跳过格式化的代码块前后添加注释标记:

  • 开头:// spotless:off
  • 结尾:// spotless:on

针对你的Java代码,修改后如下:

// spotless:off
String content = Joiner.on(System.lineSeparator()).join(
    "services:", 
    "- name: name", 
    "  image: artifacts-dev/image"
);
// spotless:on

针对Java格式化插件的额外方式

如果你的Spotless使用了google-java-format或palantir-java-format,还可以用@formatter:off和@formatter:on注释,但需要先在配置中启用该功能:

Gradle配置示例

spotless {
    java {
        googleJavaFormat() // 或者palantirJavaFormat()
        formatAnnotations() // 开启@formatter注释支持
    }
}

代码中使用示例

// @formatter:off
String content = Joiner.on(System.lineSeparator()).join(
    "services:", 
    "- name: name", 
    "  image: artifacts-dev/image"
);
// @formatter:on

单行忽略

如果只需要忽略某一行代码,可以在该行末尾添加// spotless:ignore(部分Java格式化插件支持):

String content = Joiner.on(System.lineSeparator()).join( // spotless:ignore
    "services:", 
    "- name: name", 
    "  image: artifacts-dev/image"
);

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.30 04:53:17