https://github.com/victools/jsonschema-gradle-example
Minimal example show-casing how the Java JSON Schema Generator can be used within a gradle build
https://github.com/victools/jsonschema-gradle-example
gradle json-schema jsonschema-generator
Last synced: about 2 months ago
JSON representation
Minimal example show-casing how the Java JSON Schema Generator can be used within a gradle build
- Host: GitHub
- URL: https://github.com/victools/jsonschema-gradle-example
- Owner: victools
- Created: 2020-10-03T12:37:41.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2020-10-03T12:41:30.000Z (about 5 years ago)
- Last Synced: 2025-10-04T16:41:14.323Z (3 months ago)
- Topics: gradle, json-schema, jsonschema-generator
- Size: 55.7 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Java JSON Schema Generator – Gradle Example
Minimal example show-casing how the Java JSON Schema Generator can be used within a gradle build.
----
## Usage
Depending on your OS either use the `gradlew` or `gradlew.bat` executable in the project directory.
Execute the following command:
```zsh
./gradlew -q generate
```
This will generate a schema (here for the `SchemaVersion` enum) and write it to a `schema.json` file in the project directory.
In this particular example, the schema is also printed to the console:
```
jsonschema-gradle-example % ./gradlew -q generate
{
"$schema" : "https://json-schema.org/draft/2019-09/schema",
"type" : "string",
"enum" : [ "DRAFT_6", "DRAFT_7", "DRAFT_2019_09" ]
}
```
## Content of `build.gradle`
For your convenience, here is the full content of the `build.gradle` file representing this example:
```groovy
import com.github.victools.jsonschema.generator.OptionPreset;
import com.github.victools.jsonschema.generator.SchemaGenerator;
import com.github.victools.jsonschema.generator.SchemaGeneratorConfigBuilder;
import com.github.victools.jsonschema.generator.SchemaVersion;
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath group: 'com.github.victools', name: 'jsonschema-generator', version: '4.16.0'
}
}
plugins {
id 'java-library'
}
task generate {
doLast {
def configBuilder = new SchemaGeneratorConfigBuilder(SchemaVersion.DRAFT_2019_09, OptionPreset.PLAIN_JSON);
// apply your configurations here
def generator = new SchemaGenerator(configBuilder.build());
// target the class for which to generate a schema
def jsonSchema = generator.generateSchema(SchemaVersion.class);
// handle generated schema, e.g. write it to the console or a file
def jsonSchemaAsString = jsonSchema.toPrettyString();
println jsonSchemaAsString
new File(projectDir, "schema.json").text = jsonSchemaAsString
}
}
```