MAVEN execution SQL statement

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.