pwall567 / json-kotlin-gradle

Gradle JSON Schema code generation plugin
17 stars 7 forks source link

Using the generated-sources from another module #4

Closed kjussakov closed 1 year ago

kjussakov commented 1 year ago

Hi,

First of all big thanks for the great library! I am not sure if this is really an issue with the plugin or more how I use it in my setup. I have gradle subproject "model" where the only thing I have is the json schema files in src/main/resources/schema and no other sources. build.gradle.kts:

configure<JSONSchemaCodegen> {
    packageName.set("com.example")
    inputs {
        inputComposite {
            file.set(file("src/main/resources/schema/sample_schema.json"))
            pointer.set("/definitions")
        }
    }
}

sourceSets.main {
    java.srcDirs("build/generated-sources/kotlin")
}

Then in another subproject "service" I have the following:

dependencies {
    implementation(project(":model"))
}

In the top level settings.gradle.kts I have:

rootProject.name = "example"
include("model")
include("service")

when I execute: ./gradlew clean build

I always get errors in "service" for Unresolved reference to classes which should have been generated from the src/main/resources/schema And indeed the build/generated-sources folder in "model" is missing even though the build and the jars in build/libs are generated indicating there is some problem with the build not depending on the generated task. The same issue is happening when I run:

./gradlew co360-model:build

The strange thing is if I run the build task from Intellij gradle window the generateKotlin task is executed and the generated-sources are build. I am not able to understand what Intellij is executing under the hood.

Any hints are highly appreciated.

Best regards, Rumen

pwall567 commented 1 year ago

Hi Rumen,

I have to admit that I'm not really a Gradle expert, so I'm not sure I can help you. I have little experience of multi-module Gradle builds, so I don't know how best to establish the dependency between the code generation task and the compile task. I would try experimenting with the use of dependsOn to tie the execution of the "service" build to the "model" build, but to be honest, I don't know how to do that without a lot of investigation.

An approach that works well for me (in my day job) is to run the code generation and compilation of the generated code as a separate project, resulting in a JAR of data classes. This has the advantage that the same JAR can then be included in the builds of both ends of a REST link, but I recognise that this approach may not be suitable for your project.

I'm sorry I can't offer much help – if I think of anything else I'll let you know.

Good luck, Peter

kjussakov commented 1 year ago

Hi Peter,

Thank you for taking time to answer! I myself am not Gradle expert either and have tried the dependsOn path but without much success before posting here. In any case I will close this since it is not really an issue with the plugin. If I find a solution for it I will post it here.

Best regards, Rumen

gsnerf commented 3 months ago

Sorry to revive this old issue, but I actually believe the defaults to be faulty for usage in multi-module builds.

In these cases the root directory is different and the defaults will always try to find the schema files and build it based on the "current directory". This is not necessarily the directory of the project the plugin is supposed to be executed on, but the root of the multi-module project, which most likely has no schema files available and doesn't require the generated output directly.

@kjussakov: as a workaround for your case you would need define both inputs and outputs to contain a reference to ${project.projectDir} for it to work correctly. Example below:

configure<JSONSchemaCodegen> {
    inputs {
        inputFile(file("${project.projectDir}/src/main/resources/schema")) // if not in the default location
    }
    outputDir.set(file("${project.projectDir}/build/generated-sources/kotlin"))
}

But again, I think that shouldn't be necessary and the defaults should be corrected to already mind the project directory.