在OpenAPI规范定义中使用工件版本号

时间:2019-09-06 12:07:24

标签: maven swagger swagger-ui openapi openapi-generator

我有一个使用OpenaAPI v3规范定义API的maven项目。

我使用openapi-generator-maven-plugin生成代码,并且一切正常。我还可以访问swagger-ui并查看和测试我的API。

问题是我不想两次维护版本号。因此,我想在我的api规范中引用我的maven pom的版本号,而不要复制它,以免它过时。

我尝试了Maven资源过滤,该方法似乎有效。由于当我在规范的版本字段中放入${project.version}时,可以很好地替换目标文件夹中的yaml文件,但是当我打开swagger-ui时,它将按字面意思显示“ $ {project.version}”,而不是实际版本。

这是我pom的插件配置:

<build>
      <resources>
        <resource>
          <directory>src/main/resources</directory>
          <filtering>true</filtering>
        </resource>
      </resources>
      <plugins>
        <plugin>
          <groupId>org.openapitools</groupId>
          <artifactId>openapi-generator-maven-plugin</artifactId>
          <version>${openapi-generator-maven-plugin.version}</version>
          <executions>
            <execution>
              <goals>
                <goal>generate</goal>
              </goals>
              <configuration>
                <inputSpec>${project.build.resources[0].directory}/spec.yml</inputSpec>
                <ignoreFileOverride>${project.build.resources[0].directory}/.openapi-codegen-ignore</ignoreFileOverride>
                <language>spring</language>
                <library>spring-boot</library>
                <configOptions>
                  <!-- Use the newer java.time package instead of outdated java.util-->
                  <dateLibrary>java8</dateLibrary>
                </configOptions>
                <apiPackage>${default.package}.api</apiPackage>
                <modelPackage>${default.package}.model</modelPackage>
                <invokerPackage>${default.package}.invoker</invokerPackage>
                <generateApiTests>false</generateApiTests>
              </configuration>
            </execution>
          </executions>
       </plugin>
       ...
    </plugins>
</build>

这是我的spec.yml:

openapi: 3.0.0
info:
  version: ${project.version}
...

1 个答案:

答案 0 :(得分:1)

感谢@bcoughlan的评论,我得以按需运行maven-resources-plugin。

现在,它需要从src / main / resources文件夹中过滤* yml文件,并在generates-sources阶段替换$ {project.version}占位符。由于openapi-generator-maven-plugin在同一阶段执行并在以后定义,因此它将在资源过滤后执行,并且由于${project.build.outputDirectory}/spec.yml

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-resources-plugin</artifactId>
        <version>3.1.0</version>
        <configuration>
          <resources>
            <resource>
              <directory>src/main/resources</directory>
              <filtering>true</filtering>
            </resource>
          </resources>
        </configuration>
        <executions>
          <execution>
            <phase>generate-sources</phase>
            <goals>
              <goal>resources</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <!-- Codegen plugin for api. Depends on maven-resources-plugin to have executed in the generate-sources phase! -->
      <plugin>
        <groupId>org.openapitools</groupId>
        <artifactId>openapi-generator-maven-plugin</artifactId>
        <version>${openapi-generator-maven-plugin.version}</version>
        <executions>
          <execution>
            <goals>
              <goal>generate</goal>
            </goals>
            <configuration>
                <inputSpec>${project.build.outputDirectory}/spec.yml</inputSpec>
                <ignoreFileOverride>${project.basedir}/.openapi-codegen-ignore</ignoreFileOverride>
              <language>spring</language>
              <library>spring-boot</library>
              <configOptions>
                <!-- Use the newer java.time package instead of outdated java.util-->
                <dateLibrary>java8</dateLibrary>
              </configOptions>
              <apiPackage>${default.package}.api</apiPackage>
              <modelPackage>${default.package}.model</modelPackage>
              <invokerPackage>${default.package}.invoker</invokerPackage>
              <generateApiTests>false</generateApiTests>
            </configuration>
          </execution>
        </executions>
      </plugin>

注意:openapi-generator-maven-plugin与swagger-codegen-maven-plugin几乎相同。有关差异的更多信息,请参见:https://openapi-generator.tech/docs/fork-qna

注意2:使用 spring-boot 父pom时,您将继承resource.delimeter = @属性,并且${project.version}表示法不再起作用。因此,将占位符替换为@project.version@应该可以解决此问题。