Skip to content

Getting Started

Damian Malczewski edited this page Sep 10, 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:

src/main/model/java/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 written into the project tree (src/main/model/java), so they are meant to be reviewed and checked into version control - the diff of a schema change is visible in the generated code.

To keep them out of the tree instead:

modelmaker {
    src { enabled = false }  // writes build/generated/sources/modelmaker/java/main
}

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