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.

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.17.1"))
        classpath("org.testcontainers:postgresql")
        classpath("org.postgresql:postgresql:42.3.4")
    }
}

// Declare the use of the Komapper plugin
plugins {
  id("org.komapper.gradle") version "1.4.0"
}

// 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.set("jdbc:tc:postgresql:13.3:///test?TC_INITSCRIPT=file:${initScript.absolutePath}")
                user.set("test")
                password.set("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.

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 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().

classNameResolver

A resolver that determines the names of the generated entity classes.

The default value is org.komapper.codegen.ClassNameResolver.of(prefix, suffix).

propertyNameResolver

A resolver that determines the names of properties of the generated entity classes.

The default value is org.komapper.codegen.PropertyNameResolver.of().

Last modified September 17, 2022: Add v1.4 (23eb572)