• Jenkins+ant+mysql 自动化构建脚本文件输出日志


            相信大多数金融行业的都有遇到过,想通过最简洁的方式去执行响应的脚本文件,比如测试人员、运维人员可以通过Jenkins进行构建SQL执行器,就能将SQL文件进行执行,并且输出相应的日志。

            通过这种方式,开发人员只需将SQL文件,放在指定的目录,测试人员只需要在Jenkins中进行构建执行就行。

            这套方案我在网上找了很多文章,都没有给出完整的Demo,都是一些代码块。我相信有些同学是不动ant,并且不懂build.xml的相关节点,那么今天我把我的搭建经验记录下来,并且与大家分享,不喜勿喷。

            涉及的相关JAR包如下:

                    ant-contrib-1.0b3.jar

                    mysql-connector-java-8.0.15.jar

                    apache-ant-1.9.16-bin.zip

                    下载地址:阿里云盘分享

                    Ant下载地址:Apache Ant - Binary Distributions

     

    1)编写build.xml

            新建一个build.xml

    1. "1.0"?>
    2. <project xmlns='antlib:org.apache.tools.ant'>
    3. <taskdef resource="net/sf/antcontrib/antlib.xml" classpath="ant-contrib-1.0b3.jar"/>
    4. <path id="project.class.path">
    5. <fileset dir="/dclife/mobile_team_sql/map/ant-script/lib" includes="*.jar" />
    6. path>
    7. <property name="encoding" value="UTF-8" />
    8. <property file="build.properties" />
    9. <target name="runSqlInFolder">
    10. <echo>Run the SQL at Folder: ${sqlfolder}echo>
    11. <echo>DB URL: ${db.url}echo>
    12. <echo>DB User: ${db.user}echo>
    13. <trycatch property="errMsg">
    14. <try>
    15. <for param="folder">
    16. <path>
    17. <sort xmlns:rcmp="antlib:org.apache.tools.ant.types.resources.comparators">
    18. <dirset dir="${sqlfolder}" includes="*" />
    19. sort>
    20. path>
    21. <sequential>
    22. <echo>SQL Folder: @{folder}echo>
    23. <for param="file">
    24. <path>
    25. <sort xmlns:rcmp="antlib:org.apache.tools.ant.types.resources.comparators">
    26. <fileset dir="@{folder}" includes="*.sql" casesensitive="false"/>
    27. sort>
    28. path>
    29. <sequential>
    30. <echo>SQL: @{file}echo>
    31. <execsql
    32. dbUrl="${db.url}"
    33. dbuser="${db.user}"
    34. dbpwd="${db.pwd}"
    35. sqlfile="@{file}"
    36. logfile="${sqllogdir}/${sqllogfile}_info.log"/>
    37. sequential>
    38. for>
    39. <move file="@{folder}" todir="${sqlbakdir}"/>
    40. sequential>
    41. for>
    42. <echo>Finished running all SQLecho>
    43. <echo>File moved to backup folder:echo>
    44. <echo>${sqlbakdir}echo>
    45. try>
    46. <catch>
    47. <echo>Error found when running SQLecho>
    48. <echo>Log file can be found in:echo>
    49. <echo>${sqllogdir}echo>
    50. <move file="${sqllogfile}_info.log" todir="${sqllogdir}"/>
    51. <fail>Error Occurfail>
    52. catch>
    53. <finally>
    54. finally>
    55. trycatch>
    56. target>
    57. <macrodef name="execsql" description="Run single SQL file.">
    58. <attribute name="dbUrl" description="Host Name/ IP of the DB"/>
    59. <attribute name="dbuser" description="DB User name"/>
    60. <attribute name="dbpwd" description="DB Password"/>
    61. <attribute name="sqlfile" description="SQL file to be run"/>
    62. <attribute name="logfile" default="sql.log" description="Log file"/>
    63. <sequential>
    64. <echo>Log file @{logfile}echo>
    65. <record name="@{logfile}" action="start"/>
    66. <sql driver="${driver}"
    67. url="@{dbUrl}"
    68. userid="@{dbuser}"
    69. password="@{dbpwd}"
    70. encoding="${encoding}"
    71. src="@{sqlfile}"
    72. classpathref="project.class.path"
    73. >
    74. sql>
    75. <record name="@{logfile}" action="stop"/>
    76. sequential>
    77. macrodef>
    78. project>

    2)创建build.properties

    1. db.url=jdbc:mysql://IP地址:3306/库名?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&useSSL=false
    2. db.user=数据库用户
    3. db.pwd=数据库密码
    4. driver=com.mysql.cj.jdbc.Driver
    5. Sqllogfile=logs/info.log

    3)Linux安装Ant

            将下载好的apache-ant-1.9.16-bin.zip的压缩包,上传至linux服务器,并且解压该文件夹

            unzip   apache-ant-1.9.16-bin.zip

     4)jenkins创建项目

            

    5)添加Invoke Ant

             6)执行结果

  • 相关阅读:
    UNIAPP实战项目笔记42 购物车页面新增收货地址
    HTML做一个简单漂亮的宠物网页(纯html代码)
    Java Cannot deserialize instance of `xxx` out of START_ARRAY token错误分析及解决
    C++ const关键字
    MySQL与Oracle数据库通过系统命令导出导入
    【机器学习】红酒数据集和加利福尼亚的房价数据的随机森林算法详解
    用C#开发Excel插件的强大开源工具
    Mysql索引分类及其使用实例
    知乎日报第四周总结
    晶圆代工由一家独大,到三足鼎立,再到群雄涿鹿,到底经历了什么?
  • 原文地址:https://blog.csdn.net/qq_30644579/article/details/127123707