利用ant执行dos2unix
在实际的javaweb开发中,我们往往需要用到Ant打包发布,而我目前所在的项目组却无法完全由Ant打包,原因在于我们需要对某些配置文件和脚本进行转码并赋权限,所有只好利用Ant编译好后再手动的将包传到linux服务器上进行dos2unix再chmod.
今天研究了一下Ant,使用其中的replaceregexp任务将windows平台的\r\n替换为\n,代码如下:
<target name="dos2unix" description="dos2unix"> <replaceregexp match=" " replace=" " flags="g" byline="false"> <fileset dir="${base}"> <include name="bin/**" /> <include name="config/**" /> </fileset> </replaceregexp> </target>
如下赋权限的代码目前没有环境测试,不知可不可用,测试后我会再补充说明:
<target name="chmod" depends="dos2unix" description="chmod 777"> <chmod dir="${base}/bin" perm="ugo+rwx"/> </target>
以上的赋权限只能在unix平台上进行,所以如果要在windows上赋权限是一件很麻烦的事情,不过还好,ant提供了zip和telnet,ftp的task,所以我们只要把在windows上打好的包用ftp方式放到服务器上再解压,执行chmod命令在压缩成gz包,最后将这个gz包ftp到本机就好了,主要代码如下:
<!--默认打包服务器--> <property name="server_address" value="10.10.10.10" /> <!--登陆用户--> <property name="login_name" value="root" /> <!--登陆用户秘码--> <property name="login_pwd" value="password" /> ... <!-- 如果无服务器可以直接在本地打zip包 --> <target name="zipFile" depends="delete"> <delete file="${base}/${ins_package}.zip" /> <zip zipfile="${base}/${ins_package}.zip" basedir="${base}/${package_temp}" includes="**" /> </target> <!-- 初始化服务器目录 --> <target name="initServer" depends="zipFile"> <telnet userid="${login_name}" password="${login_pwd}" server="${server_address}"> <read>${read_rsp}</read> <write echo="true">rm -rf ${package_name}</write> <read>${read_rsp}</read> <write echo="true">mkdir ${package_name}</write> <read>${read_rsp}</read> <write echo="true">cd ${package_name}</write> <read>${read_rsp}</read> </telnet> </target> <!-- 将本地zip文件ftp到服务器指定目录下 --> <target name="ftpServer" depends="initServer"> <ftp server="${server_address}" remotedir="${package_name}" userid="${login_name}" password="${login_pwd}"> <fileset file="${base}/${ins_package}.zip" /> </ftp> </target> <!-- 在服务器指定目录下对工程进行dos2unix,chmod,以及打包 --> <target name="buildGzip" depends="ftpServer"> <telnet userid="${login_name}" password="${login_pwd}" server="${server_address}"> <read>${read_rsp}</read> <write echo="true">cd ${package_name}</write> <read>${read_rsp}</read> <write echo="true">unzip ${ins_package}.zip</write> <read>${read_rsp}</read> <write echo="true">rm -rf ${ins_package}.zip</write> <read>${read_rsp}</read> <write echo="true">cd bin</write> <read>${read_rsp}</read> <write echo="true">dos2unix *</write> <read>${read_rsp}</read> <write echo="true">chmod 777 *</write> <read>${read_rsp}</read> <write echo="true">cd ../config</write> <read>${read_rsp}</read> <write echo="true">dos2unix *</write> <read>${read_rsp}</read> <write echo="true">chmod 777 *</write> <read>${read_rsp}</read> <write echo="true">cd ../</write> <read>${read_rsp}</read> <write echo="true">tar -zcvf ${ins_package}.tar.gz ./*</write> <read>${read_rsp}</read> </telnet> </target> <!-- 将服务器指定目录下打好的gz包下载到本地 --> <target name="downloadLocation" depends="buildGzip"> <delete file="${base}/${ins_package}.tar.gz" /> <ftp action="get" server="${server_address}" userid="${login_name}" password="${login_pwd}" remotedir="${package_name}"> <fileset file="${ins_package}.tar.gz" /> </ftp> <!-- 删除本地临时zip包 --> <delete file="${base}/${ins_package}.zip" /> </target>
相关推荐
BitsPlayer 2020-05-29
roygbip 2020-02-16
BitsPlayer 2020-02-13
lrcoop 2020-02-03
chichichi0 2020-01-06
BitsPlayer 2019-12-19
BitsPlayer 2019-12-14
xueliangEmail 2013-07-22
cailianren 2010-04-15
wugang0 2019-11-09
lustdevil 2011-08-28
evilvoid 2008-04-17
leonranri 2019-11-05
leeccr 2019-10-31
evilvoid 2010-02-02