如何使用 Apache Ant 以 UTF-8 编码 Java 文件?

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

How to encode Java files in UTF-8 using Apache Ant?

javaantencodingbuild.xml

提问by Madeline

In my build.xml file I fetch some Java files by cxf. Some of these Java files need to be encoded in UTF-8. How can I use Ant to change the encoding to UTF-8?

在我的 build.xml 文件中,我通过 cxf 获取了一些 Java 文件。其中一些 Java 文件需要以 UTF-8 编码。如何使用 Ant 将编码更改为 UTF-8?

PS: I found instructions for how to set the encoding for javac to UTF-8, but prior to javac I need Java files to be in UTF-8. Otherwise I get an error:

PS:我找到了有关如何将 javac 的编码设置为 UTF-8 的说明,但在 javac 之前,我需要 Java 文件为 UTF-8。否则我会收到一个错误:

warning: unmappable character for encoding utf-8

警告:用于编码 utf-8 的不可映射字符

Here is my code:

这是我的代码:

<macrodef name="lpwservice">
    <attribute name="name"/>
    <attribute name="package"/>
    <sequential>
        <property name="wsdlfile" value="${portal.basedir}/lpw/wsdl/@{name}.wsdl"/>

        <mkdir dir="${portal.basedir}/lpw/wsdl"/>
        <get src="${lpw.baseuri.cxf}/@{name}?wsdl" dest="${portal.basedir}/lpw/wsdl/@{name}.wsdl.new"/>
        <if>
            <and>
                <filesmatch file1="${portal.basedir}/lpw/wsdl/@{name}.wsdl" file2="${portal.basedir}/lpw/wsdl/@{name}.wsdl.new"/>
                <uptodate targetfile="${portal.basedir}/lpw-wsdl.jar" srcfile="${portal.basedir}/lpw/wsdl/@{name}.wsdl"/>
            </and>
            <then>
                <echo message="${wsdlfile} is up to date" level="info"/>
                <delete file="${portal.basedir}/lpw/wsdl/@{name}.wsdl.new"/>
            </then>
            <else>
                <echo message="${portal.basedir}/lpw/wsdl/@{name}.wsdl needs update" level="info"/>
                <move file="${portal.basedir}/lpw/wsdl/@{name}.wsdl.new" tofile="${portal.basedir}/lpw/wsdl/@{name}.wsdl" overwrite="true" />
                <java classname="org.apache.cxf.tools.wsdlto.WSDLToJava" fork="true" failonerror="true">
                    <arg value="-client"/>
                    <arg value="-d"/>
                    <arg value="${portal.basedir}/lpw/src"/>
                    <arg value="${portal.basedir}/lpw/wsdl/@{name}.wsdl"/>
                    <classpath>
                        <path refid="cxf.classpath"/>
                    </classpath>
                </java>
            </else>
        </if>
    </sequential>
</macrodef>

What should I do here to make

我应该在这里做什么

<java classname="org.apache.cxf.tools.wsdlto.WSDLToJava" fork="true" failonerror="true">

to encode java files in utf-8?

在utf-8 中编码java 文件?

回答by Christopher Peisert

Ant's <copy>task has attributes encodingand outputencoding.

Ant 的<copy>任务具有属性encodingoutputencoding.

Single Java File

单个 Java 文件

<copy file="myJavaFile.java" tofile="myJavaFile.java" overwrite="true"
    encoding="ISO-8859-1" outputencoding="UTF-8" />

All Java Files in a Directory

目录中的所有 Java 文件

<property name="source.dir" location="/path/to/java/files" />

<copy todir="${source.dir}" overwrite="true"
    encoding="ISO-8859-1" outputencoding="UTF-8">
  <fileset dir="${source.dir}" includes="*.java" />
</copy>

Simply change ISO-8859-1to the encoding format of your Java files.

只需更改ISO-8859-1为 Java 文件的编码格式即可。