【问题标题】:带有 flyway 和 testconteiners 的 Jooq 每次运行都会创建新容器
【发布时间】:2022-01-23 16:25:39
【问题描述】:

我想将 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 启动了自己的容器并尝试生成实体,但什么也没有。你能建议如何处理这个问题吗?

【问题讨论】:

    标签: java code-generation jooq flyway testcontainers


    【解决方案1】:

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

            <!-- 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-03
      • 2014-04-25
      • 2014-01-15
      • 2013-04-01
      • 2015-11-06
      相关资源
      最近更新 更多