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

不羁岁月 提交于 2020-01-03 10:56:12

问题


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?

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:

warning: unmappable character for encoding 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?


回答1:


Ant's <copy> task has attributes encoding and outputencoding.

Single Java File

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

All Java Files in a Directory

<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-1 to the encoding format of your Java files.



来源:https://stackoverflow.com/questions/12374324/how-to-encode-java-files-in-utf-8-using-apache-ant

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!