【问题标题】:File does not end with a newline [NewlineAtEndOfFile]文件不以换行符结尾 [NewlineAtEndOfFile]
【发布时间】:2017-10-02 09:42:29
【问题描述】:

当我尝试在我的项目上使用 mvn clean install 时,我遇到了这些错误:

[ERROR] C:\Users\lob\Downloads\zanata-platform-platform-4.0.0\zanata-platform-platform-4.0.0\parent\eclipse-code-formatter-js.xml:0: File does not end with a newline. [NewlineAtEndOfFile]
[ERROR] C:\Users\lob\Downloads\zanata-platform-platform-4.0.0\zanata-platform-platform-4.0.0\parent\eclipse-code-formatter.xml:0: File does not end with a newline. [NewlineAtEndOfFile]
[ERROR] C:\Users\lob\Downloads\zanata-platform-platform-4.0.0\zanata-platform-platform-4.0.0\parent\pom.xml:0: File does not end with a newline. [NewlineAtEndOfFile]
[ERROR] C:\Users\lob\Downloads\zanata-platform-platform-4.0.0\zanata-platform-platform-4.0.0\parent\README.txt:0: File does not end with a newline. [NewlineAtEndOfFile]
Audit done.
[INFO] There are 4 errors reported by Checkstyle 7.2 with zanata-build-tools/checkstyle.xml ruleset.
[ERROR] eclipse-code-formatter-js.xml:[0] (misc) NewlineAtEndOfFile: File does not end with a newline.
[ERROR] eclipse-code-formatter.xml:[0] (misc) NewlineAtEndOfFile: File does not end with a newline.
[ERROR] pom.xml:[0] (misc) NewlineAtEndOfFile: File does not end with a newline.
[ERROR] README.txt:[0] (misc) NewlineAtEndOfFile: File does not end with a newline.

将段落或 \n 放在这些文件的末尾并不能解决此问题。 在Window >> Preferences >> Checkstyle >> Select the configuration file >> 下的Eclipse 中没有列出这些文件。

使用:

  • Windows 10
  • Java 8
  • Eclipse Neon.1 (4.6.1)
  • Maven 3.0.4

【问题讨论】:

  • 编辑:这是一个 Linux 项目

标签: java eclipse file newline checkstyle


【解决方案1】:

tl;博士:

  1. 运行git config core.autocrlf true
  2. 删除除.git以外的所有文件和文件夹
  3. 运行git reset --hard HEAD

细节和根本原因:

  1. Git 行尾配置

    Windows 上的 Git 配置是此错误的根本原因。 zanata-platform-platform 是用 Unix 风格的行结尾构建的。在 Windows 中,maven 会将 Unix 风格的行结尾 LF 误解为不正确的 Windows 行结尾 CRLF

    第一步是重新配置安装在 Windows 上的 git,以便在 git 生成文件时将 LF 替换为 CRLF。在 Windows 上,您使用 CRLF 编辑文件,但此配置告诉 git 在您提交或推送时将 CRLF 转换为 LF

  2. 删除所有文件,同时保留包含历史记录的.git目录

  3. 让 git 新生成带有 Windows 样式行结尾的文件。

【讨论】:

  • 是这样,但我认为它更有可能是 checkstyle 错误。我使用的所有 Windows 应用程序都知道 LF 结尾并正确处理它们,因此我将 autocrlf 切换为 false。现在 checkstyle 行为...顺便说一句,它可以使用 NewlineAtEndOfFile 模块属性进行配置:<property name="lineSeparator" value="lf"/>,但我更喜欢自动识别。
【解决方案2】:

原因是一个名为 NewlineAtEndOfFilecheckstyle 规则,如果项目的任何文件在其末尾没有新行,则会阻止成功构建。

规则说:

基本原理:任何源文件和文本文件通常都应以 一个行分隔符,让其他人在末尾轻松添加新内容 文件和“diff”命令不会显示之前的行已更改。

所以必须要做的,就是在上面日志中提到的每个文件的末尾添加一个新行:

。 eclipse-code-formatter-js.xml

。 eclipse-code-formatter.xml

。 pom.xml

。 README.txt

有关此规则的更多信息:checkstyle-NewlineAtEndOfFile-rule

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-09-07
    • 2017-03-01
    • 2011-03-21
    • 2010-11-03
    • 2010-10-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多