Objective
The reason for using MAVEN to execute SQL statements or files is to ensure that a "clean" project is present in the daily development or testing process, after each build is completed.
How to use
Using MAVEN to execute SQL statements requires a dependency on the Sql-maven-plugin plugin, which starts with a brief introduction to the configuration of the project Pom file:
<build> <finalName> buglife-data-access</finalname> <plugins> <!--Add Sql-maven-plugin plugin--> <plugin> <groupid>org.codehaus.mojo</groupid > < artifactid>sql-maven-plugin</artifactid> <version>1.5</version> <!---because the database used in this instance is MySQL, it relies on MySQL connection driver <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version >5.1.27</version> </dependency> </dependencies> <!--Configure database connection information for this plug-in--> <configuration> <driver>com.mysql.jdbc.driver</ driver> <url>jdbc:mysql://localhost:3306</url> <username>root</ username> <password>root</password> </configuration> <executions> <!--Creating a database--> <execution> <id>create-db</id> < phase>process-test-resources</phase> <goals> <goal>execute</goal> </goals> <configuration> <!--set up automatic commit transaction--> <autocommit>true< /autocommit> <srcfiles> <srcFile>src/main/resources/create_db.sql</srcFile> </srcFiles> </configuration> </execution> <!--Creating a data table--> <execution> <id>create-table</id> <phase> process-test-resources</phase> <goals> <goal>execute</goal> </ goals> <configuration> <autocommit>true< /autocommit> <srcFiles> <srcfile>src/main/resources/ create_table.sql</srcfile> </srcFiles> </configuration> </execution> <!--inserting Data--> <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/resources/init_data.sql</include> </includes> </fileset> </configuration> </execution> </executions> </plugin> </plugins> </build>
All of the above POM files are executed SQL files and do not choose to execute SQL commands, because in order to change the SQL statement can be centralized in a piece of modification, not to easily modify the project file (POM). To execute a SQL command, you can use a statement similar to the following to replace <srcfiles/ >
<sqlcommand>create database ' Webutil ';</sqlcommand>
Of course, another point to note is that <phase> specifies which part of the Maven life cycle to perform, and the description of the MAVEN lifecycle can be consulted http://my.oschina.net/crazyharry/blog/353978
MAVEN execution SQL statement