运用Ant编译Java程序的一个小实例
1. native2ascii.bat文件内容:@echo off
@setlocal
set JAVA_HOME=%JDK6_HOME%
if "%JAVA_HOME%" == "" goto noJavaHome
SET CMD=%JAVA_HOME%\bin\java -Xms256m -Xmx512m -cp build\lib\ant.jar;build\lib\optional.jar;%JAVA_HOME%\lib\tools.jar;%JAVA_HOME%
\jre\lib\rt.jar org.apache.tools.ant.Main
SET BUILD=%CMD% -buildfile native2ascii.xml
if "X%1"=="Xnative" goto native
:native
%BUILD% -propertyfile build.properties native
goto mainEnd
:noJavaHome
echo.
echo Warning: JAVA_HOME environment variable is not set.
echo If build fails because sun.* classes could not be found
echo you will need to set the JAVA_HOME environment variable
echo to the installation directory of java.
echo.
:mainEnd
@endlocal
2. native2ascii.xml文件内容:
<?xml version="1.0" encoding="UTF-8"?>
<project name="MIT" default="native" basedir=".">
<property name="dist.dir" value="./dist"/>
<property name="webapp.dir" value="./web"/>
<target name="native" >
<echo message="Convert Resource Files"/>
<!-- Convert Resource Files -->
<native2ascii encoding="ISO-8859-1" src="${webapp.dir}/WEB-INF/classes/resources/en_US" dest="${dist.dir}/classes" includes="*.properties"/>
<native2ascii encoding="GBK" src="${webapp.dir}/WEB-INF/classes/resources/zh_CN" dest="${dist.dir}/classes" includes="*.properties"/>
<native2ascii encoding="GBK" src="${webapp.dir}/WEB-INF/classes/resources/zh_TW" dest="${dist.dir}/classes" includes="*.properties"/>
<native2ascii encoding="ISO-8859-1" src="${webapp.dir}/WEB-INF/classes/resources/nl_NL" dest="${dist.dir}/classes" includes="*.properties"/>
<native2ascii encoding="EUC-JP" src="${webapp.dir}/WEB-INF/classes/resources/ja_JP" dest="${dist.dir}/classes" includes="*.properties"/>
</target>
</project>
3. <project>元素有个default属性,指定默认执行的target。即在执行native2ascii批处理文件时没有传入任何参数,则默认执行default中指定的任务。
4. 批处理中参数的传递
(1) 命令
D:\dev\RB-2.1.x-gx>native2ascii
是运行D:\dev\RB-2.1.x-gx目录下的native2ascii.bat文件
(2)命令行中输入:native2ascii native
往批处理文件native2ascii.bat中传入参数native。
在给出的native2ascii.bat文件中,当传入native参数,批处理程序会根据
if "X%1"=="Xnative" goto native
找到批处理块:
:native
%BUILD% -propertyfile build.properties native
goto mainEnd
然后执行:
%BUILD% -propertyfile build.properties native
命令,这条命令里的native,是传递给native2ascii.xml文件的参数,用于指定执行native2ascii.xml文件的哪个target。如果没有这个参数,则默认执行native2ascii.xml文件中<project>元素的default属性指定的target。
作者:csz_363874279qqcom
补充:软件开发 , Java ,