在我的build.xml文件中,我通过cxf获取一些Java文件。其中一些Java文件需要以UTF-8编码。如何使用Ant将编码更改为UTF-8?
PS:我找到了如何将javac的编码设置为UTF-8的说明,但在javac之前我需要Java文件为UTF-8。否则我收到错误:
警告:编码utf-8
的不可映射字符
这是我的代码:
<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>
我该怎么做才能做到
<java classname="org.apache.cxf.tools.wsdlto.WSDLToJava" fork="true" failonerror="true">
在utf-8中编码java文件?
答案 0 :(得分:9)
Ant的<copy>
任务包含encoding
和outputencoding
属性。
<copy file="myJavaFile.java" tofile="myJavaFile.java" overwrite="true"
encoding="ISO-8859-1" outputencoding="UTF-8" />
<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>
只需将ISO-8859-1
更改为Java文件的编码格式。