java 在多模块Maven项目中使用OpenApi生成器生成API层

fhity93d  于 2023-06-28  发布在  Java
关注(0)|答案(1)|浏览(280)

Hello Maven社区
我有一个多模块的Maven项目(用于我的Sping Boot 应用程序),我想使用OpenAPI generator - Maven plugin生成API层。
让我来解释一下结构,最后你可以看到一个链接到一个repo with project。
基本上有三个pom.xml。第一个pom是项目级的,它有<dependencyManagement>,其他模块从其中继承依赖项并指定其他版本作为合适的版本。第二个pom在模块company-gaming-rest-api中,这个模块的想法是它不包含任何源代码,只包含用于生成API层的yaml文件沿着一些DTO。最后,第二个pom.xml来自一个名为company-gaming-service-core的模块。这个模块的想法是实际包含业务逻辑,并在其/target文件夹下包含所有生成的API。
所以在company-gaming-service-core pom.xml中,我使用了这样的插件:

<plugin>
                <groupId>org.openapitools</groupId>
                <artifactId>openapi-generator-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>gaming-generation</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <inputSpec>${project.basedir}/target/company/gaming/game.yaml</inputSpec>
                            <generatorName>spring</generatorName>
                            <apiPackage>company.gaming.ms.game.controller</apiPackage>
                            <modelPackage>company.gaming.ms.game.model</modelPackage>
                            <generateSupportingFiles>true</generateSupportingFiles>
                            <configOptions>

                                <enableBuilderSupport>true</enableBuilderSupport>
                                <interfaceOnly>true</interfaceOnly>
                                <dateLibrary>java11</dateLibrary>
                                <serializableModel>true</serializableModel>
                                <delegatePattern>true</delegatePattern>
                                <useBeanValidation>true</useBeanValidation>
                                <hideGenerationTimestamp>true</hideGenerationTimestamp>
                                <simpleFluentPattern>true</simpleFluentPattern>
                                <collectionFluentPattern>true</collectionFluentPattern>
                            </configOptions>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

整个项目只使用两个库,mapstructlombok,因为在我的拙见中,API生成器需要它们来生成API层的类/方法。
我使用的是IntelliJ,所以这里是一个干净的项目的图像:

以下是mvn clean install -DskipTests之后的项目结构:

mvn clean install -DskipTests之后,我得到的错误非常奇怪:

[INFO] Configured Artifact: company.gaming.ms:company-gaming-rest-api:0.0.1-SNAPSHOT:jar
[INFO] Unpacking C:\Users\FirstName.Lastname\Desktop\company-gaming-service\company-gaming-rest-api\target\company-gaming-rest-api-0.0.1-SNAPSHOT.jar to C:\Users\FirstName.Lastname\Desktop\company-gaming-service\company-gaming-service-core\target with includes "**/*.yaml" and excludes ""
[INFO] 
[INFO] --- openapi-generator-maven-plugin:4.0.2:generate (company-gaming) @ company-gaming-service-core ---
[WARNING] Exception while reading:
java.lang.RuntimeException: **Could not find C:/Users/FirstName.Lastname/Desktop/company-gaming-service/company-gaming-service-core/target/company/gaming/game.yaml on the classpath**
    at io.swagger.v3.parser.util.ClasspathHelper.loadFileFromClasspath (ClasspathHelper.java:31)
    at io.swagger.v3.parser.OpenAPIV3Parser.readWithInfo (OpenAPIV3Parser.java:135)
    at io.swagger.v3.parser.OpenAPIV3Parser.readLocation (OpenAPIV3Parser.java:45)
    at io.swagger.parser.OpenAPIParser.readLocation (OpenAPIParser.java:16)
    at org.openapitools.codegen.config.CodegenConfigurator.toClientOptInput (CodegenConfigurator.java:586)

