对前一篇的优化,实现单脚本执行成功即备份
Jenkins基础篇 系列之-—07 实现SQL脚本批量执行
Ant知识
官网说明
在ant中,无法实现常见的字符重操作,比如截取、替换。我们可以借助ant-contrib中的propertyregex任务实现字符串的截取、替换
1、字符串的截取
<!--${file.separator} 文件分割符 ,windows \ ,linux / -->
<propertyregex property="result"
input="D:\apache-ant-1.9.6-bin\lib\a.txt"
regexp="([^\*]*)\${file.separator}"
select="\1"
casesensitive="false" />
<echo>文件绝对路径:${result}</echo>
<propertyregex property="result"
input="D:\apache-ant-1.9.6-bin\lib\a.txt"
regexp="[^\${file.separator}]+$" select="\0"
casesensitive="false"/>
<echo>文件名:${result}</echo>
2、字符串的替换
比如:替换字符串root:password@127.0.0.1为root:pwd@127.0.0.1
<propertyregex property="${svr1}" input="${svr}" regexp=‘password‘ replace="pwd"/>
Target优化
<target name="runSqlInFolder">
<echo>Run the SQL at Folder: ${sqlfolder}</echo>
<echo>DB Host: ${v7testdb.host}</echo>
<echo>DB Name: ${v7testdb.name}</echo>
<echo>DB User: ${v7testdb.user}</echo>
<trycatch property="errMsg">
<try>
<for param="file">
<path>
<sort xmlns:rcmp="antlib:org.apache.tools.ant.types.resources.comparators">
<fileset dir="${sqlfolder}" includes="*/*/*.sql" casesensitive="false"/>
</sort>
</path>
<sequential>
<echo>SQL: @{file}</echo>
<propertyregex override="yes" property="sqlFolderName" input="@{file}"
regexp="${sqlfolder}${file.separator}([^\*]*)\${file.separator}" select="\1"
casesensitive="false" />
<propertyregex override="yes" property="sqlFileName" input="@{file}"
regexp="[^\${file.separator}]+$" select="\0"
casesensitive="false"/>
<execsql
dbhost="${v7testdb.host}"
dbport="${v7testdb.port}"
dbname="${v7testdb.name}"
dbuser="${v7testdb.user}"
dbpwd="${v7testdb.pwd}"
sqlfile="@{file}"
logfile="${Sqllogfile}"/>
<move file="@{file}" todir="${sqlbakdir}${file.separator}${sqlFolderName}"/>
</sequential>
</for>
<echo>Finished running all SQL</echo>
<echo>Files moved to backup folder:${sqlbakdir}</echo>
</try>
<catch>
<echo>Error found when running SQL</echo>
<echo>Log file can be found in:</echo>
<echo>${sqlbakdir}/err</echo>
<move file="${Sqllogfile}" todir="${sqlbakdir}/err"/>
<fail>Error Occur</fail>
</catch>
<finally>
</finally>
</trycatch>
</target>