JsonSchema2Pojo and manual task in gradle

827 Views Asked by At

What I would like to achieve

I would like to have a manual gradle task that I can generate Java classes based on Json schema. However, I don't want this task to run when I run other fx. gradle build.

What I did

Firstly I've create simple gradle java project with

gradle init

Then I have added jsonschema2dataclass plugin and configure it as follows (my current build.gradle):

/*
 * This file was generated by the Gradle 'init' task.
 * (...) 
 */

plugins {
    // Apply the application plugin to add support for building a CLI application in Java.
    id 'application'

    id "org.jsonschema2dataclass" version "4.5.0"
}

repositories {
    // Use Maven Central for resolving dependencies.
    mavenCentral()
}

dependencies {
    // Use JUnit Jupiter API for testing.
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.2'

    // Use JUnit Jupiter Engine for testing.
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'

    // This dependency is used by the application.
    implementation 'com.google.guava:guava:29.0-jre'
}

application {
    // Define the main class for the application.
    mainClass = 'GradlePlayground.App'
}

jsonSchema2Pojo {
            includeGeneratedAnnotation = true
            generateBuilders = true
            targetPackage = 'org.example.api' // specify package for your needs
            targetDirectoryPrefix = file("${project.rootDir}/app/src/main/java")

            source.setFrom files("${project.rootDir}/app/src/main/resources/json")
}

What I've tried

  1. Add task.enabled = false
  2. Put plugin configuration into another task
  3. Check source code of plugin to find a way to disconnect this task from build task

But all above trails have failed. When I run gradle tasks I can always see generateJsonSchema2DataClass and generateJsonSchema2DataClass0 as part of build tasks.

I'm using java 8 and gradle 6.9.3

1

There are 1 best solutions below

3
On

I'm the author of the gradle plugin.

Short answer for your question is "no, it's not an intended flow for a normal project". However, it's always possible to create a new project, build it once and extract the sources.

Could you please explain, what the reason you want to exclude run from the build chain?

My only guess is to build and publish models. And if this is a case, it's possible to do this using some gradle magic, which described in discussions in the GitHub project

UPD: Based on your gradle script, I have a lot of "why" questions I'd like to ask, it'll be easier if you