代码之家  ›  专栏  ›  技术社区  ›  jbx

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

  •  2
  • jbx  · 技术社区  · 6 年前

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

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

    然后,在客户端和服务中,我将拥有 swagger-codegen-maven-plugin . 这样,两者将同步,我将只从一个地方维护服务。其他客户端也可以依赖于 api 工件并从 service.yaml 大摇大摆的API定义。

    我的困难是如何让服务和客户参考 在另一个Maven依赖中?

    这是我目前在服务中拥有的 pom.xml ,但它指的是服务模块的本地资源,而不是 应用程序编程接口

     <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 回复  |  直到 6 年前
        1
  •  3
  •   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>
    

      <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 是暴露资源的maven依赖项的组ID、工件ID和版本。

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

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