Skip to content
No description, website, or topics provided.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.

Kotlin Object Generator + Gradle Plugin

The problem - often you have projects with a combinations of runtime, static and dyanamic configuration and you simply want Type-Checked access to all config and values in a single place without the head ache of wrapping your properties etc.

Kotlin-Object-Generator (KOG) to the rescue

A quick example (everything from auto-compile to capitalization is configurable - look at source and tests in lib)

You have a property file as follows



my.other.obj.value=It really works


  • RootValues.kt - all root (top level props) values go into this files
import kotlin.Int

val TestRootValue: Int = 567
  • My.kt - the My namespace defined
import kotlin.String

object My {
    object Other {
        object Obj {
            val Value: String = "It really works"
  • Test.kt - the Test namespace defined
import kotlin.Boolean
import kotlin.Double
import kotlin.Int
import kotlin.String

object Test {
    val Str: String = "Densebrain"

    val Int: Int = 1234

    val Double: Double = 10.5

    val Bool: Boolean = true

Use the lib

  1. Dependency
repositories {
  maven(url = "")

dependencies {
  // Use a specific release - replace + with version
  1. Code
 * Create a configuration
 * For all options - see
 * @see
data class Config(
  // Output directory
  // this example will dump the files to your desktop
  override var outputDir: File = File("${System.getenv("HOME")}/Desktop","ko-files"),
  // All your prop files
  override var files: List<File> = listOf(""), 
  // Compile to classes and add to classpath   
  override var addToClasspath: Boolean = true,

  // Package to use for generated objects
  override var packageName: String = "TestPkg",
  // Package to use for generated objects
  override var capitalize: Boolean = true
) : KOGeneratorConfig

fun main() {
  // srcFiles will hold a List<File> with all generated
  // sources - and since addToClasspath == true
  // the compiled classes will be added to the 
  // thread context classloader
  val srcFiles = KOGenerator(Config()).execute()

Gradle Plugin (works+tested on 5.3.2 - should work above 4.10)

9 times out of 10 you will want the generator in your buildSrc, feel free to use elsewhere - but the following is the common use which will make all props available in your standard build.kts

Checkout example folder for full example

  1. buildSrc/settings.gradle.kts
pluginManagement {
  repositories {
    // The following can likely be omitted as we publish
    // to the gradle plugin portal
    maven(url = "")
  1. buildSrc/build.gradle.kts
plugins {

 * For more options
 * @see org.densebrain.gradle.KOGeneratorExtension
koGenerator {

  // Output dir - where generation files go
  outputDir = File(buildDir,"ko")

  // PKG
  packageName = "foo"
  1. build.gradle.kts
import foo.Example

println("KO: ${Example.Name}")
You can’t perform that action at this time.