1、windows下配置ant运行环境,定义JAVA_HOME、ANT_HOME、PATH中加入%JAVA_HOME%\bin和%ANT_HOME%\bin;ant使用1.6.5版本或以上,jdk使用1.5或以上;
2、java工程目录推荐按照如下设置,如果是普通的应用工程,则
App
-src
-config
-test
-lib
-so/*.*
-dll/*.*
-*.bat
-*.sh
-src
-config
-test
-lib
-so/*.*
-dll/*.*
-*.bat
-*.sh
如果是web应用工程,则
webApp
-src
-web
-config
-test
-lib
-src
-web
-config
-test
-lib
3、以如下build.xml为模板,重新编写ant脚本,程序员只要定义好外部类路径和内部类路径即能正常编译
<?
xml
version
="1.0"
encoding
="UTF-8"
?>
< project default ="release" name ="test" >
<!-- 这里引入的编译器是eclipse用的,需要将eclipse带的编译器ecj.jar拷贝到%ANT_HOME%\lib下,比jdk自带的javac好很多,删掉改行则使用jdk自带的-->
< property name ="build.compiler" value ="org.eclipse.jdt.core.JDTCompilerAdapter" />
<!-- 源码默认放在目录src下,重新定义则覆盖源码目录定义-->
< property name ="src" value ="./src" />
<!-- 如果项目是web应用,则相关页面和资源默认放在目录web下,重新定义则覆盖web应用目录定义-->
< property name ="web" value ="./web" />
<!-- 配置文件默认放在目录config下,重新定义则覆盖配置文件定义-->
< property name ="config" value ="./config" />
<!-- 配置作者cuiyingfeng,重新定义则覆盖-->
< property name ="author" value ="cuiyingfeng" />
<!-- 配置作者cuiyingfeng,重新定义则覆盖-->
< property name ="version" value ="0.9.0.0" />
<!-- 以上属性在满足工程目录定义得情况下可以不用重复定义,以下属性则必须存在-->
< property name ="type" value =".jar" />
<!-- type为jar则生成test.jar,type为war则生成test.war-->
< property name ="encoding" value ="UTF-8" />
< property name ="sourcejdk" value ="1.5" />
< property name ="targetjdk" value ="1.5" />
<!-- 引用外部类路径,仅仅是编译时使用到,程序运行或者部署不需要使用的jar,比如servlet.jar等-->
< path id ="ref-classpath" >
</ path >
<!-- 依赖外部类路径,编译时使用到,程序运行或者部署也用到-->
< path id ="dep-classpath" >
< fileset dir ="../../Lib" >
< include name ="activation.jar" />
< include name ="axis.jar" />
< include name ="commons-logging*.jar" />
< include name ="commons-discovery*.jar" />
< include name ="commons-dbcp*.jar" />
< include name ="commons-pool*.jar" />
< include name ="jaxrpc*.jar" />
< include name ="log4j*.jar" />
< include name ="mail*.jar" />
< include name ="asm*.jar" />
< include name ="cglib*.jar" />
< include name ="aspect*.jar" />
< include name ="spring*.jar" />
< include name ="saaj*.jar" />
< include name ="wsdl4j*.jar" />
</ fileset >
< fileset dir ="../Common/release/lib" >
< include name ="*.jar" />
</ fileset >
</ path >
< import file ="./build_common.xml" />
</ project >
< project default ="release" name ="test" >
<!-- 这里引入的编译器是eclipse用的,需要将eclipse带的编译器ecj.jar拷贝到%ANT_HOME%\lib下,比jdk自带的javac好很多,删掉改行则使用jdk自带的-->
< property name ="build.compiler" value ="org.eclipse.jdt.core.JDTCompilerAdapter" />
<!-- 源码默认放在目录src下,重新定义则覆盖源码目录定义-->
< property name ="src" value ="./src" />
<!-- 如果项目是web应用,则相关页面和资源默认放在目录web下,重新定义则覆盖web应用目录定义-->
< property name ="web" value ="./web" />
<!-- 配置文件默认放在目录config下,重新定义则覆盖配置文件定义-->
< property name ="config" value ="./config" />
<!-- 配置作者cuiyingfeng,重新定义则覆盖-->
< property name ="author" value ="cuiyingfeng" />
<!-- 配置作者cuiyingfeng,重新定义则覆盖-->
< property name ="version" value ="0.9.0.0" />
<!-- 以上属性在满足工程目录定义得情况下可以不用重复定义,以下属性则必须存在-->
< property name ="type" value =".jar" />
<!-- type为jar则生成test.jar,type为war则生成test.war-->
< property name ="encoding" value ="UTF-8" />
< property name ="sourcejdk" value ="1.5" />
< property name ="targetjdk" value ="1.5" />
<!-- 引用外部类路径,仅仅是编译时使用到,程序运行或者部署不需要使用的jar,比如servlet.jar等-->
< path id ="ref-classpath" >
</ path >
<!-- 依赖外部类路径,编译时使用到,程序运行或者部署也用到-->
< path id ="dep-classpath" >
< fileset dir ="../../Lib" >
< include name ="activation.jar" />
< include name ="axis.jar" />
< include name ="commons-logging*.jar" />
< include name ="commons-discovery*.jar" />
< include name ="commons-dbcp*.jar" />
< include name ="commons-pool*.jar" />
< include name ="jaxrpc*.jar" />
< include name ="log4j*.jar" />
< include name ="mail*.jar" />
< include name ="asm*.jar" />
< include name ="cglib*.jar" />
< include name ="aspect*.jar" />
< include name ="spring*.jar" />
< include name ="saaj*.jar" />
< include name ="wsdl4j*.jar" />
</ fileset >
< fileset dir ="../Common/release/lib" >
< include name ="*.jar" />
</ fileset >
</ path >
< import file ="./build_common.xml" />
</ project >
4、dos下cd进工程目录,然后ant,工程将自动编译,编译成功后,将在工程目录下产生一个build和release目录,其中build目录中存放编译中间结果,release目录中存放的是可以直接安装部署的java应用和web应用。执行ant clean将会删除当前工程目录下的build和release目录。
总结:该模板封装了使用ant进行编译的细节,只暴露了一些工程属性的设置给使用者,实际应用中,程序员不需要考虑ant语法,只需要关注部分工程属性就能很方便地对java工程进行发布。该模板非常实用,基本不需要修改就可以直接在大多数J2EE项目中使用。
ps:build_common.xml可以在我的资源中下载,定价较高,慎下!
ps:build_common.xml可以在我的资源中下载,定价较高,慎下!
本文转自 cuiyingfeng 51CTO博客,原文链接:
http://blog.51cto.com/cuiyingfeng/837548
,如需转载请自行联系原作者