目标:

如何把Jenkins和SQL Maven Plugin 持续集成 Mysql

环境:

  • Jenkins

  • Maven

  • Mysql


插件:

  • SQL Maven plugin


需要的步骤:

  • Jenkins,添加一个Maven Project.

  • 编写pom.xml文件

  • 编写样例mysql文件

  • 构建Maven工程


一、Jenkins添加一个Maven Project。如图所示:

数据库持续集成(CDBI):Jenkins与SQL Maven Plugin之Mysql应用_第1张图片

二、编写Pom.xml文件

-bash-4.1$cat pom.xml

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0


 com.hmkcode

maven-sql-plugin

 1.0-SNAPSHOT

 jar


 maven-sql-plugin

http://maven.apache.org


 

  UTF-8

 


 

   

     junit

    junit

     4.11

     test

   

 


     

      org.codehaus.mojo

      sql-maven-plugin

       1.5


       

               

                      mysql

                      mysql-connector-java

                      5.1.9

               

       


       

        com.mysql.jdbc.Driver

        jdbc:mysql://mysqlserver:3306/test

         root

        123@qwe

       


       

         

           drop-table

          process-test-resources

           

             execute

           

           

            true

             DROP TABLE IFEXISTS person

           

         


         

           create-table

          process-test-resources

           

             execute

           

           

            true

             

              src/main/sql/schema.sql

             

           

         


         

           insert-data

          process-test-resources

           

             execute

           

           

    ascending

             

${basedir}

               

                src/main/sql/test-data.sql

               

             

           

         

       

     

   

 


三、编写Mysql脚本样例

  • schema.sq

-bash-4.1$cat schema.sql

CREATETABLE person (id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(255));

  • test-data.sql

-bash-4.1$cat test-data.sql

INSERTINTO person (name) VALUES ('person-1');

INSERTINTO person (name) VALUES ('person-2');

INSERTINTO person (name) VALUES ('person-3');

INSERTINTO person (name) VALUES ('person-4');

INSERTINTO person (name) VALUES ('person-5');

INSERTINTO person (name) VALUES ('person-6');

四、构建Jenkins工程

Console Output

Startedby user hongjun
Building in workspace/usr/share/tomcat6/.jenkins/jobs/maven_mysql_test/workspace
Parsing POMs
[workspace] $ /opt/jdk-1.6.0_26//bin/java -Xmx512m -XX:MaxPermSize=256m -cp/usr/share/tomcat6/.jenkins/plugins/maven-plugin/WEB-INF/lib/maven31-agent-1.4.jar:/usr/local/maven/boot/plexus-classworlds-2.5.1.jar:/usr/local/maven/conf/loggingjenkins.maven3.agent.Maven31Main /usr/local/maven/var/lib/tomcat6/webapps/jenkins/WEB-INF/lib/remoting-2.33.jar/usr/share/tomcat6/.jenkins/plugins/maven-plugin/WEB-INF/lib/maven31-interceptor-1.4.jar/usr/share/tomcat6/.jenkins/plugins/maven-plugin/WEB-INF/lib/maven3-interceptor-commons-1.4.jar50842
<===[JENKINS REMOTING CAPACITY]===>channel started
log4j:WARN No appenders could be found for logger(org.apache.commons.beanutils.converters.BooleanConverter).
log4j:WARN Please initialize the log4j system properly.
Executing Maven:  -B -f/usr/share/tomcat6/.jenkins/jobs/maven_mysql_test/workspace/pom.xmlinstall
[INFO] Scanning for projects...
[INFO]                                                                        
[INFO] ------------------------------------------------------------------------
[INFO] Building maven-sql-plugin 1.0-SNAPSHOT
[INFO]------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources(default-resources) @ maven-sql-plugin ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory/usr/share/tomcat6/.jenkins/jobs/maven_mysql_test/workspace/src/main/resources
[INFO]
[INFO] --- maven-compiler-plugin:2.5.1:compile(default-compile) @ maven-sql-plugin ---
[INFO] No sources to compile
[INFO]
[INFO] ---maven-resources-plugin:2.6:testResources (default-testResources) @maven-sql-plugin ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory/usr/share/tomcat6/.jenkins/jobs/maven_mysql_test/workspace/src/test/resources
[INFO]
[INFO] --- sql-maven-plugin:1.5:execute(drop-table) @ maven-sql-plugin ---
[INFO] Executing commands
[INFO] 1 of 1 SQL statements executed successfully
[INFO]
[INFO] --- sql-maven-plugin:1.5:execute(create-table) @ maven-sql-plugin ---
[INFO] Executing file: /tmp/schema.1609113259sql
[INFO] 1 of 1 SQL statements executed successfully
[INFO]
[INFO] --- sql-maven-plugin:1.5:execute(insert-data) @ maven-sql-plugin ---
[INFO] Executing file:/usr/share/tomcat6/.jenkins/jobs/maven_mysql_test/workspace/src/main/sql/test-data.sql
[INFO] 6 of 6 SQL statements executed successfully
[INFO]
[INFO] ---maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @maven-sql-plugin ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test(default-test) @ maven-sql-plugin ---
[INFO] No tests to run.
[JENKINS] Recording test results
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar(default-jar) @ maven-sql-plugin ---
[WARNING] JAR will be empty - no content was marked forinclusion!
[INFO] Building jar:/usr/share/tomcat6/.jenkins/jobs/maven_mysql_test/workspace/target/maven-sql-plugin-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install(default-install) @ maven-sql-plugin ---
[INFO] Installing/usr/share/tomcat6/.jenkins/jobs/maven_mysql_test/workspace/target/maven-sql-plugin-1.0-SNAPSHOT.jarto/usr/share/tomcat6/.m2/repository/com/hmkcode/maven-sql-plugin/1.0-SNAPSHOT/maven-sql-plugin-1.0-SNAPSHOT.jar
[INFO] Installing/usr/share/tomcat6/.jenkins/jobs/maven_mysql_test/workspace/pom.xml to/usr/share/tomcat6/.m2/repository/com/hmkcode/maven-sql-plugin/1.0-SNAPSHOT/maven-sql-plugin-1.0-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO]------------------------------------------------------------------------
[INFO] Total time: 7.428s
[INFO] Finished at: Mon Apr 28 10:50:05 CST 2014
[INFO] Final Memory: 9M/102M
[INFO]------------------------------------------------------------------------
[JENKINS] Archiving/usr/share/tomcat6/.jenkins/jobs/maven_mysql_test/workspace/pom.xml tocom.hmkcode/maven-sql-plugin/1.0-SNAPSHOT/maven-sql-plugin-1.0-SNAPSHOT.pom
[JENKINS] Archiving/usr/share/tomcat6/.jenkins/jobs/maven_mysql_test/workspace/target/maven-sql-plugin-1.0-SNAPSHOT.jartocom.hmkcode/maven-sql-plugin/1.0-SNAPSHOT/maven-sql-plugin-1.0-SNAPSHOT.jar
channel stopped
Finished: SUCCESS