使swagger Codegen Maven插件从另一个Maven依赖项访问yaml文件

时间:2018-11-18 13:12:43

标签: java maven swagger swagger-codegen swagger-codegen-maven-plugin

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

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

parent
 +- api
    +- src/main/resources/api/service.yaml
 +- client
 +- service

然后在客户端和服务端我都将拥有swagger-codegen-maven-plugin。这样,两者将保持同步,而我只会在一个地方维护该服务。其他客户端也可以依赖api工件,并根据service.yaml Swagger API定义生成其代码。

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

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

 <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>

不确定这是我必须在Maven中执行的操作,还是要引用来自其他maven依赖项的资源,还是在swagger插件配置中必须执行的操作。

1 个答案:

答案 0 :(得分:1)

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

<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>

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

  <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>

其中group:api:version是组ID,工件ID和暴露资源的Maven依赖版本。

最后,在swagger-codegen-maven-plugin配置中,yaml文件可以称为:

<inputSpec>${project.build.directory}/maven-shared-archive-resources/api/service.yaml</inputSpec>
相关问题