OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.36k stars 6.46k forks source link

[REQ] Allow generating only src/main/java in gradle plugin #6155

Open AceHack opened 4 years ago

AceHack commented 4 years ago

Is your feature request related to a problem? Please describe.

When using the gradle plugin instead of creating a whole new project for java clients, I would rather just have the code generated as a precompile step and added to the classpath like https://github.com/joelittlejohn/jsonschema2pojo handles code generation.

Describe the solution you'd like

Ability to only generate the code classes in src/main/java for java clients and then the generated classes be auto referenced in the existing gradle project that's dong the code generation.

Describe alternatives you've considered

This is able as close as I've been able to come to accomplishing that with the current plugin, but it still generates a ton of extra needed stuff like the gradle project, gradle wrapper, and a bunch of other things. I would really like to be able to generate just the source code.

in build.gradle.kts

val openApiGeneratedDir = "${project.buildDir}/generated/sources/openApi"

tasks.compileJava {
    dependsOn(tasks.openApiGenerate)
}

sourceSets.main {
    java.srcDir(openApiGeneratedDir)
}

openApiGenerate {
    inputSpec.set("https://xxx.yyy.zzz")
    validateSpec.set(true)
    generatorName.set("java")
    engine.set("mustache")
    skipOverwrite.set(false)
    outputDir.set(openApiGeneratedDir)
    apiPackage.set("datalake.subscription.api")
    invokerPackage.set("datalake.subscription.invoker")
    modelPackage.set("datalake.subscription.model")
    generateApiTests.set(false)
    generateApiDocumentation.set(false)
    generateModelTests.set(false)
    generateModelDocumentation.set(false)
    configOptions.set(mapOf(
            "java8" to "true",
            "dateLibrary" to "java8",
            "library" to "native",
            "asyncNative" to "true",
            "serializationLibrary" to "jackson"
    ))
jimschubert commented 4 years ago

@AceHack You can achieve what you're trying to do by defining an ignore file.

For example:

cat > .openapi-generator-java-sources.ignore <<EOF
*
**/*
!**/src/main/java/**/*
EOF

Then, in your task add this:

ignoreFileOverride.set(".openapi-generator-java-sources.ignore")

You can name the ignore file whatever you want, and put it under any directory you want. The patterns work the same as gitignore (although rather than ignoring committed files you're ignoring generated files). The big differences is that patterns are relative to your generated directory only, and we don't support the ignore file in nested directories (that is, we don't traverse directories and merge like gitignore does).

Let me know if that works for you.

marcelmore commented 3 years ago

Having the same problem and it almost works with this ignore patterns but not quite yet. (see image)

tree
luisalves00 commented 3 years ago

Same with me @mmoehring.

As a nice to have, would be cool for IntelliJ to pickup generated folder automatically, like when using Mapstruct. I think is because they use "annotationProcessor". e.g:

dependencies {
    compileOnly("org.mapstruct:mapstruct")
    annotationProcessor("org.mapstruct:mapstruct-processor")
}

compileJava {
    options.compilerArgs = [
            "-Amapstruct.defaultComponentModel=spring",
            "-Amapstruct.suppressGeneratorTimestamp=true",
            "-Amapstruct.suppressGeneratorVersionInfoComment=true",
            "-Amapstruct.unmappedTargetPolicy=ERROR"
    ]
}

To have a similar layout as mapstruct I did:

openApiGenerate {
    generatorName = "java"
    library = "jersey2"
    inputSpec = "...".toString()
    outputDir = "$buildDir/generated/sources/openapi".toString()
    ignoreFileOverride = file("$rootDir/.openapi-generator-java-sources.ignore").toString()
    configOptions = [
        dateLibrary: "java8",
        sourceFolder: "java/main"
    ]
}

compileJava.dependsOn tasks.openApiGenerate

sourceSets {
    main {
        java {
            srcDir new File(buildDir, "/generated/sources/openapi/java/main")
        }
    }
}

An I have to run on command line so the sources are generated as IntelliJ doesn't seem to execute the openApiGenerate task.