Executions

The following build configuration shows how to drop/create a database and schema, then populate it before the test phase, and drop the database after the test phase.

<project>
  [...]
  <build>
    [...]
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>sql-maven-plugin</artifactId>
        <version>1.5</version>

        <dependencies>
          <!-- specify the dependent jdbc driver here -->
          <dependency>
            <groupId>postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>8.1-407.jdbc3</version>
          </dependency>
        </dependencies>

        <!-- common configuration shared by all executions -->
        <configuration>
          <driver>org.postgresql.Driver</driver>
          <url>jdbc:postgressql://localhost:5432:yourdb</url>
          <username>postgres</username>
          <password>password</password>
          <!-- You can comment out username/password configurations and
               have maven to look them up in your settings.xml using ${settingsKey}
          -->
          <settingsKey>sensibleKey</settingsKey>
          <!--all executions are ignored if -Dmaven.test.skip=true-->
          <skip>${maven.test.skip}</skip>
        </configuration>

        <executions>
          <execution>
            <id>drop-db-before-test-if-any</id>
            <phase>process-test-resources</phase>
            <goals>
              <goal>execute</goal>
            </goals>
            <configuration>
              <!-- need another database to drop the targeted one -->
              <url>jdbc:postgressql://localhost:5432:bootstrapdb</url>
              <autocommit>true</autocommit>
              <sqlCommand>drop database yourdb</sqlCommand>
              <!-- ignore error when database is not avaiable -->
              <onError>continue</onError>
            </configuration>
          </execution>

          <execution>
            <id>create-db</id>
            <phase>process-test-resources</phase>
            <goals>
              <goal>execute</goal>
            </goals>
            <configuration>
              <url>jdbc:postgressql://localhost:5432:yourdb</url>
              <!-- no transaction -->
              <autocommit>true</autocommit>
              <sqlCommand>create database yourdb</sqlCommand>
            </configuration>
          </execution>

          <execution>
            <id>create-schema</id>
            <phase>process-test-resources</phase>
            <goals>
              <goal>execute</goal>
            </goals>
            <configuration>
              <autocommit>true</autocommit>
              <srcFiles>
                <srcFile>src/main/sql/your-schema.sql</srcFile>
              </srcFiles>
            </configuration>
          </execution>

          <execution>
            <id>create-data</id>
            <phase>process-test-resources</phase>
            <goals>
              <goal>execute</goal>
            </goals>
            <configuration>
              <orderFile>ascending</orderFile>
              <fileset>
                <basedir>${basedir}</basedir>
                <includes>
                  <include>src/test/sql/test-data2.sql</include>
                  <include>src/test/sql/test-data1.sql</include>
                </includes>
              </fileset>
            </configuration>
          </execution>

          <!-- drop db after test -->
          <execution>
            <id>drop-db-after-test</id>
            <phase>test</phase>
            <goals>
              <goal>execute</goal>
            </goals>
            <configuration>
              <autocommit>true</autocommit>
              <sqlCommand>drop database yourdb</sqlCommand>
            </configuration>
          </execution>
        </executions>
      </plugin>
      [...]
    </plugins>
    [...]
  </build>
  [...]
</project>