Chinese problem in ant of "Chinese problem not negotiable"

Source: Internet
Author: User
In the software system, because of the existence of a variety of character coding problems, such as another we have a very headache of Chinese problems. Therefore, it is now tending to use UTF-8 encoding as a coding format in software, thus avoiding the problem of most character encodings. This article is the same as the "80 ago" article, with Spring Project, please "Spring fans" of self-respect, nothing to disturb, the text of the deficiencies are welcome to criticize the advice.

In Java development, we often use ant to compile and package projects. By default, ant invokes a file named Build.xml, which defines the relevant units and tasks, and can execute the corresponding task unit according to the user command.
For example, in many open source projects such as spring, WebWork, Easyjweb, easydbo, and so on, its root directory has a build.xml file.
In foreign open source projects, the contents of the Build.xml file are all English characters, so build.xml use normal ANSI or UTF-8 or GBK, and will not encounter character encoding problems. And for the domestic open source project Easyjweb, easydbo, in order to facilitate domestic users, certainly in the document will contain some Chinese instructions. By default, including Chinese characters, build.xml files formatted as ANSI are not functioning correctly, which should be similar to the reason I write for dom4j.
At this point, we need to change the Build.xml file to the UTF-8 format and specify the format of the XML file as Utf-8. The following are some of the build.xml files in the Easyjweb Open source project:
<?xml version= "1.0" encoding= "UTF-8"?>
<project name= "EASYJF" default= usage "basedir=". "xmlns:artifact=" Urn:maven-artifact-ant ">
<property file= "Easyjfbuild.properties"/>
<!--directory definition--> <property name= "Main.src.dir" value= "${basedir}/src/main"/>
<property name= "Test.src.dir" value= "${basedir}/src/test"/>
<property name= "Lib.dir" value= "${basedir}/lib"/>
<property name= "Env.lib.dir" value= "${lib.dir}/build/"/>
<property name= "Doc.dir" value= "${basedir}/doc"/>
<property name= "Build.dir" value= "${basedir}/build"/>
<property name= "Release.dir" value= "${basedir}/release"/>
<property name= "Web.dir" value= "${basedir}/webapps"/>
<property name= "Ide.dir" value= "${basedir}/misc/ide"/>
<!--project Properties-->
<property name= "Project.provider.name" value= "EASYJF"/>
<property name= "Project.name" value= "Jweb"/>
<property name= "Project.author" value= "http://www.easyjf.com/"/>
<property name= "project.version" value= "0.6"/> <!--Publish file name-->
....
The problem here is how to build a Build.xml file in UTF-8 format.
the wrong habit:
In general, we are accustomed to using tools such as Notepad under Windows to encode the contents of an XML file directly and save it as a UTF-8 format, as shown in the following illustration:

The Build.xml file created in this way is incorrect, and the most disturbing thing is the very instability. This instability appears to be used correctly in some systems, and some systems may have errors similar to the following:
D:/easyjf/wlhy/bin>build.bat
BuildFile:.. /build.xml Build FAILED
D:/easyjf/wlhy/build.xml:1: Missing file root component
Total time:0 seconds
Please press any key to continue ... This error was also found by the Williamraym in the EASYJF team while writing the Easyjweb build script, and many netizens later reflected similar errors, such as the Iula project under the current EASYJF:
Http://www.easyjf.com/html/bbs/20060814/1208824819851986.htm
(Perhaps my machine fault-tolerant, no error, hehe!). the right approach:
As I said earlier, it is incorrect to create an XML file in UTF-8 format with Notepad, one of the correct ways to do this is to use Eclipse to build this XML file, and to set up the project as a UTF-8 for the entire project. A build.xml file is then created directly, and the file is saved in UTF-8 format by default.
The script file that you build, when you use ant, works fine regardless of what Chinese characters are included in the Build.xml. Of course, you should also be able to use other development tools to create UTF-8 format files, but also ask everyone to test. In addition, you can also use Java to write a small program to the ANSI format file into the UTF-8 file, so there is no problem. Summary:
  There is no problem with the build process for many open source projects because the script file does not contain Chinese characters or any other character. Face in the domestic open source project, because of the existence of Chinese annotation, description, etc., so in the establishment of build.xml files need to do some special processing, the use of UTF-8 format files. Of course, this paper also focuses on two methods of establishing UTF-8 file, in which Notepad is unstable and wrong, and the script built with professional Java development tools or JDK is correct and stable. (Note: This article author, easyjf open source Team Big Gorge, reprint, please keep the author's statement.) )  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.