带有 flyway 和 testconteiners 的 Jooq 每个 运行 创建新容器

Jooq with flyway and testconteiners creates new container per run

我想将 flyway 与 testcontainers 一起用于 jooq 生成。为此,我有 2 个插件

<plugin>
            <groupId>org.flywaydb</groupId>
            <artifactId>flyway-maven-plugin</artifactId>
            <version>7.14.0</version>
            <executions>
                <execution>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>migrate</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <url>jdbc:tc:postgresql:10://localhost:7432/test</url>
                <user>test</user>
                <password>test</password>
                <driver>
                    org.testcontainers.jdbc.ContainerDatabaseDriver
                </driver>
                <locations>
                    <location>filesystem:src/main/resources/db/migrations</location>
                </locations>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.jooq</groupId>
            <artifactId>jooq-codegen-maven</artifactId>
            <version>3.14.15</version>

            <executions>
                <execution>
                    <id>java-generator</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                </execution>
            </executions>

            <configuration>
                <jdbc>
                    <driver>org.testcontainers.jdbc.ContainerDatabaseDriver</driver>
                    <url>jdbc:tc:postgresql:10://localhost:7432/test</url>
                    <user>test</user>
                    <password>test</password>
                </jdbc>

                <generator>
                    <database>
                        <name>org.jooq.meta.postgres.PostgresDatabase</name>
                        <includes>.*</includes>
                        <inputSchema>public</inputSchema>
                    </database>

                    <generate/>

                    <target>
                        <packageName>com.test</packageName>
                        <directory>target/generated-sources/jooq</directory>
                    </target>
                </generator>
            </configuration>
        </plugin>

所以,我看到 flyway 启动了 tc,应用了所有脚本,然后 jooq 启动了它自己的容器并尝试生成实体,但什么也没有。您能否建议如何处理此问题?

你必须先像这样启动测试容器:

        <!-- Start Testcontainer -->
        <plugin>
            <groupId>org.codehaus.gmaven</groupId>
            <artifactId>groovy-maven-plugin</artifactId>
            <version>2.1.1</version>
            <executions>
                <execution>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>execute</goal>
                    </goals>
                    <configuration>
                        <source>
                            db = new org.testcontainers.containers.PostgreSQLContainer("postgres:12.7")
                                .withUsername("${db.username}")
                                .withDatabaseName("jtaf4")
                                .withPassword("${db.password}")
                            db.start()
                            project.properties.setProperty('db.url', db.getJdbcUrl())
                        </source>
                    </configuration>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>org.testcontainers</groupId>
                    <artifactId>postgresql</artifactId>
                    <version>${testcontainers.version}</version>
                </dependency>
                <!-- Junit seems to be a transitive dependency of testcontainers? -->
                <dependency>
                    <groupId>junit</groupId>
                    <artifactId>junit</artifactId>
                    <version>4.13.1</version>
                </dependency>
            </dependencies>
        </plugin>
        <!-- Migrate schema -->
        <plugin>
            <groupId>org.flywaydb</groupId>
            <artifactId>flyway-maven-plugin</artifactId>
            <version>7.14.0</version>
            <executions>
                <execution>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>migrate</goal>
                    </goals>
                    <configuration>
                        <url>${db.url}</url>
                        <user>${db.username}</user>
                        <password>${db.password}</password>
                        <locations>
                            <location>filesystem:src/main/resources/db/migration</location>
                        </locations>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <!-- Generate jOOQ code -->
        <plugin>
            <groupId>org.jooq</groupId>
            <artifactId>jooq-codegen-maven</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <jdbc>
                    <driver>${db.driver}</driver>
                    <url>${db.url}</url>
                    <user>${db.username}</user>
                    <password>${db.password}</password>
                </jdbc>
                <generator>
                    <database>
                        <inputSchema>public</inputSchema>
                    </database>
                    <target>
                        <packageName>ch.jtaf.db</packageName>
                    </target>
                </generator>
            </configuration>
        </plugin>