Skip to content

Getting Started

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

Getting Started

1. Apply the plugin

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

The plugin works on its own, but it only wires generated sources into a source set when the java plugin is applied - which is the normal case.

2. Add the runtime dependencies you need

ModelMaker generates annotations; it does not bring their libraries with it. Add only what the enabled features require:

dependencies {
    // jackson feature
    implementation(platform("com.fasterxml.jackson:jackson-bom:2.22.2"))
    implementation("com.fasterxml.jackson.core:jackson-databind")

    // validation feature
    implementation("jakarta.validation:jakarta.validation-api:3.1.1")
    implementation("org.hibernate.validator:hibernate-validator:9.1.3.Final")

    // openApi feature
    implementation("io.swagger.core.v3:swagger-annotations-jakarta:2.2.30")
}

org.jspecify:jspecify is not on that list - the plugin adds it on compileOnly itself, unless your build already declares it. See Gradle Plugin.

3. Write a schema

One type per file, under src/main/model. The file name must be <title>.json:

src/main/model/com.example.dto.Person.json
{
  "$modelmaker": "v1.0",
  "title": "com.example.dto.Person",
  "type": "object",
  "required": ["id"],
  "properties": {
    "id": { "type": "string" },
    "age": { "type": "integer", "minimum": 0 }
  }
}

The full key list is on Schema Format.

4. Turn on the features you want

Everything is off by default - the bare generator emits a plain immutable POJO with no third-party annotations.

modelmaker {
    features {
        jackson { enabled = true }
        validation { enabled = true }
    }
}

Per-schema overrides are possible too, see Feature Configuration.

5. Build

./gradlew build          # generation runs as part of compilation
./gradlew generateModel  # or run both generators on their own

generateModelJava runs before compilation and writes:

build/generated/sources/modelmaker/java/main/com/example/dto/Person.java

That directory is registered as a source directory of the main source set, so javac picks it up with no extra configuration.

Where the output goes

By default, generated sources are throwaway build output under build/generated/sources/modelmaker, the way most code generators behave - nothing to commit, nothing to keep in sync.

To write them into the project tree instead, next to the schemas, where they are reviewed and checked into version control - so the diff of a schema change is visible in the generated code:

modelmaker {
    src { enabled = true }  // writes src/main/model/{java,kotlin}
}

To read the schemas from somewhere else - the generated sources follow them:

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

Either way the directory is wiped and rebuilt on every run of the task - never edit generated files by hand.

What you get

A final class with private fields, no setters, a builder, equals / hashCode / toString, and a mutate() method returning a pre-filled builder. Walk through it on Generated Java Code.

Next steps

Clone this wiki locally