maven:sql-maven-plugin插件执行sql脚本
来源:互联网 发布:最好用的p2p软件 编辑:程序博客网 时间:2024/06/06 00:53
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’的executionmvn -f run-sql.xml sql:execute@clean-tables# 通过@execution-id的方式指定执行id为‘create-tables’的executionmvn -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》
《execute-mojo》
《Maven Properties Guide》
阅读全文
0 0
- maven:sql-maven-plugin插件执行sql脚本
- Maven插件之sql-maven-plugin
- exec-maven-plugin(Maven自动执行批处理脚本的插件,可用于复制WAR包到Tomcat)
- cobertura-maven-plugin插件 maven
- maven插件 buildnumber-maven-plugin
- Maven插件:maven-javadoc-plugin
- eclipse maven plugin 插件
- maven-surefire-plugin插件
- maven-compiler-plugin插件
- 插件maven-compiler-plugin
- Maven-assembly-plugin插件
- maven-antrun-plugin插件
- 使用appassembler-maven-plugin插件生成启动脚本
- maven插件不执行-build-helper-maven-plugin pom中不执行
- Maven的tomcat插件tomcat-maven-plugin
- Maven插件之buildnumber-maven-plugin
- Maven插件之maven-assembly-plugin
- Maven插件之Maven-Enforcer-Plugin
- 【C语言】【unix c】什么是标准数据流
- Python中定制类的比较运算方法
- 记一个idea下MAVEN项目的坑
- 安装Ubuntu双系统问题记录
- bzoj1212 [HNOI2004]L语言(Trie+暴力)
- maven:sql-maven-plugin插件执行sql脚本
- 一个乒乓球引发的一系列问题(穷举法)
- Vuejs之路之--循环渲染
- Vue之过度(动画)
- 【C语言】【unix c】GDB调试工具的使用
- [CEOI2011] balloons 单调栈
- 嵌套评论的数据库表设计
- php和数据库mysql的一些简单交互
- python简单脚本1