documentation-website/Writerside/topics/Exposed-gradle-plugin.md
The Exposed Gradle plugin provides build-time tooling for working with Exposed-based database schemas.
Its primary feature is generating SQL migration scripts by comparing Exposed table definitions with an existing database schema.
Testcontainers)To install the plugin, add it to the plugins block in your Gradle build script:
plugins {
id("org.jetbrains.exposed.plugin") version "%exposed_version%"
}
To generate migration scripts based on the difference between your existing database schema and your Exposed table definitions,
use the generateMigrations task:
./gradlew generateMigrations
Generated files are written to the configured output directory.
You can configure migration generation to run automatically during the Gradle build lifecycle.
For example, you can generate migrations before the build or the processResources tasks:
// Generate migration scripts before the build task
tasks.named("build") {
dependsOn("generateMigrations")
}
// Generate migration scripts before the processResources task
tasks.named("processResources") {
dependsOn("generateMigrations")
}
Configure the plugin using the exposed.migrations block in your
<path>build.gradle.kts</path> file.
At minimum, configure the following properties:
tablesPackage as the package name where Exposed table definitions are located.Testcontainers configuration.To configure a database connection, set the databaseUrl, databaseUser, and databasePassword properties:
exposed {
migrations {
tablesPackage.set("com.example.db.tables")
databaseUrl.set("jdbc:postgresql://localhost:5432/mydb")
databaseUser.set("postgres")
databasePassword.set("password")
}
}
Testcontainers {id="testcontainers-config"}To configure a Testcontainers connection, set the testContainersImageName property:
exposed {
migrations {
tablesPackage.set("com.example.db.tables")
testContainersImageName.set("postgres:latest")
}
}
For more details and supported database container images, see .
{style="tip"}
When
testContainersImageNameis configured, the plugin usesTestcontainersinstead of a direct database connection for schema generation.
{style="note"}
Optionally, you can configure the following properties for additional control over migration generation and file naming:
<deflist type="medium"> <def> <title><code>classpath</code></title> Classpath scanned for Exposed table definitions.Defaults to the project's runtime classpath. </def> <def id="file-directory">
<title><code>fileDirectory</code></title>Directory where migration scripts are stored.
Defaults to "src/main/resources/db/migration".
Prefix used for migration script names.
Defaults to "V".
</def>
<def>
Version format used for migration script names. For supported values, see version formats.
Defaults to a timestamp in the yyyyMMddHHmmss format.
</def>
<def>
Separator used in migration script names.
Defaults to "__".
</def>
<def>
Whether the descriptive part of migration script names is converted to uppercase.
Defaults to true.
</def>
<def>
File extension used for migration scripts.
Defaults to ".sql".
</def>
</deflist>
Example:
exposed {
migrations {
// ...
classpath = sourceSets.main.get().runtimeClasspath
fileDirectory.set(layout.projectDirectory.dir("src/main/resources/db/migration"))
filePrefix.set("V")
fileVersionFormat.set(VersionFormat.TIMESTAMP_ONLY)
fileSeparator.set("__")
useUpperCaseDescription.set(true)
fileExtension.set(".sql")
}
}
The plugin supports the following VersionFormat values:
<deflist>
<def>
Example: V20260417195521__CREATE_TABLE_USERS.sql
</def>
<def>
Example: V202604171955__CREATE_TABLE_USERS.sql
</def>
<def>
Example: V3_20260417195521__CREATE_TABLE_USERS.sql
</def>
<def>
Example: V3_202604171955__CREATE_TABLE_USERS.sql
</def>
<def>
Example: V3_1__CREATE_TABLE_USERS.sql
</def>
<def>
Example: V3__CREATE_TABLE_USERS.sql
</def>
</deflist>
For version formats that include a major version, the plugin scans the configured fileDirectory to determine the
next available version. If the directory is empty, or if no compatible migration files are found, numbering starts at 1.
By default, migration scripts use the following naming pattern:
<prefix><version><separator><description><extension>
For example:
V20260417195521__CREATE_TABLE_USERS.sql
The generated description (CREATE_TABLE_USERS) is derived from the generated SQL statement and
typically follows this format:
<OPERATION>_<OBJECT>_<IDENTIFIER>_<EXTRA>
When a migration contains multiple SQL statements, the description is usually derived from the first significant statement.
CREATE TABLE statement.CREATE TABLE statement
instead of CREATE SEQUENCE.If the plugin cannot derive a standard description, it falls back to a generic name, such as CUSTOM_STATEMENT_12345.
You can override the generated filename by passing the --filename argument to the generateMigrations task:
./gradlew generateMigrations --filename=V0__initialize_schema.sql
When
--filenameis specified, the plugin generates a single migration script containing all migration statements, even if the schema diff affects multiple tables.
{style="note"}
Testcontainers {id="use-testcontainers"}Testcontainers is a Java library that lets you run temporary
Docker containers during tests or build tasks.
You can use Testcontainers to start a disposable database instance automatically while generating migration scripts.
Docker must be installed and running to use
Testcontainers.
{style="note"}
Testcontainers workflowWhen using Testcontainers, the Exposed Gradle plugin performs the following steps:
If the configured migration directory contains existing migration scripts, the plugin applies them using Flyway before generating new migrations.
This ensures that newly generated migration scripts are based on the latest schema state, including changes introduced by previous migrations.
The plugin supports the following database container images:
| Database | Container images |
|---|---|
| MySQL | mysql, mysql:latest, or other tags |
| MariaDB | mariadb, mariadb:latest , or other tags |
| PostgreSQL | postgres, postgres:latest , or other tags |
| SQL Server | mcr.microsoft.com/mssql/server, mcr.microsoft.com/mssql/server:2025-latest, or other tags |
| Oracle | Images starting with container-registry.oracle.com/,gvenzl/oracle- or oracle/ |
The Exposed Gradle plugin generates migration scripts, but it does not apply them to your database automatically.
After generating migration scripts, review and apply them using your existing database migration workflow. For example, you can:
After applying the generated scripts, your database schema should match your current Exposed table definitions.