在Maven中将嵌入式数据库与Flyway和jOOQ结合使用以进行持续集成 [英] Using embedded database with Flyway and jOOQ in Maven for continuous integration

查看:61
本文介绍了在Maven中将嵌入式数据库与Flyway和jOOQ结合使用以进行持续集成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我真的想用连续集成服务器上工作,而不能访问任何基于服务器的数据库.最终,我想将此部署到Amazon,所以我需要一个与之兼容的解决方案PostgreSQL . HSQLDB的文件协议似乎符合要求.

So I'm really trying to do things 'right' with SQL that will break at compile time using flyway and jOOQ. To do this I need a database solution that can work on the continuous integration server with no access to any server-based database. Ultimately, I want to deploy this to Amazon so I need a solution that is mostly compatible with postgreSQL. HSQLDB's file protocol seems to fit that bill.

<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                  http://maven.apache.org/xsd/maven-4.0.0.xsd">

<properties>
    <schema></schema>
    <db.groupId>org.hsqldb</db.groupId>
    <db.artifactId>hsqldb</db.artifactId>
    <db.version>2.3.2</db.version>
    <flyway.url>jdbc:hsqldb:file:myDB/db</flyway.url>
    <flyway.driver>org.hsqldb.jdbcDriver</flyway.driver>
    <jooq.generator.database.name>org.jooq.util.hsqldb.HSQLDBDatabase</jooq.generator.database.name>
    <flyway.user></flyway.user>
    <flyway.password></flyway.password>

</properties>

<dependencies>
    <dependency>
        <groupId>${db.groupId}</groupId>
        <artifactId>${db.artifactId}</artifactId>
        <version>${db.version}</version>
    </dependency>
    <dependency>
        <groupId>org.jooq</groupId>
        <artifactId>jooq</artifactId>
        <version>3.4.2</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.flywaydb</groupId>
            <artifactId>flyway-maven-plugin</artifactId>
            <version>3.1</version>

            <executions>
                <execution>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>migrate</goal>
                    </goals>
                </execution>
            </executions>

            <configuration>
                <locations>
                    <location>filesystem:src/main/resources/db/migration</location>
                </locations>
            </configuration>
        </plugin>
        <plugin>

            <!-- Specify the maven code generator plugin -->
            <groupId>org.jooq</groupId>
            <artifactId>jooq-codegen-maven</artifactId>
            <version>3.4.2</version>

            <!-- The plugin should hook into the generate goal -->
            <executions>
                <execution>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                </execution>
            </executions>

            <!-- Work around Maven's classloader -->
            <dependencies>
                <dependency>
                    <groupId>${db.groupId}</groupId>
                    <artifactId>${db.artifactId}</artifactId>
                    <version>${db.version}</version>
                </dependency>
            </dependencies>

            <configuration>

                <!-- JDBC connection parameters -->
                <jdbc>
                    <driver>${flyway.driver}</driver>
                    <url>${flyway.url}</url>
                    <user>${flyway.user}</user>
                    <password>${flyway.password}</password>
                </jdbc>

                <!-- Generator parameters -->
                <generator>
                    <name>org.jooq.util.DefaultGenerator</name>
                    <database>
                        <name>${jooq.generator.database.name}</name>
                        <includes>.*</includes>
                        <excludes></excludes>
                        <inputSchema>${schema}</inputSchema>
                    </database>
                    <target>
                        <packageName>package.goes.here</packageName>
                        <directory>target/generated-sources/jooq</directory>
                    </target>
                </generator>
            </configuration>
        </plugin>
    </plugins>
</build>

问题是Flyway可以很好地创建数据库,但是当轮到jOOQ使用数据库生成代码时,它将失败,并显示以下信息:

The problem is that Flyway can create the database fine, but when it becomes jOOQ's turn to use the database to generate code, it fails with:

Caused by: org.hsqldb.HsqlException: Database lock acquisition failure: lockFile: org.hsqldb.persist.LockFile@1096ec89[file =...db/db.lck, exists=true, locked=false, valid=false, ] method: checkHeartbeat read: 2015-02-09 03:56:15 heartbeat - read: -863 ms.
at org.hsqldb.error.Error.error(Unknown Source)
at org.hsqldb.error.Error.error(Unknown Source)
at org.hsqldb.persist.LockFile.newLockFileLock(Unknown Source)
at org.hsqldb.persist.Logger.acquireLock(Unknown Source)
at org.hsqldb.persist.Logger.open(Unknown Source)
at org.hsqldb.Database.reopen(Unknown Source)
at org.hsqldb.Database.open(Unknown Source)
at org.hsqldb.DatabaseManager.getDatabase(Unknown Source)
at org.hsqldb.DatabaseManager.newSession(Unknown Source)

问题的核心是 Maven的插件不共享同一类加载器作为POM的其余部分,则必须再次指定JDBC驱动程序依赖项.因此,我得到了该驱动程序的一个新实例,它与已经打开的驱动程序发生了冲突,而Flyway已打开,而不是使用相同的驱动程序.

The core of problem is that Maven's plugins do not share the same classloader as the rest of the POM, and you must specify the JDBC driver dependency a second time. Thus I get a new instance of the driver and it conflicts with the already open driver Flyway has open, instead of using the same one.

因此,解决方案可能存在许多不同的路径:

So a solution may exist along a number of different paths:

  1. Flyway可以正确关闭数据库.显然,Flyway的插件中有一种解决方法,可以读取项目的类路径.
  2. 可以通过某种方式将jOOQ配置为读取项目的类路径.
  3. 可能还有另一个数据库可以更好地工作.
  4. 您!

推荐答案

感谢Thilo的指导.用exec代替Flyway插件,但是我不得不创建一个更简单的命令行客户端使它起作用.

Thanks Thilo for the direction. Replacing the Flyway plugin with exec works, but I had to create a simpler command line client to make it work.

这篇关于在Maven中将嵌入式数据库与Flyway和jOOQ结合使用以进行持续集成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