maven:sql-maven-plugin插件执行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>

运行方式如下:

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

#定义多个独立执行的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>

命令行执行如下:

# 通过@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

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

参考资料:

[《sql-maven-plugin usage》][1]
[《execute-mojo》][2]
[《Maven Properties Guide》][3]
[1]:http://www.mojohaus.org/sql-maven-plugin/usage.html
[2]:http://www.mojohaus.org/sql-maven-plugin/execute-mojo.html
[3]:https://cwiki.apache.org/confluence/display/MAVEN/Maven+Properties+Guide

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

10km

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值