【问题标题】:Maven SQL plugin: execute scripts conditionallyMaven SQL 插件:有条件地执行脚本
【发布时间】:2015-04-02 08:50:08
【问题描述】:

在 Java 项目中,我使用 SQL Maven 插件来执行具有给定配置文件的 SQL 脚本。这是我的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>

这很好用。我通过mvn sql:execute -Pimport_oracle执行它。

现在我想做的是使用它们的文件/文件夹名称有条件地执行这些 SQL 脚本。通常,我的项目中有这种结构,其中文件夹的名称对应于我的项目版本:

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

如果在我的项目的 pom 中我有 &lt;version&gt;2.5.0&lt;/version&gt;,我希望 SQL Maven 插件执行前 3 个脚本(因为它们的文件夹名称是 2.5.0

我可以使用 SQL Maven 插件实现这一点吗?我在doc 中看不到任何有用的东西。蚂蚁的任务会是更好的解决方案吗?

【问题讨论】:

    标签: java maven pom.xml sql-maven-plugin


    【解决方案1】:

    您可以使用 ${project.version} 但这只会匹配 V2.5.0

    但我建议使用 Flyway 或 Liquibase 等数据库迁移工具

    【讨论】:

    • 感谢您的回答。 Liquibase 似乎是一个很好的解决方案,但却是一个“重”的解决方案。由于我的需求很简单,我真的很想用 maven/ant 来执行此操作
    • 我终于接受你的回答了。经过几个月寻找一个简单的解决方法(甚至是我的 pom 中的 groovy 脚本),我终于采用了 flywaydb。根据我的帖子中表达的需要配置它很容易,但它是一个大吃货:(
    猜你喜欢
    • 1970-01-01
    • 2013-12-27
    • 2012-12-08
    • 1970-01-01
    • 2022-08-23
    • 1970-01-01
    • 1970-01-01
    • 2014-04-03
    • 2018-03-11
    相关资源
    最近更新 更多