Version v1.0-RC1 of the documentation is no longer actively maintained. The site that you are currently viewing is an archived snapshot. For up-to-date documentation, see the latest version.
Gradle Plugin
Overview
The Gradle plugin provided by Komapper generates the following two types of Kotlin source code from database metadata:
- Entity classes
- Mapping definitions
See Entity Classes for entity classes and mapping definitions.
Note
Use of this Gradle plugin is not required. Please consider using this plugin if you already have table definitions in your database.How to use
The latest version of the plugin can be found on the Gradle plugin portal site.
The following code is an example of Gradle build script using the plugin:
buildscript {
repositories {
mavenCentral()
}
// Define dependencies on Testcontainers and PostgreSQL JDBC driver
dependencies {
classpath(platform("org.testcontainers:testcontainers-bom:1.16.3"))
classpath("org.testcontainers:postgresql")
classpath("org.postgresql:postgresql:42.3.2")
}
}
// Declare the use of the Komapper plugin
plugins {
id("org.komapper.gradle") version "1.0.0-RC1"
}
// Configure settings related to the Komapper plugin
komapper {
generators {
// Name the register block appropriately for each database to be used, and write the settings in the block
register("postgresql") {
val initScript = file("src/main/resources/init_postgresql.sql")
// Set JDBC parameters; use PostgreSQL on Testcontainers
jdbc {
driver.set("org.testcontainers.jdbc.ContainerDatabaseDriver")
url = "jdbc:tc:postgresql:13.3:///test?TC_INITSCRIPT=file:${initScript.absolutePath}",
user = "test",
password = "test"
}
packageName.set("org.komapper.example.postgresql")
overwriteEntities.set(true)
overwriteDefinitions.set(true)
}
}
}
After setting up the above, the entity classes and mapping definitions can be generated by executing the following command:
$ ./gradlew komapperGenerator
The same result can be obtained by explicitly specifying the name passed to the register
function:
$ ./gradlew komapperPostgresqlGenerator
See also Examples.
Parameter list
Indicates the parameters that can be set within the register
block.
jdbc.driver
Represents the JDBC driver class name.
Setting is required.
jdbc.url
Represents a JDBC URL.
Setting is required.
jdbc.user
Represents a JDBC user.
jdbc.password
Represents a JDBC password
catalog
Represents a catalog name, which is used to get database metadata.
This value is passed as a parameter to the DatabaseMetaData#getTables method.
schemaPattern
Represents a schema name pattern, which is used to get database metadata.
It can be written similarly to a LIKE predicate, such as %SALES%
.
This value is passed as a parameter to the DatabaseMetaData#getTables method.
tableNamePattern
Represents a table name pattern, which is used to get database metadata.
It can be written similarly to a LIKE predicate, such as JOB%
.
This value is passed as a parameter to the DatabaseMetaData#getTables method.
tableTypes
Represents table types, which are used to get database metadata.
The default value is a list containing only TABLE
.
This parameter can contain following values:
- TABLE
- VIEW
This value is passed as a parameter to the DatabaseMetaData#getTables method.
destinationDir
The output destination for the generated Kotlin source code.
The default value is src/main/kotlin
.
Note
The source code for entity classes is output as “entities.kt”. And the source code for mapping definitions is output as “entityDefinitions.kt”.
These file names cannot be changed.
packageName
The package name of the generated entity classes and mapping definition classes.
prefix
A prefix for the simple name of generated entity classes.
The default value is an empty string.
suffix
A suffix for the qualified name of generated entity classes.
The default value is an empty string.
overwriteEntities
Indicates whether the source code of the generated entity classes should be overwritten.
The default value is false
.
overwriteDefinitions
Indicates whether the source code of the generated mapping definition classes should be overwritten.
The default value is false
.
declareAsNullable
Indicates whether all properties of the generated entity classes should be declared as nullable types.
The default value is false
.
If this value is false
, the decision to declare the property as a nullable type
is determined from the database metadata for each property.
useCatalog
Indicates whether the catalog name should be explicit in the generated mapping definitions.
The default value is false
.
useSchema
Indicates whether the schema name should be explicit in the generated mapping definitions.
The default value is false
.
propertyTypeResolver
A type resolver that determines the types of properties of the generated entity classes.
The default value is org.komapper.codegen.PropertyTypeResolver.of()
.
enquote
A function that quotes SQL identifiers.
The default value is org.komapper.codegen.Enquote.of()
.