Java 如何摆脱 Checkstyle 消息“文件不以换行符结尾”。

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/997021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 22:03:02  来源:igfitidea点击:

How to get rid of Checkstyle message 'File does not end with a newline.'

javawindowsmaven-2netbeanscheckstyle

提问by Michael Pralow

I'm working with a Maven (jar) Project in Netbeans (Windows), which creates Checkstyle reports with the Maven Checkstyle plugin.

我正在使用 Netbeans (Windows) 中的 Maven (jar) 项目,该项目使用 Maven Checkstyle 插件创建 Checkstyle 报告。

No matter what I do, I always get the message: File does not end with a newlinefor Java class files.

无论我做什么,我总是得到消息:File does not end with a newline对于 Java 类文件。

What can I do/configure in either Netbeans or Checkstyle to get rid of the message ?

我可以在 Netbeans 或 Checkstyle 中执行/配置什么来消除该消息?

Versions of used software:

使用过的软件版本:

  • WinXP SP3
  • Netbeans 6.7 RC1 (happens with 6.5 too)
  • Maven 2.0.9
  • Maven Checkstyle Plugin 2.2
  • Java 1.6 (Update 14)
  • WinXP SP3
  • Netbeans 6.7 RC1(也适用于 6.5)
  • Maven 2.0.9
  • Maven Checkstyle 插件 2.2
  • Java 1.6(更新 14)

采纳答案by Jon Strayer

Put a newline at the end of the file
or
configure CheckStyle not to care.

在文件末尾放置一个换行符

配置 CheckStyle 不关心。

<module name="Checker">
    <!-- stuff deleted -->
    <module name="NewlineAtEndOfFile">
        <property name="severity" value="ignore" />
    </module>


You also have to tell the Maven Checkstyle plugin to use your checkstyle config file.

您还必须告诉 Maven Checkstyle 插件使用您的 checkstyle 配置文件。

<plugin>
    <artifactId>maven-checkstyle-plugin</artifactId>
    <configuration>
        <configLocation>${basedir}/yourCheckstyle.xml</configLocation>
    </configuration>
</plugin>

回答by Stuart Jones

There's a simpler way to do this. You can specify a suppressions file without overriding the sun checkstyle config file:

有一种更简单的方法可以做到这一点。您可以在不覆盖 sun checkstyle 配置文件的情况下指定抑制文件:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-checkstyle-plugin</artifactId>
    <configuration>
        <suppressionsLocation>suppressions.xml</suppressionsLocation>
        <configLocation>config/sun_checks.xml</configLocation>
    </configuration>
</plugin>

Where your suppressions.xml is:

你的pressions.xml 在哪里:

<?xml version="1.0"?>
<!DOCTYPE suppressions PUBLIC
    "-//Puppy Crawl//DTD Suppressions 1.1//EN"
    "http://www.puppycrawl.com/dtds/suppressions_1_1.dtd">

<suppressions>
    <suppress checks="NewlineAtEndOfFile" files=".java" />
</suppressions>

回答by Raghu

Eclipse ..Window >> Preferences >> Checkstyle >> Select the configuration file >>

..Window >> Preferences >> Checkstyle >> Select the configuration file >>

  1. Click on "Properties"button >> Uncheck the Protect Checkstyle Configuration file.
  2. Click on "Configure"button >> search for "New Line At End Of File">> Uncheck the checkbox which you see next to New Line At End Of File.
  1. 单击"Properties"按钮 >> 取消选中Protect Checkstyle Configuration file.
  2. 单击"Configure"按钮>>搜索"New Line At End Of File">>取消选中您在旁边看到的复选框New Line At End Of File

回答by Alexey Voinov

In my case that was a problem with improper configuration of that checker. By default it uses system default convention of line endings. I was working on windows and the project was using unix-style line endings. I don't think ignoring the warning is the best strategy, so what I've done is:

就我而言,这是该检查器配置不当的问题。默认情况下,它使用系统默认的行尾约定。我在 Windows 上工作,该项目使用的是 unix 样式的行尾。我不认为忽略警告是最好的策略,所以我所做的是:

<module name="Checker">
    <module name="NewlineAtEndOfFile">
        <property name="lineSeparator" value="lf" />
    </module>
</module>

回答by Ben

I run into this problem when files are saved as UNIX format instead of DOS. (or other way around if you do your work in Unix)

当文件保存为 UNIX 格式而不是 DOS 时,我遇到了这个问题。(或其他方式,如果您在 Unix 中工作)

To fix it, I use a program do convert from UNIX format to DOS and save the file in that format.

为了修复它,我使用了一个程序来将 UNIX 格式转换为 DOS 并以该格式保存文件。

In eclipse: File -> Convert Line Delimiters To -> Windows

在 Eclipse 中:文件 -> 将行分隔符转换为 -> Windows

回答by Heiko Mueller

//Here is the brute force solution:

//这里是蛮力解决方案:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;

/**
 *
 * @author hMueller
 */

public class UnixToWindowsEncoding {

//rewrites all .java files in Windows encoding
//a[0] = parent directory
//loops through all subdirectories and fixes whatever .java file it finds
public static void main(String[] a){
    String startdir = a[0];
    fixDir(startdir);

}

private static void fixDir(String dirname){
    try{
        File f = new File(dirname);
        File[] list = f.listFiles();
        for(int i = 0; i < list.length; i++){
            if(list[i].isDirectory()){
                fixDir(list[i].getAbsolutePath());
            }else{
                if(list[i].getName().endsWith(".java")){
                    ArrayList<String> l = new ArrayList<String>();
                    FileReader fr = new FileReader(list[i]);
                    BufferedReader br = new BufferedReader(fr);
                    String line = "";
                    while( (line = br.readLine()) != null){
                        l.add(line);
                    }
                    br.close();
                    fr.close();
                    list[i].delete();
                    FileWriter fwr = new FileWriter(list[i]);
                    for(String s : l){
                        fwr.write(s + "\r\n");
                    }
                    fwr.flush();
                    fwr.close();
                }
            }
        }
    }catch(Exception ioe){
        ioe.printStackTrace();
    }
}

}