Syncing ktor project generator with gradle init task
Published: 2024-06-02 14:07:42

Good morning, Gradle init task generates a project with the very useful structure based on centralized dependency management, namely libs.versions.toml file in 'gradle' directory.
This is very useful when dependency versions change, and it looks like a good place where insert 'all' dependencies and versions.
It would be a good thing if the ktor project generator followed this structure.
For example, in libs.versions. toml: 

`[versions]
kotlin-version = "2.0.0"
ktor-version = "2.3.11"
guava = "32.1.3-jre"
junit-jupiter-engine = "5.10.1"
exposed-version = "0.50.+"toml format
h2 = "2.2.224"
postgres = "42.5.1"
mu-logging = "2.0.11"
logback = "1.4.14"
hikaricp-version = "5.1.0"
ehcache-version = "3.10.8"`

In fact, when I generate a ktor-project, I spend at least one hour to adapt the structure to the toml format, because I'm convinced that the build.gradle.kts is cleaner and more readable, for instance


`plugins {

    alias(libs.plugins.jvm)

    //ktor
    alias(libs.plugins.ktor)

    //kotlin.serialization
    alias(libs.plugins.kotlin.serialization)

  }

repositories {
    // Use Maven Central for resolving dependencies.
    mavenCentral()
}


group = "org.erfranco"
version = "0.0.1"

application {

    mainClass = "org.erfranco.ApplicationKt"

    val isDevelopment: Boolean = project.ext.has("development")
		println("Property 'development'  found? : $isDevelopment")
    applicationDefaultJvmArgs = listOf("-Dio.ktor.development=$isDevelopment")
}


dependencies {

    //logging
    implementation(libs.mu.logging)
    implementation(libs.logback)

    // Ktor stuff
    implementation(libs.ktor.server.core)
    implementation(libs.ktor.server.statuspages)
    implementation(libs.ktor.server.configyaml)
    implementation(libs.ktor.server.serialization.json)
    implementation(libs.ktor.server.content.negotiationjvm)
    implementation(libs.ktor.server.host.commonjvm)
		
		//Embedded server		
    //implementation(libs.ktor.server.tomcatjvm)
    implementation(libs.ktor.server.nettyjvm)

    //implementation(libs.ktor.server.freemarkerjvm)

    // Exposed stuff
    implementation(libs.exposed.core)
    implementation(libs.exposed.jdbc)

    //implementation(libs.exposed.crypt)
    //implementation(libs.exposed.dao)
    //implementation(libs.exposed.kotlin.datetime)
    //implementation(libs.exposed.json)
    //implementation(libs.exposed.money)

    // Database
    implementation(libs.h2)
   //implementation(libs.postgres)

    //pooling and cache
    //implementation(libs.ehcache)
    //implementation(libs.hikaricp)

    testImplementation(libs.ktor.server.testsjvm)
    testImplementation(libs.kotlin.test.junit)

    // Use the Kotlin JUnit 5 integration.
    //testImplementation(libs.kotlin.test.junit5)

    // Use the JUnit 5 integration.
    //testImplementation(libs.junit.jupiter.engine)

    //testRuntimeOnly(libs.junit.platform.launcher)

}`

Moreover, gradle.properties file is clean:

`kotlin.code.style=official
project.ext {
  development
}`


This is my libs.versions.toml file, in 'gradle' folder, almost the same for all projects:

`# This file was generated by the Gradle 'init' task.
# https://docs.gradle.org/current/userguide/platforms.html#sub::toml-dependencies-format
# Ermanno Franco 2024-6-02

[versions]
kotlin-version = "2.0.0"
ktor-version = "2.3.11"
guava = "32.1.3-jre"
junit-jupiter-engine = "5.10.1"
exposed-version = "0.50.+"
h2 = "2.2.224"
postgres = "42.5.1"
mu-logging = "2.0.11"
logback = "1.4.14"
hikaricp-version = "5.1.0"
ehcache-version = "3.10.8"


[libraries]

mu-logging = { module = "io.github.microutils:kotlin-logging-jvm", version.ref = "mu-logging" }
logback = { module = "ch.qos.logback:logback-classic", version.ref = "logback" }
kotlin-test-junit = { module = "org.jetbrains.kotlin:kotlin-test-junit", version.ref = "kotlin-version" }
kotlin-test-junit5 = { module = "org.jetbrains.kotlin:kotlin-test-junit5", version.ref = "kotlin-version" }
junit-jupiter-engine = { module = "org.junit.jupiter:junit-jupiter-engine", version.ref = "junit-jupiter-engine" }
junit-platform-launcher = { module = "org.junit.platform:junit-platform-launcher" }

guava = { module = "com.google.guava:guava", version.ref = "guava" }

exposed-core = {module = "org.jetbrains.exposed:exposed-core", version.ref = "exposed-version" }
exposed-crypt = {module = "org.jetbrains.exposed:exposed-crypt", version.ref = "exposed-version" }
exposed-dao = {module = "org.jetbrains.exposed:exposed-dao", version.ref = "exposed-version" }
exposed-jdbc = {module = "org.jetbrains.exposed:exposed-jdbc", version.ref = "exposed-version" }
exposed-kotlin-datetime = {module = "org.jetbrains.exposed:exposed-kotlin-datetime", version.ref = "exposed-version" }
exposed-json = {module = "org.jetbrains.exposed:exposed-json", version.ref = "exposed-version" }
exposed-money = {module = "org.jetbrains.exposed:exposed-money", version.ref = "exposed-version" }

h2 = {module = "com.h2database:h2", version.ref = "h2" }
postgres = {module = "org.postgresql:postgresql", version.ref = "postgres" }

hikaricp = {module = "com.zaxxer:HikariCP", version.ref = "hikaricp-version" }
ehcache = {module = "org.ehcache:ehcache", version.ref = "ehcache-version" }

ktor-server-core = { module = "io.ktor:ktor-server-core-jvm", version.ref = "ktor-version" }
ktor-server-statuspages = { module = "io.ktor:ktor-server-status-pages", version.ref = "ktor-version" }
ktor-server-configyaml = { module = "io.ktor:ktor-server-config-yaml", version.ref = "ktor-version" }
ktor-server-testsjvm = { module = "io.ktor:ktor-server-tests-jvm", version.ref = "ktor-version" }
ktor-server-freemarkerjvm = { module = "io.ktor:ktor-server-freemarker-jvm", version.ref = "ktor-version" }

ktor-server-tomcatjvm = { module = "io.ktor:ktor-server-tomcat-jvm", version.ref = "ktor-version" }
ktor-server-nettyjvm = { module = "io.ktor:ktor-server-netty-jvm", version.ref = "ktor-version" }

ktor-server-serialization-json = { module = "io.ktor:ktor-serialization-kotlinx-json-jvm", version.ref = "ktor-version" }
ktor-server-content-negotiationjvm = { module = "io.ktor:ktor-server-content-negotiation-jvm", version.ref = "ktor-version" }
ktor-server-host-commonjvm = { module = "io.ktor:ktor-server-host-common-jvm", version.ref = "ktor-version" }

[plugins]
jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin-version" }
kotlin-serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin-version" }
ktor = { id = "io.ktor.plugin", version.ref = "ktor-version" }
`
Happy coding!