- 通过一番琢磨,参考网上些许资料以及Ant文档,将Ant在J2EE构建中常用的脚本整合了起来,实现了Ant从版本控制器(SVN/CVS/VSS)checkout项目源码, 编译编译打包,部署软件包到远程/本地服务器的整合,从此任何复杂的J2EE构建变得方便优雅...
- 现记录大致笔记如下:
- 分为两部分:
- 1.我的环境
- 2.文字大致叙述整合流程以及注意事项
- 3.贴脚本实例
- 一. 我的环境
- JDK1.7/TOMCAT6.0/ANT1.8.2/SVN/CVS/SERVER-U
- 二:整合流程
- 1.下载并安装Ant,配置ANT_HOME,PATH;在命令行执行ANT -VERSION,查看版本号检查是否安装配置成功
- 2. 将ant从SVN checkout,上传FTP,启动Tomcat的依赖Jar分别加入进ant目录下地lib目录,具体为
- A.SVN依赖jar: svnant.jar/svnClientAdapter.jar/svnjavahl.jar;可从这里下载(http://download.youkuaiyun.com/source/3544090)
- B.FTP依赖Jar: commons-net-1.4.1.jar;可从这里下载(http://download.youkuaiyun.com/source/3060302)
- C.Tomcat 依赖Jar:将Tomcat目录中lib目录下地catalina-ant.jar拷贝之ant目录下地lib目录
- 3.编写脚本(分段编写,并一一测试)
- A.从SVN/CVS checkout项目
- B.打包编译
- C.部署软件包到远程服务器
- (1) 通过FTP上传到远程服务器webapps目录:
- 注意事项:本人在远程服务器架设的ftp服务器是Server-U,设置Server-U账户权限时注意赋予账户写文件的权限以及访问需要的路径(可以为任意路径)
- (2) FTP上传完毕后,启动Web application:
- 注意设置添加账户 <user username="admin" password="admin" roles="admin,manager"/>
- 三:脚本实例
- <!-- svn下载,编译,打包,上传到ftp服务器,启动Web程序-->
- 1.Tomcat/svn/ftp等配置属性文件 ant.properties
- webapp.name=antDemo
- catalina.home=D:\Tomcat6.1
- catalina.apps=D:\Tomcat6.1\webapps
- #svn properties#
- svn.url=svn://192.168.1.202/antDemo
- svn.username=*****
- svn.password=*****
- #cvs properties#
- #cvs.root=:pserver:***@192.168.8.27:2401/home/Functions
- #cvs.password=*****
- #cvs.checkout.dir=.
- #tomcat properties#
- tomcat.manager.url=http://localhost:9080/manager
- tomcat.manager.username=*****
- tomcat.manager.password=*****
- #ftp properties#
- ftp.server=192.168.1.202
- ftp.password=*****
- ftp.userid=*****
- 2.build.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <project name="${webapp.name}" default="start" basedir=".">
- <!-- svn下载,编译,打包,上传到ftp服务器,启动Web程序-->
- <property file="ant.properties" /> <!-- 引入Tomcat/svn/ftp等配置属性文件-->
- <property name="complile.dir" location="${basedir}/bin/classes"/>
- <property name="src.dir" value="${basedir}/src"/>
- <property name="lib.dir" value="${basedir}/WebRoot/WEB-INF/lib"/>
- <!-- =================================
- clean files
- ================================= -->
- <target name="cleanAll">
- <delete dir="${basedir}" />
- </target>
- <!-- Ant svn Task Definition -->
- <taskdef name="svn" classname="org.tigris.subversion.svnant.SvnTask" />
- <!-- =================================
- SVN Checkout
- ================================= -->
- <target name="svn.checkout" depends="cleanAll">
- <mkdir dir="${basedir}/SVN"/>
- <svn javahl="true" username="${svn.username}" password="${svn.password}">
- <checkout url="${svn.url}" destPath="${basedir}/SVN"></checkout>
- </svn>
- </target>
- <!-- =================================
- CVS Checkout
- ================================= -->
- <!--
- <target name="initpass" depends="cleanAll">
- <cvspass cvsroot="${cvs.root}" password="${cvs.password}" />
- </target>
- <target name="checkout" depends="initpass">
- <cvs cvsroot="${cvs.root}" command="checkout" quiet="true" cvsrsh="ssh" package="${webapp.name}" dest="${cvs.checkout.dir}" />
- <cvs cvsroot="${cvs.root}" command="checkout" quiet="true" cvsrsh="ssh" package="${webapp.name}" dest="${cvs.checkout.dir}" />
- </target>
- -->
- <!-- =================================
- target: compile the src
- ================================= -->
- <target name="init">
- <delete dir="${complile.dir}"/>
- <mkdir dir="${complile.dir}"/>
- </target>
- <path id="complile.classpath">
- <fileset dir="${catalina.home}/lib">
- <include name="catalina*.jar"/>
- </fileset>
- <fileset dir="${lib.dir}">
- <include name="**/*.jar"/>
- </fileset>
- </path>
- <target name="complile" depends="init">
- <javac srcdir="${src.dir}" destdir="${complile.dir}">
- <classpath path="${complile.dir}" />
- <classpath refid="complile.classpath"/>
- </javac>
- </target>
- <!-- =================================
- target: deploy war
- ================================= -->
- <target name="war" depends="complile">
- <delete>
- <fileset dir="." includes="**/*.war" />
- </delete>
- <war destfile="${webapp.name}.war" webxml="WebRoot/WEB-INF/web.xml">
- <classes dir="${complile.dir}"/>
- <fileset dir="WebRoot" id="dir">
- <include name="**/*.*"/>
- <exclude name="WEB-INF"/>
- </fileset>
- </war>
- </target>
- <!-- =================================
- target: upload with ftp
- ================================= -->
- <target name="upload" depends="war">
- <ftp password="${ftp.password}"
- server="${ftp.server}"
- userid="${ftp.userid}"
- remotedir="${catalina.apps}"
- binary="true"
- >
- <fileset file="${basedir}/${webapp.name}.war"/>
- </ftp>
- </target>
- <!-- Ant Tomcat Task Definition -->
- <taskdef name="deploy" classname="org.apache.catalina.ant.DeployTask" />
- <taskdef name="undeploy" classname="org.apache.catalina.ant.UndeployTask" />
- <taskdef name="remove" classname="org.apache.catalina.ant.RemoveTask" />
- <taskdef name="reload" classname="org.apache.catalina.ant.ReloadTask" />
- <taskdef name="start" classname="org.apache.catalina.ant.StartTask" />
- <taskdef name="stop" classname="org.apache.catalina.ant.StopTask" />
- <taskdef name="list" classname="org.apache.catalina.ant.ListTask" />
- <!-- =================================
- target: deploy_to_tomcat
- ================================= -->
- <target name="deploy" depends="war">
- <deploy url="${tomcat.manager.url}"
- username="${tomcat.manager.username}"
- password="${tomcat.manager.password}"
- path="/${webapp.name}"
- war="file:${basedir}/${webapp.name}.war" />
- </target>
- <target name="undeploy">
- <undeploy url="${tomcat.manager.url}"
- username="${tomcat.manager.username}"
- password="${tomcat.manager.password}"
- path="/${webapp.name}"/>
- </target>
- <target name="remove">
- <remove url="${tomcat.manager.url}"
- username="${tomcat.manager.username}"
- password="${tomcat.manager.password}"
- path="/${webapp.name}"/>
- </target>
- <target name="reload">
- <reload url="${tomcat.manager.url}"
- username="${tomcat.manager.username}"
- password="${tomcat.manager.password}"
- path="/${webapp.name}"/>
- </target>
- <target name="start" depends="upload">
- <start url="${tomcat.manager.url}"
- username="${tomcat.manager.username}"
- password="${tomcat.manager.password}"
- path="/${webapp.name}"/>
- </target>
- <target name="stop">
- <stop url="${tomcat.manager.url}"
- username="${tomcat.manager.username}"
- password="${tomcat.manager.password}"
- path="/${webapp.name}"/>
- </target>
- <target name="list">
- <list url="${tomcat.manager.url}"
- username="${tomcat.manager.username}"
- password="${tomcat.manager.password}"/>
- </target>
- <!-- =================================
- target: tomcat shell
- ================================= -->
- <target name="tomcat.start">
- <java jar="${catalina.home}/bin/bootstrap.jar" fork="true">
- <jvmarg value="-Dcatalina.home=${catalina.home}"/>
- </java>
- </target>
- <target name="tomcat.stop">
- <java jar="${catalina.home}/bin/bootstrap.jar" fork="true">
- <jvmarg value="-Dcatalina.home=${catalina.home}"/>
- <arg line="stop"/>
- </java>
- </target>
- <target name="tomcat.debug">
- <java jar="${catalina.home}/bin/bootstrap.jar" fork="true">
- <jvmarg value="-Dcatalina.home=${catalina.home}"/>
- <jvmarg value="-Xdebug"/>
- <jvmarg value="-Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n"/>
- </java>
- </target>
- </project>
Ant+Tomcat自动部署
http://blog.youkuaiyun.com/zhhp1314520/article/details/2220139
http://lei705.blog.163.com/blog/static/19241669201002022916748/
Ant 构建工具(从SVN下载项目 ,编译,到tomcat下运行项目)
http://blog.youkuaiyun.com/Rose19891213/article/details/6085899
使用ANT打包J2EE程序
http://www.chiefsailor.net/blog/2011/06/deploy_j2ee_eclipse_project_war_with_ant/
http://tiankong2009519.blog.163.com/blog/static/117684369200910251653149/
WAR文件的Ant构建文件(build.xml)
【转载地址】:http://www.gootry.com/wangzhuan/article/100916183417/214
<project name="Hello World Web Site" default ="war " basediz = ".">
<!--构建 WAR 文件-->
<target name ="war " description="Builds the WAR file.">
<war destfile="\${deploy.war}" webxml="\${basedir}/webapp-diz/WEB-INF/web.xml" basediz="\${basedir}/webapp-dir" excludes="WEB-INF/**/*">
<lib dir="\${basedir}/webapp-dir/WEB-INF/lib"/>
<webinf dir="\${basedir}/webapp-dir/WEB-INF" excludes="web.xml">
<metainf dir ="\${basedir}/webapp-dir/META-INF"/>
</war>
</target>
</project>
请注意war.Ant任务怎样创建WAR文件。笔者在打包文件中使用了webxml属性,并给它提供WEB-INF/web.xml文件的路径,然后让它包含了Web应用程序的basedir下的所有文件,并设置为\${basedir}/webapp-dir。
笔者还告诉Ant,在哪里能找到这个特殊对待的WEB-INF目录,这个WEB-INF目录很特殊,它虽然没有包含服务于客户端的文件,但仍然组成了Web应用程序代码和配置文件。
但是,笔者不想在该打包文件中再包含WEB-INF/web.xml文件,因为这会让Ant误认为笔者想两次包含web.xml。因此,笔者进行了excludes="WEB-INF/**/*"属性设置。双星号(**)意味着映射所有目录路径,由此推断,这一设置排斥了属性设置并让Ant不包含WEB-INF树中的内容。
这是笔者预期的内容,因为webinf元素已经包含了WEB-INF树,减去web.xml文件(这已通过webxml属性进行了特殊处理)。
Ant: 大规模应用中的应用
http://www.blogjava.net/chelsea/archive/2008/01/12/174901.html
- <?xml version="1.0"?>
- <project name="PRO_NAME" default="all" basedir=".">
- <property name="src.dir" value="src" />
- <property name="classes.dir" value="WEB-INF/classes" />
- <property name="lib.dir" value="WEB-INF/lib" />
- <!--用于输出打包的文件夹-->
- <property name="temp.dir" value="${java.io.tmpdir}/${ant.project.name}" />
- <property name="target.dir" value="target" />
- <!-- 定义classpath -->
- <path id="master-classpath">
- <fileset file="${lib.dir}/*.jar" />
- <pathelement path="${classes.dir}" />
- </path>
- <target name="clean">
- <delete dir="${classes.dir}" />
- <delete dir="${temp.dir}" />
- <delete file="${target.dir}/${ant.project.name}.jar"/>
- <delete file="${target.dir}/${ant.project.name}.war"/>
- <delete dir="${target.dir}" />
- </target>
- <!-- 初始化任务 -->
- <target name="init" depends="clean">
- <mkdir dir="${target.dir}" />
- <mkdir dir="${temp.dir}" />
- <mkdir dir="${classes.dir}" />
- </target>
- <!-- 编译 -->
- <target name="compile" depends="init" description="compile the source files">
- <javac srcdir="${src.dir}" destdir="${classes.dir}" debug="true" encoding="GB2312">
- <classpath refid="master-classpath" />
- </javac>
- <copy todir="${classes.dir}" overwrite="true">
- <fileset dir="${src.dir}">
- <include name="**/*.xml" />
- <include name="**/*.properties" />
- </fileset>
- </copy>
- </target>
- <!--打jar包-->
- <target name="jar" depends="compile">
- <jar jarfile="${target.dir}/${ant.project.name}.jar">
- <fileset dir="${classes.dir}">
- <include name="**/*" />
- </fileset>
- </jar>
- </target>
- <target name="preWar" depends="jar">
- <copy todir="${temp.dir}/WEB-INF/lib" overwrite="true">
- <fileset dir="${target.dir}">
- <include name="${ant.project.name}.jar"/>
- </fileset>
- </copy>
- <copy todir="${temp.dir}" overwrite="true">
- <fileset dir="${basedir}">
- <include name="${lib.dir}/*.jar" />
- <include name="index.html" />
- <include name="page/**/*.*" />
- <include name="WEB-INF/*.xml" />
- </fileset>
- </copy>
- </target>
- <!--打war包-->
- <target name="war" depends="preWar">
- <jar jarfile="${target.dir}/${ant.project.name}.war">
- <fileset dir="${temp.dir}">
- <include name="**/*.*"/>
- </fileset>
- </jar>
- </target>
- <target name="all" depends="war">
- <delete dir="${temp.dir}"/>
- </target>
- </project>