0

In some scecific usescases like the interaction with jooq-codegen-maven, it is necessary to initialize a Database from Maven.

One alternative could be the usage of flyway, but how to use only testcontainers with the method: .withInitScript? Using this way, I receive a error message because it doesn´t find the file:

        <plugin>
            <groupId>org.codehaus.gmaven</groupId>
            <artifactId>groovy-maven-plugin</artifactId>
            <version>2.1.1</version>
            <executions>
                <execution>
                    <id>testcontainer-start</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>execute</goal>
                    </goals>
                    <configuration>
                        <source>
                            db = new org.testcontainers.containers.PostgreSQLContainer("postgres:15.1-alpine")
                                    .withUsername("${db.username}")
                                    .withDatabaseName("postgres")
                                    .withPassword("${db.password}");
                                    //.withInitScript("postgres-sakila-db/postgres-sakila-schema.sql");

                            containerDelegate = new org.testcontainers.jdbc.JdbcDatabaseDelegate(db, "");
                            //org.testcontainers.ext.ScriptUtils.runInitScript(containerDelegate, "filesystem:src/main/resources/postgres-sakila-db/postgres-sakila-schema.sql");

                            db.start();
                            project.properties.setProperty('db.url', db.getJdbcUrl());
                            project.properties.setProperty('testcontainer.containerid', db.getContainerId());
                            project.properties.setProperty('testcontainer.imageName', db.getDockerImageName());
                        </source>
                    </configuration>
                </execution>
                <execution>
                    <id>testcontainer-stop</id>
                    <phase>test</phase>
                    <goals>
                        <goal>execute</goal>
                    </goals>
                    <configuration>
                        <source>
                            containerId = "${testcontainer.containerid}"
                            imageName = "${testcontainer.imageName}"
                            println("Stopping testcontainer $containerId - $imageName")
                            org.testcontainers.utility.ResourceReaper
                                    .instance()
                                    .stopAndRemoveContainer(containerId, imageName);
                        </source>
                    </configuration>
                </execution>

            </executions>

            <dependencies>
                <dependency>
                    <groupId>org.testcontainers</groupId>
                    <artifactId>postgresql</artifactId>
                    <version>${testcontainers.version}</version>
                </dependency>
            </dependencies>
        </plugin>

What is the specific syntax to find the file if it is located in src/main/resources/?

//.withInitScript("postgres-sakila-db/postgres-sakila-schema.sql");

Many thanks in advance

Juan Antonio

jabrena
  • 1,166
  • 3
  • 11
  • 25

0 Answers0