sqk-maven-plugin 插件样例

1.创建一个maven插件,插件中执行某个输出语句表示插件创建成功。

2修改pom.xml,加载sql-maven-plugin,载入sql文件,成功运行sql文件。

用处:sql-maven-plugin与CI创建持续性数据库集成工具。自动执行sql文件



转载的详解:

sql-maven-plugin插件提供了sql脚本的执行功能,允许用户执行指定的sql脚本文件或语句。

最近在进行一个项目是基于maven管理的java开发项目,其中有一个环节要对数据库初始化创建表,需要在maven中执行,正好有机会学习了sql-maven-plugin的使用.

关于sql-maven-plugin的详细说明参见http://www.mojohaus.org/sql-maven-plugin

下面的maven脚本实现的功能就是在mysql数据库中执行指定的sql脚本(create_tables.sql)来创建表:

run-sql.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project xmlns="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>yourGroupId</groupId>
  <artifactId>yourArtifactId</artifactId>
  <!--这里package不能使用默认的jar,否则不会执行插件-->
  <packaging>maven-plugin</packaging>
  <name>facelog-sql</name>
  <build>
    <plugins>        
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>sql-maven-plugin</artifactId>
        <version>1.5</version>
        <dependencies>
          <!-- 定义依赖的数据库驱动jar包(mysql) -->
          <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.43</version>
          </dependency>  
        </dependencies>
        <configuration>
          <!-- 定义数据库连接参数 -->
          <driver>com.mysql.jdbc.Driver</driver>
          <url>jdbc:mysql://localhost:3306/test</url>
          <username>root</username>
          <password></password>
          <!-- 指定要执行的sql脚本 'sql'文件夹为脚本所在文件夹下的子文件夹 -->
          <srcFiles>
              <srcFile>${project.basedir}/sql/create_tables.sql</srcFile>
          </srcFiles>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38

运行方式如下:

# 因为上面的脚本我没有使用缺省的文件名pom.xml,所以maven执行的时候要用-f 指定文件名
mvn -f run-sql.xml sql:execute
 
 
  • 1
  • 2

定义多个独立执行的execution

上面的脚本可以一次性执行一个或多个sql脚本,如果我们希望每个脚本可以在命令行分别独立执行,那么就要定义多个execution来实现。 
比如我们将删除表的语句和建表语句分成两个文件(clean_tables.sql,create_tables.sql),希望在命令行分别执行两个脚本,那么 上面脚本就修改成如下的样子:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project xmlns="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>net.gdface.facelog</groupId>
  <artifactId>facelog-sql</artifactId>
  <!--这里package不能使用默认的jar,否则不会执行插件-->
  <packaging>maven-plugin</packaging>
  <name>facelog-sql</name>
  <build>
    <plugins>        
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>sql-maven-plugin</artifactId>
        <version>1.5</version>
        <dependencies>
          <!-- 定义依赖的数据库驱动jar包(mysql) -->
          <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.43</version>
          </dependency>  
        </dependencies>
        <configuration>
          <!-- 定义数据库连接参数 -->
          <driver>com.mysql.jdbc.Driver</driver>
          <url>jdbc:mysql://localhost:3306/test</url>
          <username>root</username>
          <password></password>
        </configuration>
        <executions>
          <!-- 删除表操作 -->
          <execution>
            <id>clean-tables</id>
            <configuration>
              <srcFiles>
                <srcFile>${project.basedir}/sql/clean_tables.sql</srcFile>
              </srcFiles>
            </configuration>
          </execution> 
          <!-- 创建表操作 -->
          <execution>
            <id>create-tables</id>
            <configuration>
              <srcFiles>
                <srcFile>${project.basedir}/sql/create_tables.sql</srcFile>
              </srcFiles>
            </configuration>
          </execution> 
        </executions>
      </plugin>
    </plugins>
  </build>
</project>
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54

命令行执行如下:

# 通过@execution-id的方式指定执行id为‘clean-tables’的execution
mvn -f run-sql.xml sql:execute@clean-tables
# 通过@execution-id的方式指定执行id为‘create-tables’的execution
mvn -f run-sql.xml sql:execute@create-tables
 
 
  • 1
  • 2
  • 3
  • 4

注意 Maven 3.3.1以上版本支持上述的@execution-id 的用法 
参见 https://stackoverflow.com/questions/3166538/how-to-execute-maven-plugin-execution-directly-from-command-line

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值