Skip to content

Gradle Plugin

Damian Malczewski edited this page Sep 10, 2026 · 2 revisions

Gradle Plugin

io.github.malczuuu.modelmaker is the intended way to use ModelMaker: it registers the modelmaker { } extension, adds two generation tasks, and wires their output into the main source set.

plugins {
    java
    id("io.github.malczuuu.modelmaker") version "..."
}

The plugin applies nothing by itself. Source-set wiring and the JSpecify dependency happen only once the java plugin is present; Kotlin extension generation additionally requires org.jetbrains.kotlin.jvm.

The modelmaker { } extension

modelmaker {
    schemas {
        directory = layout.projectDirectory.dir("src/main/model")  // default
    }
    src {
        enabled = true  // default: write into <schemas>/{java,kotlin} (checked in);
    }                   // false writes into build/generated/sources/modelmaker instead
    kotlin {
        enabled = false  // default off
    }
    features {
        jackson {          // @JsonCreator / @JsonProperty / ...
            enabled = false
            annotateFields = false
            annotateGetters = true
        }
        validation { enabled = false }  // jakarta.validation annotations, @Valid cascades
        openApi { enabled = false }  // OpenAPI @Schema (description / example / requiredMode)
        withers { enabled = false }  // withXyz(value) per property
        preferPrimitives { enabled = false }  // int/double/boolean instead of boxed types
    }
}
  • schemas { directory } - default src/main/model. Where schema files are read from; it need not exist.
  • src { enabled } - default true. Write generated sources into the project tree rather than under build/.
  • kotlin { enabled } - default false. Generate mutate { } extensions, see Kotlin Extensions.
  • features { } - all off by default. See Feature Configuration.

Schema files

Schemas live under src/main/model, scanned recursively. One type per file, named <title>.json. Subdirectories are free to use for grouping - the package comes from title, not from the path.

The directory need not exist: a project that applies the plugin without any schemas builds normally.

src/main/model/
├── com.example.dto.Address.json
├── com.example.dto.Customer.json
└── java/                              ← generated output (src.enabled = true)
    └── com/example/dto/
        ├── Address.java
        └── Customer.java

If the directory holds no schemas - or does not exist - the tasks log No model schemas found in ... and remove their output directory instead of leaving an empty one behind.

To read schemas from somewhere else:

modelmaker {
    schemas { directory = layout.projectDirectory.dir("src/main/schemas") }
}

With src.enabled on, the generated sources follow: they are written to <schemas>/java and <schemas>/kotlin.

Tasks

  • generateModelJava (JavaModelGenerate) - one .java class per schema file.
  • generateModelKotlin (KotlinModelGenerate) - depends on generateModelJava; one <Name>Extensions.kt per type.
  • generateModel - a lifecycle task running both of the above. Handy to invoke by hand; the build does not need it, since compilation already depends on the generators through the source set.

Both are in the modelmaker group and run automatically before compilation, because their output directories are registered as source directories of the main source set.

./gradlew generateModelJava
./gradlew build              # runs generation first

Configuring a task directly

The task exposes the same blocks as the extension, with the extension's values as conventions:

import io.github.malczuuu.modelmaker.gradle.tasks.JavaModelGenerate

tasks.named<JavaModelGenerate>("generateModelJava") {
    features {
        withers { enabled = true }
    }
}

This is the middle level of the precedence chain described in Feature Configuration.

Output locations

src.enabled Java Kotlin
true (default) src/main/model/java src/main/model/kotlin
false build/generated/sources/modelmaker/java/main build/generated/sources/modelmaker/kotlin/main

Both directories are added to the main source set. javac compiles the .java; the Kotlin plugin, when applied, compiles the .kt.

With src.enabled = true the generated code is part of the repository - a schema change shows up as a reviewable diff. Exclude it from formatting tools that would otherwise fight the generator:

spotless {
    java {
        target("src/**/*.java")
        targetExclude("build/**", "src/main/model/java/**")
    }
}

Task behavior

  • Not incremental. The output directory is deleted and rebuilt on every run - never hand-edit generated files.
  • Cacheable. Both tasks are @CacheableTask, with the schema directory as a relative-path-sensitive input and the feature flags as @Inputs. Flip a flag and generation re-runs.
  • Configuration-cache compatible. The plugin declares configuration cache support, and the repository itself builds with org.gradle.configuration-cache=true.
  • generateModelKotlin doubles as a cleaner. When Kotlin generation is off - or the Kotlin plugin is absent - it deletes its output directory instead of leaving stale .kt files behind.

JSpecify

Generated classes are @NullMarked, so org.jspecify:jspecify must be on the compile classpath. The plugin adds it on compileOnly unless the build already declares it in api, compileOnlyApi, compileOnly or implementation.

Declare it yourself when you want a specific version, or when the annotations must be visible at runtime:

dependencies {
    implementation("org.jspecify:jspecify:1.0.1")
}

Kotlin projects

plugins {
    kotlin("jvm") version "..."
    id("io.github.malczuuu.modelmaker") version "..."
}

modelmaker {
    kotlin { enabled = true }
}

Both conditions must hold - kotlin.enabled = true and the Kotlin JVM plugin applied - or generateModelKotlin produces nothing. Details on Kotlin Extensions.

Other dependencies

The plugin never contributes Jackson, jakarta.validation or swagger annotations. Add whichever the enabled features need, see Getting Started.

Without Gradle

The core generator is a plain library with no build-tool API. See Library Usage.

Clone this wiki locally