Java 如何在 ant 的 build.xml 中设置 -Dfile.encoding?

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

How do I set -Dfile.encoding within ant's build.xml?

javaantcharacter-encodingjavac

提问by neu242

I've got java source files with iso-8859-1 encoding. When I run ant, I get "warning: unmappable character for encoding UTF-8". I can avoid this if I run ant -Dfile.encoding=iso-8859-1or add encoding="ISO-8859-1"to each javac statement.

我有带有 iso-8859-1 编码的 java 源文件。当我运行ant 时,我收到“警告:编码 UTF-8 的不可映射字符”。如果我运行ant -Dfile.encoding=iso-8859-1或将encoding="ISO-8859-1" 添加到每个 javac 语句,我可以避免这种情况。

Is there a way to set the property globally within build.xml? <property name="file.encoding" value="ISO-8859-1">does not work. I know that I can add a foo=ISO-8859-1 property and set encoding="${foo}" to each javac statement, but I'm trying to avoid that.

有没有办法在 build.xml 中全局设置属性? <property name="file.encoding" value="ISO-8859-1">不起作用。我知道我可以添加一个 foo=ISO-8859-1 属性并将 encoding="${foo}" 设置为每个 javac 语句,但我试图避免这种情况。

采纳答案by skaffman

A few options:

几个选项:

  1. add -Dfile.encoding=iso-8859-1to your ANT_OPTSenvironment variable
  2. use <presetdef>to setup defaults for all of your <javac>invocations
  1. 添加-Dfile.encoding=iso-8859-1到您的ANT_OPTS环境变量
  2. 用于<presetdef>为所有<javac>调用设置默认值

回答by skaffman

Ant itself cannot set system properties, but if you really want to, you can write a java program which sets the system property, and execute that from within Ant.

Ant 本身不能设置系统属性,但如果你真的想要,你可以编写一个设置系统属性的 java 程序,然后在 Ant 中执行它。

回答by Dominic Mitchell

If you've got files encoded in a particular way, it's probably best to tell javac that rather than forcing the whole JVM to use a particular encoding. The javac taskhas an encoding attribute for this reason.

如果文件以特定方式编码,最好告诉 javac,而不是强制整个 JVM 使用特定编码。由于这个原因,javac 任务有一个编码属性。

<javac srcdir="${src.dir}" destdir="${build.classes.dir}" encoding="iso-8859-1" />

But really, you should just convert the source files to UTF-8. Everything tastes better in UTF-8. :)

但实际上,您应该将源文件转换为 UTF-8。一切都在 UTF-8 中味道更好。:)

回答by Laxman G

Before changing build file i am getting java compile error like below.

在更改构建文件之前,我收到如下所示的 java 编译错误。

ApplicationConstant.java:73: error: unmappable character for encoding asciipublic static final String INVALID_MDTVERSION_SECOND = " This not compatible with the servera?s Version";

ApplicationConstant.java:73:错误:编码 ascii 的不可映射字符public static final String INVALID_MDTVERSION_SECOND = " This not compatible with the servera?s Version";

I have encounter this error when I used to have ant java target as:

当我曾经将 ant java 目标设为:

 <javac encoding="ascii"...>

Than i have change as below

比我有如下变化

<javac encoding="iso-8859-1" ...> 

This issue got solved.

这个问题解决了。