如何在gradle Java应用程序中包含来自openapi-generator的客户端? [英] How to include the client from openapi-generator in a gradle java application?

查看:78
本文介绍了如何在gradle Java应用程序中包含来自openapi-generator的客户端?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个gradle Java应用程序,该应用程序从openAPI规范文件生成一个客户端并使用该客户端.因此,我使用 gradle init (类型:应用程序,语言:Java,DSL:常规,测试框架:Junit Jupiter,项目名称:simple-java-app,包结构: a.aa ).

行之有效的小例子:

我可以使用包 b.bb 和类 Foo 创建一个新的源文件夹 second/loc/src/main/java .并使用以下 build.gradle

 插件{id'java'id应用程序"}储存库{jcenter()}sourceSets {第二 {Java {srcDir'second/loc/src/main/java'}}}compileJava {源+ = sourceSets.second.java}依赖项{实施'com.google.guava:guava:29.0-jre'testImplementation'org.junit.jupiter:junit-jupiter-api:5.6.2'testRuntimeOnly'org.junit.jupiter:junit-jupiter-engine:5.6.2'}应用 {mainClassName ='a.aa.App'}测试 {useJUnitPlatform()} 

主类可以访问 Foo :

 包a.aa;导入b.bb.Foo;公共类应用{公共静态void main(String [] args){System.out.println(new Foo().sayFoo());}} 

什么不起作用

现在,我尝试使用 openapi-generator 生成的代码进行相同的操作:

plugins 下,我添加 id"org.openapi.generator"版本"4.3.1"

然后我添加了一个新任务:

  openApiGenerate {generatorName ="java";inputSpec ="$ rootDir/specs/petstore.yaml" .toString()outputDir ="$ buildDir/Generated" .toString()apiPackage ="org.openapi.example.api"invokerPackage ="org.openapi.example.invoker"modelPackage ="org.openapi.example.model"configOptions = [dateLibrary:"java8"]} 

然后我执行任务 openApiGenerate 并在文件系统中确认已生成源(eclipse将不会显示build文件夹).现在,我使用与上面相同的方法在 build.gradle

中生成

 插件{id'java'id应用程序"id"org.openapi.generator"版本"4.3.1"}储存库{jcenter()}openApiGenerate {generatorName ="java";inputSpec ="$ rootDir/specs/petstore.yaml" .toString()outputDir ="$ buildDir/Generated" .toString()apiPackage ="org.openapi.example.api"invokerPackage ="org.openapi.example.invoker"modelPackage ="org.openapi.example.model"configOptions = [dateLibrary:"java8"]}sourceSets {客户 {Java {srcDir'$ buildDir/Generated/src/main/java'}}}compileJava {源+ = sourceSets.client.java}依赖项{实施'com.google.guava:guava:29.0-jre'testImplementation'org.junit.jupiter:junit-jupiter-api:5.6.2'testRuntimeOnly'org.junit.jupiter:junit-jupiter-engine:5.6.2'}应用 {mainClassName ='a.aa.App'}测试 {useJUnitPlatform()} 

但是当我现在尝试使用这些类时:

 包a.aa;导入org.openapi.example.model.Pet;公共类应用{公共静态void main(String [] args){宠物p =新宠物(0L);System.out.println(p.getId());}} 

导入或宠物都无法解析.

 >任务:compileJava失败C:\ ... \ simple-java-app \ src \ main \ java \ a \ aa \ App.java:6:错误:包org.openapi.example.model不存在导入org.openapi.example.model.Pet;^C:\ ... \ simple-java-app \ src \ main \ java \ a \ aa \ App.java:14:错误:找不到符号宠物p =新宠物(0);^符号:宠物类位置:类AppC:\ ... \ simple-java-app \ src \ main \ java \ a \ aa \ App.java:14:错误:找不到符号宠物p =新宠物(0);^符号:宠物类位置:类App3个错误失败:生成失败,发生异常. 

我不知道如何调试它,坦率地说,我不确定源集是否正确.所有openapi-generator教程的教程似乎都在使用它们,我还没有尝试过子项目,openApiGenerate任务似乎使用 build.gradle 和所有内容创建了一个完整的项目.

解决方案

您需要将源代码从生成的代码添加到您的项目中.我的一个项目中的一个示例:

sourceSets.main.java.srcDir

生成后,请确保刷新gradle和项目.

I want to create a gradle java application that generates a client from an openAPI specification file and uses that client. So I created a java application with gradle init (type:application, language:Java, DSL:groovy, test-framework:Junit Jupiter, project-name:simple-java-app, package-structure:a.aa).

Small example of what works:

I can create a new source folder second/loc/src/main/java with a package b.bb and a class Foo. And with the following build.gradle

plugins {
    id 'java'
    id 'application'
}

repositories {
    jcenter()
}

sourceSets {
    second {
        java {
            srcDir 'second/loc/src/main/java'
        }
    }
}

compileJava {
    source += sourceSets.second.java
}

dependencies {
    implementation 'com.google.guava:guava:29.0-jre'
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.2'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.6.2'
}

application {
    mainClassName = 'a.aa.App'
}

test {
    useJUnitPlatform()
}

The main class can access Foo:

package a.aa;

import b.bb.Foo;

public class App {

    public static void main(String[] args) {
        System.out.println(new Foo().sayFoo());
    }
}

What doesn't work

Now I try the same for generated code by openapi-generator:

Under plugins I add id "org.openapi.generator" version "4.3.1"

And I add a new task:

openApiGenerate {
    generatorName = "java"
    inputSpec = "$rootDir/specs/petstore.yaml".toString()
    outputDir = "$buildDir/generated".toString()
    apiPackage = "org.openapi.example.api"
    invokerPackage = "org.openapi.example.invoker"
    modelPackage = "org.openapi.example.model"
     configOptions = [
        dateLibrary: "java8"
    ]
}

Then I execute the task openApiGenerate and confirm in the file system that the sources have been generated(eclipse won't show the build folder). Now I use the same method as above resulting in below build.gradle

plugins {
    id 'java'
    id 'application'
    id "org.openapi.generator" version "4.3.1"
}

repositories {
    jcenter()
}

openApiGenerate {
    generatorName = "java"
    inputSpec = "$rootDir/specs/petstore.yaml".toString()
    outputDir = "$buildDir/generated".toString()
    apiPackage = "org.openapi.example.api"
    invokerPackage = "org.openapi.example.invoker"
    modelPackage = "org.openapi.example.model"
    configOptions = [
        dateLibrary: "java8"
    ]
}

sourceSets {
    client {
        java {
            srcDir '$buildDir/generated/src/main/java'
        }
    }
}

compileJava {
    source += sourceSets.client.java
}

dependencies {
    implementation 'com.google.guava:guava:29.0-jre'
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.2'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.6.2'
}

application {
    mainClassName = 'a.aa.App'
}

test {
    useJUnitPlatform()
}

But when I try to use the classes now:

package a.aa;

import org.openapi.example.model.Pet;

public class App {

    public static void main(String[] args) {
        Pet p = new Pet(0L);
        System.out.println(p.getId());
    }
}

neither import nor Pet can be resolved.

> Task :compileJava FAILED
C:\...\simple-java-app\src\main\java\a\aa\App.java:6: error: package org.openapi.example.model does not exist
import org.openapi.example.model.Pet;
                                ^
C:\...\simple-java-app\src\main\java\a\aa\App.java:14: error: cannot find symbol
        Pet p = new Pet(0);
        ^
  symbol:   class Pet
  location: class App
C:\...\simple-java-app\src\main\java\a\aa\App.java:14: error: cannot find symbol
        Pet p = new Pet(0);
                    ^
  symbol:   class Pet
  location: class App
3 errors

FAILURE: Build failed with an exception.

I don't know how to debug this, frankly I'm unsure if source sets are even the right way. All openapi-generator tutorials tutorials seem to use them, I haven't tried subprojects yet, the openApiGenerate task seems to create a complete project with build.gradle and everything.

解决方案

You need to add the sources from the generated code to your project. One example from one of my projects:

sourceSets.main.java.srcDir "${buildDir}/generated/src/main/java"

After generation make sure you refresh gradle and project.

这篇关于如何在gradle Java应用程序中包含来自openapi-generator的客户端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