POM中的编译错误,原因是使用openApi生成代码



所以我使用的是openApi代码生成器。(所有这些都适用于Swagger代码生成,同样的错误(

我在eclipse中创建了一个maven项目,我有一个POM,看起来像这样。。。

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test.api.openapi</groupId>
<artifactId>test-openapi-codegen</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>test-openapi-codegen</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>4.3.1</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>${project.basedir}/src/main/resources/openAPI.yaml</inputSpec>
<generatorName>java</generatorName>
<apiPackage>com.test.openapi.codegen.demo.api</apiPackage>
<modelPackage>com.test.openapi.codegen.demo.model</modelPackage>
<invokerPackage>com.test.openapi.codegen.demo</invokerPackage>
<configOptions>
<sourceFolder>src/java/</sourceFolder>
</configOptions>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>4.3.1</version>
<scope>provided</scope>
</dependency> 
</dependencies>
</project>

因此,这个POM使用一个YAML文件来生成JAVA代码,YAML文件充当要生成的代码的架构/结构文件。

这里有一个链接:[https://openapi-generator.tech/docs/plugins/][1] -->链接中提到了我使用的插件。

因此,当我进行mvn干净编译时,代码是在标记->中提到的输出文件夹中生成的;src/java/生成的代码->像这样。也正在为所生成的代码生成POM;mvn clean compile";我得到下面的错误,说很多";缺少个包(共个(。读下两行。

这是一个陷阱,我检查了生成的POM,并且POM中存在严格的依赖关系,我也使用";MVN COMPILE";并成功构建了POM

Changes detected - recompiling the module!
[INFO] Compiling 43 source files to T:PerforcetestStreamopenapi-codegentargetclasses
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] T:/Perforce/testStream/openapi-codegen/target/generated-sources/openapi/src/java/com/test/openapi/codegen/demo/model/ProvisioningBatch.java:[18,23] package com.google.gson does not exist

我做错了什么?我已经尽可能好地描述了。

我通过添加缺失的依赖项解决了这个问题,但我认为这不是最好的方法,最好使用cli jar来满足需要。

从mavencentral下载jar,openapi-codegen-cli.jar及其可运行的jar,并根据req使用参数。

它将从YAML生成一个代码,然后您可以使用生成的POM来mvn clean install或任何您想要的

最新更新