File does not end with a newline [NewlineAtEndOfFile]

11,025

Solution 1

tl;dr:

  1. run git config core.autocrlf true
  2. remove all files and folders other than .git
  3. run git reset --hard HEAD

detail and root cause:

  1. Git line ending config

    Git configuration on Windows is the root cause of this error. zanata-platform-platform which is built with Unix style line ending. In windows, maven will misunderstands Unix style line ending LF as improper Windows line ending CRLF.

    First step is to re-configure git installed on Windows so that when git generate file replacing LF to CRLF. On Windows, you edit files with CRLF but this config tells git to convert CRLF to LF when you commit or push.

  2. Delete all files while keeping .git directory which contains history

  3. Let git newly generate files with Windows style line ending.

Solution 2

The cause is a checkstyle rule called NewlineAtEndOfFile which prevents a successful build if any of the project's files does not have a new line at its end.

The rule says:

Rationale: Any source files and text files in general should end with a line separator to let other easily add new content at the end of file and "diff" command does not show previous lines as changed.

So what must be done, is simply to add a new line at the end of each of the files mentioned in the above logs:

. eclipse-code-formatter-js.xml

. eclipse-code-formatter.xml

. pom.xml

. README.txt

For more infos on this rule: checkstyle-NewlineAtEndOfFile-rule

Share:
11,025
lob
Author by

lob

Updated on June 04, 2022

Comments

  • lob
    lob almost 2 years

    When trying to use mvn clean install on my project I'm getting those errors:

    [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.
    

    Putting paragraphs or \n at the end of those files does not solve this issue. In Eclipse under Window >> Preferences >> Checkstyle >> Select the configuration file >> those files aren't listed.

    Using:

    • Windows 10
    • Java 8
    • Eclipse Neon.1 (4.6.1)
    • Maven 3.0.4
  • Alexander Radchenko
    Alexander Radchenko about 4 years
    Thats the case, but I think it's more likely to be the checkstyle bug. All Windows applications I use know about LF endings and treat them correctly, so I switched autocrlf to false. Now that checkstyle behavior... BTW it is configurable with NewlineAtEndOfFile module property: <property name="lineSeparator" value="lf"/>, but I would prefer automatic recognition.