它说它在classpath上找不到,但它设法将它从rest模块复制到核心模块。我确实玩过其他版本的mapstruct/openapi插件,但运气不好:(
这里是link to the repo
Yaml架构正确,并针对Swagger Editor进行了验证。

更新1基于khmarbaise的建议:

我已经在核心模块插件中注解了与API生成器相关的内容。现在,在我的rest-api模块中,我有以下内容:

<build>
    <plugins>
        

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <id>unpack</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>unpack</goal>
                    </goals>
                    <configuration>

                        <artifactItems>
                            <artifactItem>
                                <groupId>company.gaming.ms</groupId>
                                <artifactId>company-gaming-rest-api</artifactId>
                                <version>0.0.1-SNAPSHOT</version>
                                <type>jar</type>
                                <overWrite>false</overWrite>
                            </artifactItem>
                        </artifactItems>
                        <includes>**/*.yaml</includes>
                        <outputDirectory>${project.build.directory}</outputDirectory>
                        <overWriteReleases>true</overWriteReleases>
                        <overWriteSnapshots>true</overWriteSnapshots>
                    </configuration>
                </execution>

            </executions>

        </plugin>

        <plugin>
            <groupId>org.openapitools</groupId>
            <artifactId>openapi-generator-maven-plugin</artifactId>
            <executions>
                <execution>
                    <id>company-gaming-api-generation</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                    <configuration>

                        <!--${project.basedir}

                        C:/Users/FirstName.LastName/Desktop/repo/company-gaming-service/company-gaming-rest-api/src/main/resources/company.gaming/game.yaml-->
                        <inputSpec>${project.basedir}\src\main\resource\company\gaming\game.yaml</inputSpec>
                        <generatorName>spring</generatorName>
                        <apiPackage>company.gaming.ms.game.controller</apiPackage>
                        <modelPackage>company.gaming.ms.game.model</modelPackage>
                        <generateSupportingFiles>true</generateSupportingFiles>
                        <configOptions>

                            <enableBuilderSupport>true</enableBuilderSupport>
                            <interfaceOnly>true</interfaceOnly>
                            <dateLibrary>java11</dateLibrary>
                            <serializableModel>true</serializableModel>
                            <delegatePattern>true</delegatePattern>
                            <useBeanValidation>true</useBeanValidation>
                            <hideGenerationTimestamp>true</hideGenerationTimestamp>
                            <simpleFluentPattern>true</simpleFluentPattern>
                            <collectionFluentPattern>true</collectionFluentPattern>
                        </configOptions>
                    </configuration>
                </execution>
            </executions>
        </plugin>

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <executions>
                <execution>
                    <id>add-client-sources</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>add-source</goal>
                    </goals>
                    <configuration>
                        <sources>
                            <source>${project.build.directory}/generated-sources/openapi/src/main/java</source>
                            <source>${project.build.directory}/generated-sources/annotations</source>
                            <source>${project.build.directory}/generated-sources/xjc-client</source>
                        </sources>
                    </configuration>
                </execution>
            </executions>
        </plugin>

    </plugins>
</build>

尝试与/\company.gamingcompany/gaming它只是继续为我服务:

[INFO] --- openapi-generator-maven-plugin:6.6.0:generate (gaming-api-generation) @ company-gaming-service-core ---
[WARNING] Exception while reading:
io.swagger.v3.parser.exception.ReadContentException: Unable to read location `main/company-gaming-rest-api/src/main/resources/company.gaming/game.yml`
    at io.swagger.v3.parser.OpenAPIV3Parser.readContentFromLocation (OpenAPIV3Parser.java:295)
    at io.swagger.v3.parser.OpenAPIV3Parser.readLocation (OpenAPIV3Parser.java:92)

请注意,错误时它输出.yml而不是.yaml

fiei3ece

fiei3ece1#

然后Open API maven generator plugin在类(和资源)编译到target文件夹之前运行,因为它必须通过YAML生成类,而YAML是你自己的项目类引用的,所以这就是为什么它找不到YAML文件到目标文件夹的原因。
为了解决这个问题,你必须改变:<inputSpec>${project.basedir}/target/company/gaming/game.yaml</inputSpec>
进入
<inputSpec>${project.basedir}/company-gaming-service/src/main/resources/company/gaming/game.yaml</inputSpec>

相关问题