tags:

views:

71

answers:

2

I have a bunch of SQL commands bound to the pre-integration-test phase whose job is to create a "test" database and point the application to it.

Sometimes, I want to just "rebuild" my test database without all the other stuff in the lifecycle. For example, if my test is catastrophically failing and screwing up the test database, I may have to rebuild it several times until I figure out what the problem is.

Here's what my POM looks like:

<profile>
    <id>test-setup-teardown</id>
       <activation>
           <activeByDefault>true</activeByDefault>
       </activation>
       <build>
           <plugins>
               <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>sql-maven-plugin</artifactId>
                    <version>1.3</version>

                    <dependencies>
                        <dependency>
                            <groupId>${database-dependency-groupId}</groupId>
                            <artifactId>${database-dependency-artifactId}</artifactId>
                            <version>${database-dependency-version}</version>
                        </dependency>
                    </dependencies>

                    <configuration>
                        <url>${test-database-admin-url}</url>
                        <username>${test-database-admin-username}</username>
                        <password>${test-database-admin-password}</password>
                        <driver>${database-driver}</driver>
                        <autocommit>true</autocommit>
                    </configuration>

                    <executions>
                        <execution>
                            <id>test-database-pre-setup</id>
                            <phase>pre-integration-test</phase>
                            <goals>
                                <goal>execute</goal>
                            </goals>
                            <configuration>
                                <sqlCommand>${test-database-teardown}</sqlCommand>
                                <onError>continue</onError>
                            </configuration>
                        </execution>

                        <execution>
                            <id>test-database-setup</id>
                            <phase>pre-integration-test</phase>
                            <goals>
                                <goal>execute</goal>
                            </goals>
                            <configuration>
                                <sqlCommand>${test-database-setup}</sqlCommand>
                            </configuration>
                        </execution>

                        <execution>
                            <id>test-database-schema</id>
                            <phase>pre-integration-test</phase>
                            <goals>
                                <goal>execute</goal>
                            </goals>
                            <configuration>
                                <url>${test-database-url}</url>
                                <username>${database-user}</username>
                                <password>${database-password}</password>
                                <srcFiles>
                                    <srcFile>${basedir}/metadata/build/database/${database-engine}/appx.sql</srcFile>
                                </srcFiles>
                            </configuration>
                        </execution>

                        <execution>
                            <id>test-database-teardown</id>
                            <phase>post-integration-test</phase>
                            <goals>
                                <goal>execute</goal>
                            </goals>
                            <configuration>
                                <sqlCommand>${test-database-teardown}</sqlCommand>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>

How can I run all the executions of this profile? Something like mvn sql:execute only runs one of the executions (the last, I think).

I've tried making the bound phase be a property, and then allow the user to specify another profile which changes the default from pre-integration-test to validate, but explaining to someone why rebuilds are executed thusly:

mvn validate -Pforce-rebuild,test-setup-teardown

simply enforces the fact that non-toy projects have a lot of magic in the POM. Please, show me the way!

<ed>Maybe a good solution would be a way to run executions by id from the command line?</ed>

A: 

"pre-integration-test" is a phase you can invoke directly.

mvn -Ptest-setup-teardown pre-integration-test

This will still go through the lifecycle of compile, copy-resources, unit tests, etc. You could try to add -DskipTests=true to the command (not sure if this will skip integration-test phase too):

mvn -Ptest-setup-teardown pre-integration-test -DskipTests=true
matt b
I'm specifically trying to avoid going through the lifecycle as it takes 3-4 minutes to get to pre-integration-test. I've tried adding all the plug-ins to profiles so they may be easily turned off, but this adds a lot of complication when trying to have branching behaviour (i.e. sometimes do this, sometimes do that, etc.).
Monkey Boson
this kind of sounds like the type of task which is best done outside of maven, since it really doesn't have much to do with building your main app
matt b
+1  A: 

Maybe a good solution would be a way to run executions by id from the command line?

This is not possible. Executions are for binding to the lifecycle.

And calling sql:execute and passing all the parameters on the command line doesn't seem to be a viable option (this could have been done in a batch script but there will be a problem with the srcFiles optional parameter anyway).

So, in my opinion, the "less worse" option would be to duplicate (sigh) the sql-maven-plugin setup in another profile, for example rebuild, to bind the desired executions on the validate phase and to declare the validate goal as default goal in this profile. This would allow to type something like this:

mvn -Prebuild

Maybe some optimizations are possible to avoid duplication (by variablelizing the execution phase like you did) but this means more black magic to me (which might not be desirable).

Pascal Thivent
Good call on the default goal. I don't like the duplication either, but agree that it's probably the least-bad solutions. Thanks!
Monkey Boson