日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

sqk-maven-plugin 插件样例

發布時間:2023/12/10 编程问答 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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

總結

以上是生活随笔為你收集整理的sqk-maven-plugin 插件样例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。