Search code examples
javamavenpom.xmlsql-maven-plugin

Maven SQL plugin: execute scripts conditionally


On a Java project, I use the SQL Maven Plugin to execute SQL scripts with a given profile. Here is a part of my pom.xml :

<profile>
<id>import_oracle</id>
<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>sql-maven-plugin</artifactId>
            <version>1.5</version>
            <dependencies>
                [oracle dependency]
            </dependencies>
            <configuration>
                [oracle conf: username, password, DB url, driver class..]
            </configuration>
            <executions>
                <execution>
                    <id>default-cli</id>
                    <goals>
                        <goal>execute</goal>
                    </goals>
                    <configuration>
                        <srcFiles>
                            <srcFile>sql/datas/V2.0.0/foo.sql</srcFile>
                            ...
                        </srcFiles>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
</profile>

This works well. I execute it through mvn sql:execute -Pimport_oracle.

Now what I would like to do is to execute these SQL scripts conditionnally with their file/folder names. Typically, I have this structure in my project, where folder's name correspond to a version of my project:

sql/datas/V2.0.0/foo.sql
sql/datas/V2.0.0/bar.sql
sql/datas/V2.5.0/helloworld.sql
sql/datas/V3.0.0/stuff.sql

If by example in my project's pom I have <version>2.5.0</version>, I would like the SQL Maven Plugin to execute the first 3 scripts (because their folder name is <= to my project version) but not the last one, because 3.0.0 > 2.5.0

Can I achieve this with the SQL Maven Plugin ? I see nothing in the doc usefull for this. Could a Ant's task be a better solution ?


Solution

  • You could use ${project.version} but this would only match V2.5.0

    But I would recommend using a Database migration tool like Flyway or Liquibase