Chinese in ant, which has no questions about Chinese Characters

Source: Internet
Author: User
In the software system, there are various character encoding problems, such as the Chinese problem that we have a headache. As a result, it tends to use UTF-8 encoding as the software to pass the encoding format, which can avoid the problem of most character encoding. This article is the same as the article "before 80" and has nothing to do with the spring Project. Please be self-respecting and do not disturb the spring fans. You are welcome to criticize and advise the shortcomings in this article.

In Java Development, ant is often used to compile and package projects. By default, ant calls a file named build. XML, which defines relevant units and tasks, and can execute corresponding task units according to user commands.
For example, in many open-source projects such as spring, webwork, easyjweb, and easydbo, the root directory contains a build. xml file.
In open-source projects outside China, the content in the build. xml file is all English characters, so it is okay to use the normal ANSI or UTF-8 or GBK build. XML, and there will be no character encoding problems. For domestic open-source projects easyjweb and easydbo, some Chinese instructions must be included in the file to facilitate domestic users. By default, the build. xml file in ANSI format cannot run properly, which is similar to the dom4j file.
At this point, we need to change the build. xml file to the UTF-8 format, and specify the XML file format as UTF-8. The following is part of the build. xml file 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"/> <! -- Release file name -->
....
The problem here is how to create a build. xml file in UTF-8 format.
Bad habits:
In general, we are used to using tools such as notepad in Windows to directly encode the content of XML files, and save it as a UTF-8, as shown in:
 
The build. xml file created in this way is incorrect, and the most worrying thing is that it is very unstable. This instability can be used correctly in some systems, and some systems may encounter errors similar to the following: D:/easyjf/wlhy/bin> build. bat
Buildfile: ../build. xml build failed
D:/easyjf/wlhy/build. xml: 1: The file root component is missing.
Total time: 0 seconds
Press any key to continue... this error was also found when William Raym from the easyjf team wrote the easyjweb build script. Later, many netizens reported similar errors, such as the IULA project under easyjf:
Http://www.easyjf.com/html/bbs/20060814/1208824819851986.htm
(Maybe my machine has good fault tolerance and won't report an error !).The correct method:
As mentioned above, using notepad to create an XML file in UTF-8 format is incorrect. After many practices, one of the correct methods is to use eclipse to create this XML file, when creating a project, you need to set the character set of the entire project to a UTF-8, and then directly build a build. XML file, which is saved as a UTF-8 by default.
In this way, the created script file can run normally no matter what Chinese characters are contained in build. xml when ant is used. Of course, it should also be able to use other development tools to create UTF-8 format files, please also try. In addition, you can also use Java to write a small program to convert an ANSI format file into a UTF-8 file, this is no problem.Summary:
The reason why many open-source projects do not have problems during the build process is that their script files do not contain Chinese characters or other characters. In domestic open-source projects, due to the existence of Chinese notes, instructions, so in the establishment of build. xml files need to do some special processing, the use of UTF-8 format files. Of course, this article also focuses on the two methods to establish the UTF-8 file, the use of notepad is unstable, wrong; the scripts created with professional Java development tools or JDK are correct and stable. (Note: The author of this article, easyjf open-source team Daxia, reposted 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.