强势推荐ANT小蚂蚁

共计 6195 个字符,预计需要花费 16 分钟才能阅读完成。

之前就知道 ANT 这个小蚂蚁,一直没有用过,这两天,下了点功夫,写了一个 ANT 的脚本。功能还行,可以完成同步 svn 代码、编译代码、打包代码、上传、部署的功能。

直接上干货,看代码。

[code]

#JDK home

jdk.home=/opt/Java/jdk1.6.0_33

#webapp name

webapp.name=pmeapp

project.name=pmeapp

#svn

svn.url=http://127.0.0.1:9344/svn/pmeapp/trunk

svn.uname=dapeng

svn.pwd=dapengpwd

#war

war.exclude=

war.exclude.classes=

#ftp

ftp.server=127.0.0.1

ftp.password=dapengpwd

ftp.userid=dapeng

ftp.path=/opt/ftp/

#ssh

ssh.host=127.0.0.1

ssh.path=/opt/tomcat6/webapps

ssh.pwd=dapengpwd

ssh.uname=dapeng

#ssh

ssh.path.webapp=/opt/tomcat6/webapps

ssh.server.bin=/opt/tomcat6/bin

ssh.cmd.sshClean=rm -rf /opt/tomcat6/webapps/${webapp.name}/

ssh.server.start=/opt/tomcat6/bin/startup.sh

ssh.server.stop=/opt/tomcat6/bin/shutdown.sh

[/code]

另外一个是关键中的核心

[sourcecode]

<project basedir=’.’default=’usage’name=’${project.name}’>

<!– =================================================================== –>

<!– 下句是 import 进 ant 属性配置文件,properties 文件里存放基本的配置变量. –>

<!– 该变量可以在 build.xml 中直接引用. –>

<!– =================================================================== –>

<property file=’ant.properties’/>

<!– =================================================================== –>

<!– 以下的几个属性是系统自带的, 初始了 tstamp 之后, 它们就有值了 –>

<!– ${DSTAMP} ${TSTAMP} ${TODAY} –>

<tstamp/>

<property name=’war.name’value=’${webapp.name}’/>

<!– =================================================================== –>

<!– Init –>

<!– =================================================================== –>

<target name=’init’>

<echo message=’————————————-‘/>

<echo message=’start ant build ${project.name} – ${DSTAMP}${TSTAMP}’/>

<property name=’debug’value=’off’/>

<property name=’optimize’value=’on’/>

<property name=’deprecation’value=’on’/>

<!– java 源文件路径 –>

<property name=’src.dir’value=’${basedir}/src’/>

<!– jar 包路径 –>

<property name=’lib.dir’value=’${basedir}/WebRoot/WEB-INF/lib’/>

<!– webapp 路径 –>

<property name=’webapp.dir’value=’${basedir}/WebRoot’/>

<!– 准备源文件路径 –>

<property name=’build.src’value=’${basedir}/AntBuild/build’/>

<!– 编译源文件路径 –>

<property name=’build.dest’value=’${basedir}/AntBuild/bin’/>

<!– 准备 webapp 文件路径 –>

<property name=’buildwar.dest’value=’${basedir}/AntBuild/warsrc’/>

<!– 打包 war 文件路径 –>

<property name=’war.dest’value=’${basedir}/AntBuild/war’/>

<!– jre lib 路径 –>

<property name=’jre.lib’value=’${jdk.home}/jre/lib’/>

<!– 引用 svn task 文件,使用 svn 任务可以使用–>

<typedef resource=’org/tigris/subversion/svnant/svnantlib.xml’/>

<!– 设置 svn 相关属性 –>

<svnSetting id=’svn.setting’svnkit=’true’username=’${svn.uname}’password=’${svn.pwd}’javahl=’false’/>

<!– classpath –>

<path id=’classpath’>

<!–web.lib–>

<fileset dir=’e:/lib’>

<include name=’/*.jar’/>

</fileset>

<fileset dir=’${jre.lib}’>

<include name=’
/.jar’/>

</fileset>

<fileset dir=’${lib.dir}’>

<include name=’**/
.jar’/>

</fileset>

<!–<pathelement location=’lib/‘/>–>

</path>

</target>

<!– =================================================================== –>

<!– checkout –>

<!– =================================================================== –>

<target name=’checkout’depends=’init’>

<echo message=’————————————-‘/>

<echo message=’start checkout ${project.name} – ${DSTAMP}${TSTAMP}’/>

