Documentation

Java

Community Module

These modules are maintained by the community, outside of the Testcontainers project.

Description

A minimal, framework-agnostic Gradle plugin that manages container lifecycles for build-time tasks (such as code generation, database migrations, schema inspection, or integration testing) using Testcontainers.

Examples

Dependency:
plugins {
    id("io.github.regulskimichal.testcontainers") version "0.2.0"
}

// additional configuration, add modules you want to use in the build process
dependencies {
    "testcontainersClasspath"("org.testcontainers:testcontainers-postgresql:2.0.5")
}
Usage:
import org.testcontainers.containers.JdbcDatabaseContainer
import org.testcontainers.gradle.DatabaseType
import org.testcontainers.gradle.getContainer

// 1. Define container
testcontainers {
    jdbcContainer("postgres", DatabaseType.POSTGRESQL) {
        image("postgres:18-alpine")
        databaseName("testdb")
        username("user")
        password("pass")
        portMapping(5432)
    }
}

// 2. Use container in a custom task
tasks.register("printDbInfo") {
    dependsOn("startPostgresContainer")
    usesService(testcontainers.service)

    val dbProvider = testcontainers.getContainer<JdbcDatabaseContainer<*>>("postgres")

    doFirst {
        val db = dbProvider.get()
        logger.warn(
            """
            SUCCESSFULLY CONFIGURED POSTGRES CONTAINER!
            JDBC URL: ${db.jdbcUrl}
            Username: ${db.username}
            Password: ${db.password}""".trimIndent()
        )
    }
}