Ant 基础
使用ant脚本,对java web项目,编译、打包、发布部署到tomcat
ant.bat -f D:\AntTest\build.xml dist ant.bat -f D:\AntTest\build.xml deploy ant -f D:\AntTest\build.xml undeploy
Example1:
生成两个jar包,一个是源文件Jar,一个是编译后的Class文件Jar
<?xml version="1.0"?> <project basedir="." default="dist" name="Project Argon"> <description>A simple Java project</description> <property name="srcDir" location="${basedir}/src"/> <property name="buildDir" location="${basedir}/build"/> <property name="distDir" location="${basedir}/dist"/> <target name="init"> <tstamp/> <mkdir dir="${buildDir}"/> <mkdir dir="${distDir}"/> </target> <target name="compile" depends="init"> <javac srcdir="${srcDir}" destdir="${buildDir}"/> </target> <target name="dist" depends="compile"> <jar destfile="${distDir}/package-${DSTAMP}.jar" basedir="${buildDir}"> <manifest> <attribute name="Built-By" value="${user.name}"/> <attribute name="Main-Class" value="package.Main"/> </manifest> </jar> <jar destfile="${distDir}/package-src-${DSTAMP}.jar" basedir="${srcDir}"/> </target> <target name="clean"> <delete dir="${buildDir}"/> <delete dir="${distDir}"/> </target> </project>
Example2:
项目中:
<?xml version="1.0" encoding="UTF-8"?> <project basedir="." default="jar" name="ESB_YS_YS_InquiryMachineInfoSrv"> <!-- ################ --> <!-- # properties # --> <!-- ################ --> <property name="src.dir" value="${basedir}/src" /> <property name="build.dir" value="${basedir}/build"/> <property name="build.classes.dir" value="${build.dir}/classes"/> <property name="application.dir" value="D:\jboss-5.1.0.GA\"/> <!-- ################## --> <!-- # dependency lib # --> <!-- ################## --> <path id="lib.classpath"> <fileset dir="${application.dir}/client" > <include name="**/*.jar" /> </fileset> <fileset dir="${application.dir}/common" > <include name="**/*.jar" /> </fileset> </path> <!-- ########### --> <!-- # clean # --> <!-- ########### --> <target name="clean"> <delete dir="${build.dir}"/> <delete file="${basedir}/${ant.project.name}.jar"/> </target> <!-- ########### --> <!-- # init # --> <!-- ########### --> <target name="init" depends="clean"> <mkdir dir="${build.dir}"/> <mkdir dir="${build.classes.dir}"/> <copy includeemptydirs="false" todir="${build.classes.dir}"> <fileset dir="${src.dir}"> <exclude name="**/*.java" /> </fileset> </copy> </target> <!-- ############# --> <!-- # compile # --> <!-- ############# --> <target name="compile" depends="init"> <javac srcdir="${src.dir}" destdir="${build.classes.dir}" debug="on" encoding="utf-8" deprecation="on" optimize="off" includes="**" includeantruntime="yes"> <classpath refid="lib.classpath" /> </javac> </target> <!-- ############# --> <!-- # jar # --> <!-- ############# --> <target name="jar" depends="compile"> <jar jarfile="${basedir}/${ant.project.name}.jar"> <fileset dir="${build.classes.dir}"/> </jar> </target> <!-- ############ --> <!-- # deploy # --> <!-- ############ --> <target name="deploy" depends="undeploy"> <copy includeemptydirs="false" todir="${application.dir}/server/default/deploy" file="${basedir}/${ant.project.name}.jar"/> </target> <!-- ############## --> <!-- # undeploy # --> <!-- ############## --> <target name="undeploy"> <delete file="${application.dir}/server/default/deploy/${ant.project.name}.jar"/> </target> </project>
Example3:
.net项目提交前删除各层的DLL和pdb
clean-DLL.bat:
:: execute ant 注释 ant -f clean-DLL.xml pause
clean-DLL.xml:
<project name="clean-DLL" default="clean-DLL" basedir="."> <target name="clean-DLL"> <delete verbose="true" includeEmptyDirs="true"> <fileset dir="${basedir}"> <include name="*/bin/Debug/**/*" /> <include name="*/obj/Debug/**/*" /> </fileset> </delete> <delete verbose="true" dir="${basedir}/Web/bin" /> <delete verbose="true" includeEmptyDirs="true"> <fileset dir="${basedir}/Web/"> <include name="logs/**/*" /> </fileset> </delete> </target> </project>
Example4:
一个WEB工程打包为WAR包并部署到Jboss:
<?xml version="1.0"?> <project name="JBoss" default="war" basedir=".."> <property environment="env"/> <property name="app.dir" value="${basedir}/JWS"/> <property name="src.dir" value="${app.dir}/src"/> <property name="jboss.home" value="${env.JBOSS_HOME}"/> <property name="jboss.server.config" value="default"/> <property name="build.dir" value="${app.dir}/build"/> <property name="build.classes.dir" value="${build.dir}/classes"/> <!--Build classpath --> <path id="build.classpath"> <fileset dir="${basedir}/lib"> <includename="**/*.jar"/> </fileset> <pathelement location="${build.classes.dir}"/> </path> <!--=================================================================== --> <!--Preparesthebuild directory --> <!--=================================================================== --> <target name="prepare" depends="clean"> <mkdir dir="${build.dir}"/> <mkdir dir="${build.classes.dir}"/> </target> <!--=================================================================== --> <!--Compilesthesourcecode --> <!--=================================================================== --> <target name="compile" depends="prepare" description="编绎web 服务"> <javac srcdir="${src.dir}" destdir="${build.classes.dir}" debug="on" deprecation="on" optimize="off" includes="**"> <classpath refid="build.classpath"/> </javac> </target> <!-- Build thetestdeployments --> <target name="war" depends="compile" description="创建WS 发布包"> <war warfile="${app.dir}/Services.war" webxml="${app.dir}/WEB-INF/web.xml"> <classes dir="${build.classes.dir}"> <include name="com/foshanshop/ws/HelloWorldService.class"/> </classes> </war> </target> <target name="deploy" depends="war"> <copy file="${app.dir}/Services.war" todir="${jboss.home}/server/${jboss.server.config}/deploy"/> </target> <!--=================================================================== --> <!--Cleansup generatedstuff --> <!--=================================================================== --> <target name="clean"> <delete dir="${build.dir}"/> </target> </project>
2、Linux下ANT的安装
1。下载
下载地址:http://ant.apache.org/bindownload.cgi 下载apache-ant-1.8.1-bin.tar.gz(当前最新版本),将该下载包拷贝到/usr/local下(随意了,找个地方就好)
2。解压
cd /usr/local
tar -zxvf apache-ant-1.8.1-bin.tar.gz
解压后会在/usr/local 下生成一个文件夹apache-ant-1.8.1
3。配置环境变量
(ant是基于java的,所以要先确保系统中已经安装了jdk)
vi /etc/profile,如下所示
# System wide environment and startup programs, for login setup # Functions and aliases go in /etc/bashrc JAVA_HOME=/usr/java/jdk JAVA_BIN=/usr/java/jdk/bin PATH=$JAVA_HOME/bin:$PATH CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar export JAVA_HOME JAVA_BIN PATH CLASSPATH 以下省略……
添加如下内容:
ANT_HOME=/usr/local/apache-ant-1.8.1 PATH=$JAVA_HOME/bin:$ANT_HOME/bin:$PATH
export JAVA_HOME JAVA_BIN ANT_HOME PATH CLASSPATH
修改后如下所示:
# System wide environment and startup programs, for login setup # Functions and aliases go in /etc/bashrc JAVA_HOME=/usr/java/jdk JAVA_BIN=/usr/java/jdk/bin ANT_HOME=/usr/local/apache-ant-1.8.1 PATH=$JAVA_HOME/bin:$ANT_HOME/bin:$PATH CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar export JAVA_HOME JAVA_BIN ANT_HOME PATH CLASSPATH
[root@web jlib]# echo $ANT_HOME
/opt/apache/apache-ant-1.8.1
[root@web jlib]# echo $PATH
/usr/kerberos/sbin:/usr/kerberos/bin:/opt/apache/apache-ant-1.8.1/bin:/usr/java/jdk1.6.0_20/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
cd /usr/bin
ln -s -f /usr/local/ant/bin/ant
运行ant -version,输出如下内容:
[root@localhost ~]# ant -version
Apache Ant version 1.8.1 compiled on April 30 2010
ok,ant配置完成。