<svn refid=’svn.setting’>

<checkout url=’${svn.url}’revision=’HEAD’destPath=’.’/>

</svn>

</target>

<!– =================================================================== –>

<!– 编译源文件–>

<!– =================================================================== –>

<target name=’build’depends=’checkout’>

<echo message=’————————————-‘/>

<echo message=’start build ${project.name} – ${DSTAMP}${TSTAMP}’/>

<mkdir dir=’${buildwar.dest}/WEB-INF/classes’/>

<delete>

<fileset dir=’${buildwar.dest}/WEB-INF/classes’includes=’/.‘/>

</delete>

<javac srcdir=’${src.dir}’destdir=’${buildwar.dest}/WEB-INF/classes’debug=’${debug}’optimize=’${optimize}’includeantruntime=’on’>

<classpath refid=’classpath’/>

</javac>

<copy todir=’${buildwar.dest}/WEB-INF/classes’>

<fileset dir=’${src.dir}’>

<include name=’
/.‘/>

<exclude name=’/*.java’/>

</fileset>

</copy>

</target>

<!– =================================================================== –>

<!– 打 war 包–>

<!– =================================================================== –>

<target name=’war’depends=’build’>

<echo message=’————————————-‘/>

<echo message=’start war ${project.name} – ${DSTAMP}${TSTAMP}’/>

<delete>

<fileset dir=’.’includes=’
/*.war’/>

</delete>

<!–needxmlfile 设为 false 才不会报错 web.xml 不存在 ant 会报错–>

<war destfile=’${war.name}.war’needxmlfile=’false’>

<!– <lib dir=’${basedir}/WebRoot/WEB-INF/lib’/> –>

<classes dir=’${buildwar.dest}/WEB-INF/classes’excludes=’${war.exclude.classes}’/>

<fileset dir=’${webapp.dir}’excludes=’${war.exclude}’/>

</war>

</target>

<!– =================================================================== –>

上传本地文件到远程服务器,执行远程命令

<!– =================================================================== –>

<target name=’ssh’depends=’war’>

<echo message=’————————————-‘/>

<echo message=’start ssh ${project.name} – ${DSTAMP}${TSTAMP}’/>

<!–上传–>

<scp file=’${basedir}/${war.name}.war’todir=’${ssh.uname}:${ssh.pwd}@${ssh.host}:${ssh.path}’trust=’true’/>

</target>

<!– =================================================================== –>

<!– 停止服务器–>

<!– =================================================================== –>

<target name=’stop’depends=’ssh’>

<echo message=’————————————-‘/>

<echo message=’stop server – ${DSTAMP}${TSTAMP}’/>

<sshexec host=’${ssh.host}’username=’${ssh.uname}’password=’${ssh.pwd}’port=’22’trust=’true’verbose=’true’command=’${ssh.server.stop}’/>

</target>

<!– =================================================================== –>

<!– 启动服务器–>

<!– =================================================================== –>

<target name=’start’depends=’stop’>

<echo message=’————————————-‘/>

<echo message=’start server – ${DSTAMP}${TSTAMP}’/>

<sshexec host=’${ssh.host}’username=’${ssh.uname}’password=’${ssh.pwd}’port=’22’trust=’true’verbose=’true’command=’${ssh.server.start}’/>

</target>

<!– =================================================================== –>

<!– 清除临时文件–>

<!– =================================================================== –>

<target name=’clean’depends=’init’>

<echo message=’————————————-‘/>

<echo message=’start clean ${project.name} – ${DSTAMP}${TSTAMP}’/>

<delete includeemptydirs=’true’>

<fileset dir=’${basedir}’excludes=’ant.properties,build.xml’/>

</delete>

</target>

</project>

[/sourcecode]

注释写的还算详细哈!

ANT 的配置网上一大堆,google 一下就知道了(有时宁愿用有道,也不愿意用百度)。

想起之前 svn 下载、编译、上传、部署全部手动操作,唉!

现在,一个命令搞定,好舒服!

构建工具,还有另外一种选择,使用 MAVEN,MAVEN 更是一种更优秀的构建工具,也推荐学习一下的。

多学习一种是好事哈!不能总用过 myeclipse 就行吧!目前我的系统采用的是 linuxdeepin,基本都能满足我的日常使用了,下次有机会再介绍我的 linux 经历史了。

正文完
 
zhaopeng
版权声明:本站原创文章,由 zhaopeng 2012-08-01发表,共计6195字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)