使 swagger codegen maven 插件从另一个 maven 依赖项访问 yaml 文件

jbx*_*jbx 5 java maven swagger swagger-codegen swagger-codegen-maven-plugin

我有一个用 Swagger 编写的 API,我想为其生成服务实现和客户端,并且它们必须位于单独的 maven 模块中。

我正在考虑将它们分成 3 个单独的 Maven 模块(或同一父 pom 的子模块)。

parent
 +- api
    +- src/main/resources/api/service.yaml
 +- client
 +- service
Run Code Online (Sandbox Code Playgroud)

然后在客户端和服务中我都会有swagger-codegen-maven-plugin. 这样两者都会同步,我只会在一个地方维护服务。其他客户端也可以依赖于api工件并从service.yamlSwagger API 定义生成它们的代码。

我的难点是如何让服务和客户端service.yaml在另一个 Maven 依赖项中引用?

这是我目前在 service 中所拥有的pom.xml,但它指的是 service 模块的本地资源,而不是apimaven 依赖项。

 <plugin>
        <groupId>io.swagger</groupId>
        <artifactId>swagger-codegen-maven-plugin</artifactId>
        <version>${io.swagger.codegen.version}</version>
        <executions>
          <execution>
            <id>api</id>
            <goals>
              <goal>generate</goal>
            </goals>
            <configuration>            
<!-- can this refer to another maven dependency's resources? --> 
<inputSpec>${basedir}/src/main/resources/api/service.yaml</inputSpec>
              <language>spring</language>
              <library>spring-boot</library>
              <modelPackage>com.test.model</modelPackage>
              <apiPackage>com.test.api</apiPackage>
              <generateSupportingFiles>false</generateSupportingFiles>
              <configOptions>
                <java8>true</java8>
                <dateLibrary>java8</dateLibrary>
                <interfaceOnly>true</interfaceOnly>
              </configOptions>
            </configuration>
          </execution>
       </executions>
    </plugin>
Run Code Online (Sandbox Code Playgroud)

不确定这是我必须从 Maven 做的事情,从另一个 Maven 依赖项中引用资源,还是我必须在 swagger 插件配置中做的事情。

jbx*_*jbx 6

我设法找到的解决方案是使用maven-remote-resources-plugin. 在pom.xml需要公开资源的maven项目中,您可以放置​​:

<build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-remote-resources-plugin</artifactId>
        <executions>
          <execution>
            <goals>
              <goal>bundle</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <includes>
            <include>**/*.yaml</include>
          </includes>
        </configuration>
      </plugin>
    </plugins>
  </build>
Run Code Online (Sandbox Code Playgroud)

然后在需要导入它们的项目中,需要引用该项目如下:

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-remote-resources-plugin</artifactId>
    <configuration>
      <resourceBundles>
        <resourceBundle>group:api:version</resourceBundle>
      </resourceBundles>
    </configuration>
    <executions>
      <execution>
        <phase>
          generate-sources
        </phase>
        <goals>
          <goal>process</goal>
        </goals>
      </execution>
    </executions>
  </plugin>
Run Code Online (Sandbox Code Playgroud)

其中group:api:version是公开资源的 Maven 依赖项的组 ID、工件 ID 和版本。

最后,在swagger-codegen-maven-plugin配置里面,yaml文件可以参考:

<inputSpec>${project.build.directory}/maven-shared-archive-resources/api/service.yaml</inputSpec>
Run Code Online (Sandbox Code Playgroud)