java Javac:将警告视为错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1040074/
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-10-29 14:53:47  来源:igfitidea点击:

Javac: Treat warnings as errors

javaantwarningsjavac

提问by Itay Maman

I have an Ant file that compiles my program. I want the javac task to fail if any warning was reported by the compiler. Any clue on how to do that?

我有一个编译我的程序的 Ant 文件。如果编译器报告了任何警告,我希望 javac 任务失败。关于如何做到这一点的任何线索?

采纳答案by Michael Myers

Use the -Werrorflag. It's not listed in the -helpoutput, but it works.

使用-Werror旗帜。它没有在-help输出中列出,但它有效。

I found it through this blog entryand tested on my own code (in NetBeans with Ant). The output was:

我通过这个博客条目找到了它,并在我自己的代码上进行了测试(在 NetBeans 和 Ant 中)。输出是:

MyClass.java:38: warning: [serial] serializable class MyClass has no definition of serialVersionUID
public class MyClass extends JComponent {
1 warning
BUILD FAILED (total time: 3 seconds)

Note that this is Java 6 only, though.

请注意,这仅适用于 Java 6。

Edit: Example of specifying this in Ant buildfile:

编辑:在 Ant 构建文件中指定它的示例:

<javac srcdir="${src.dir}" destdir="${classes.dir}" classpathref="classpath">
    <compilerarg value="-Xlint:all"/>
    <compilerarg value="-Werror"/>
</javac>