Home > Back-end >  jsonschema2pojo returns error when try to generate dto's using schema
jsonschema2pojo returns error when try to generate dto's using schema

Time:11-22

There is a application on spring-boot and gradle and need to generate dto's using json schema. Try to use jsonschema2pojo plugin for do it and took example from here (changed only plugins block used plugins closure instead apply keyword

build.gradle file

buildscript {
        repositories {
            mavenCentral()
        }
        dependencies {
            classpath 'org.jsonschema2pojo:jsonschema2pojo-gradle-plugin'
        }
    }
    
plugins {
    id 'java'
    id 'jsonschema2pojo'
    id 'org.springframework.boot' version '2.7.5'
    id 'io.spring.dependency-management' version '1.0.15.RELEASE'
}

group = 'nuclear.bot'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-actuator'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    compileOnly 'org.projectlombok:lombok'
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

tasks.named('test') {
    useJUnitPlatform()
}


jsonSchema2Pojo {
    generateBuilders = false
    usePrimitives = false
    source = files("src/main/json-schema")
    targetDirectory = file("${project.buildDir}/generated-sources/js2p")
    targetPackage = 'crom.tech.api.models'
    propertyWordDelimiters = [] as char[]
    useLongIntegers = false
    useBigIntegers = false
    useDoubleNumbers = true
    useBigDecimals = false
    includeHashcodeAndEquals = true
    includeToString = true
    annotationStyle = 'jackson2'
    customAnnotator = 'org.jsonschema2pojo.NoopAnnotator'
    includeJsr303Annotations = false
    sourceType = 'jsonschema'
    removeOldOutput = true
    outputEncoding = 'UTF-8'
    useJodaDates = false
    useCommonsLang3 = false
    initializeCollections = true
    classNamePrefix = ""
    classNameSuffix = ""
    fileExtensions = [] as String[]
    includeConstructors = true
    serializable = true
    includeAccessors = true
    includeDynamicAccessors = false
}

But when I try to nuild project, there is error message:

Build file 'C:\learn\Nuclear-bot\Nuclear-bot-parsing-processor\build.gradle' line: 12

Plugin [id: 'jsonschema2pojo'] was not found in any of the following sources:

  • Try:

Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

CodePudding user response:

The jsonschema2pojo plugin doesn't support the plugins block. The reason is that it is missing a so-called "marker artifact" on the Gradle Plugin Portal. This is what tells Gradle what actual dependency (here org.jsonschema2pojo:jsonschema2pojo-gradle-plugin) to use for a given plugin. It is also the reason why it isn't listed in the Portal.

So we are stuck with the legacy way of applying the plugin for now. There is an issue for it here. But as it has been open since 2015, they are probably looking for contributors.

Also note that you need to add a version number to the classpath dependency, e.g.:

// Groovy DSL
buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'org.jsonschema2pojo:jsonschema2pojo-gradle-plugin:1.1.2' // <-- Version here
    }
}

plugins {
    // Other plugins here...
}

apply plugin: 'jsonschema2pojo' // <-- Legacy (but required) way of applying the plugin
  • Related