Executing SQL

The SQL Maven plugin sends SQL statements to the RDBMS of your choice.

In the example the data set required for running the Unit Test cases is created before the execution of the test phase.

<!-- execute SQL statements -->
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>sql-maven-plugin</artifactId>

                <dependencies>
                    <!-- specify the dependent jdbc driver here -->
                    <dependency>
                        <groupId>com.oracle</groupId>
                        <artifactId>ojdbc14</artifactId>
                        <version>10.2.0.2.0</version>
                        <scope>test</scope>
                    </dependency>
                    <dependency>
                        <groupId>postgresql</groupId>
                        <artifactId>postgresql</artifactId>
                        <version>8.2-504.jdbc3</version>
                        <scope>test</scope>
                    </dependency>
                </dependencies>

                <!-- common configuration shared by all executions -->
                <configuration>
                    <username>${db-username}</username>
                    <password>${db-password}</password>
                    <url>${db-url}</url>
                    <driver>${db-driver}</driver>
                </configuration>

                <executions>
                    <!-- Create unittest testdata -->
                    <execution>
                        <id>create-data</id>
                        <phase>process-test-resources</phase>
                        <goals>
                            <goal>execute</goal>
                        </goals>
                        <configuration>
                            <srcFiles>
                                <srcFile>${sql-file}</srcFile>
                            </srcFiles>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

Few remarks:

  • 2 dependencies (Oracle and PostgreSQL drivers) are configured because my project was being tested with the 2 databases
  • configuration settings are defined in the profiles.xml file
  • the execution runs the sql contained in an external file (property ${sql-file})

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: