目标:
如何把Jenkins和SQL Maven Plugin 持续集成 Mysql
环境:
Jenkins
Maven
Mysql
插件:
SQL Maven plugin
需要的步骤:
Jenkins,添加一个Maven Project.
编写pom.xml文件
编写样例mysql文件
构建Maven工程
一、Jenkins添加一个Maven Project。如图所示:
二、编写Pom.xml文件
-bash-4.1$cat pom.xml
<projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.hmkcode</groupId>
<artifactId>maven-sql-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>maven-sql-plugin</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>sql-maven-plugin</artifactId>
<version>1.5</version>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.9</version>
</dependency>
</dependencies>
<configuration>
<driver>com.mysql.jdbc.Driver</driver>
<url>jdbc:mysql://mysqlserver:3306/test</url>
<username>root</username>
<password>123@qwe</password>
</configuration>
<executions>
<execution>
<id>drop-table</id>
<phase>process-test-resources</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<autocommit>true</autocommit>
<sqlCommand>DROP TABLE IFEXISTS person</sqlCommand>
</configuration>
</execution>
<execution>
<id>create-table</id>
<phase>process-test-resources</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<autocommit>true</autocommit>
<srcFiles>
<srcFile>src/main/sql/schema.sql</srcFile>
</srcFiles>
</configuration>
</execution>
<execution>
<id>insert-data</id>
<phase>process-test-resources</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<orderFile>ascending</orderFile>
<fileset>
<basedir>${basedir}</basedir>
<includes>
<include>src/main/sql/test-data.sql</include>
</includes>
</fileset>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
三、编写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