如何告诉 Checkstyle 检查换行符/换行符 LF 而不是 CRLF 的所有行?

How to tell Checkstyle to check all lines for linefeed / newline LF and not CRLF?

有没有办法在 checkstyle 中检查文件是否仅使用 LF 而没有使用 CRLF,反之亦然?

我找到的唯一支票是

NewlineAtEndOfFile

但是我只在文件末尾搜索更多内容?

Checkstyle 中还没有开箱即用的东西。如果你想禁止CRLF你可以使用下面的配置:

<module name="RegexpMultiline">
    <property name="format" value="\r\n"/>
    <property name="message" value="CRLF line endings are prohibited"/>
</module>

还有这个禁止 LF 的:

<module name="RegexpMultiline">
    <property name="format" value="(?<!\r)\n"/>
    <property name="message" value="LF line endings are prohibited"/>
</module>

只是为了补充 Michal Kordas 的好答案。

下面是一个稍微修改过的配置,它只会匹配第一个错误的换行符并且也禁止 Mac OS 行尾(CR):

<module name="RegexpMultiline">
    <property name="format" value="(?s:(\r\n|\r).*)"/>
    <property name="message" value="CRLF and CR line endings are prohibited, but this file uses them."/>
</module>

它已由 Michael Vorburger in this discussion 发布,因此对他表示敬意 - 我已将其发布在这里以确保完整性。