Blog

It's minimal, but I'm posting things.


Autogenerating an OpenAPI json file using Maven

Using Maven it can be useful to generate and version a OpenAPI json file after the integration-test phase.

The following pom.xml configuration will start a separate instance of your application on a specified port, and once the integration-test phase is successfully done, then the plugin generates the json file openapi.json.

<profiles>
  <profile>
    <!-- Json OpenAPI profile -->
    <id>springdocs</id>
    <activation>
      <activeByDefault>false</activeByDefault>
    </activation>
    <properties>
      <springdocs.host>http://localhost</springdocs.host>
      <springdocs.port>8090</springdocs.port>
    </properties>
    <dependencies>
      <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>${h2.version}</version>
      </dependency>
      <dependency>
        <groupId>org.springdoc</groupId>
        <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
        <version>${springdoc-openapi-starter-webmvc-ui.version}</version>
      </dependency>
    </dependencies>
    <build>
      <plugins>
        <plugin>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-maven-plugin</artifactId>
          <configuration>
            <jvmArguments>-Dserver.port=${springdocs.port} -Dspring.profiles.active=test -Dspring.application.admin.enabled=true</jvmArguments>
          </configuration>
          <executions>
            <execution>
              <id>pre-integration-test</id>
              <goals>
                <goal>start</goal>
              </goals>
            </execution>
            <execution>
              <id>post-integration-test</id>
              <goals>
                <goal>stop</goal>
              </goals>
            </execution>
          </executions>
        </plugin>
        <plugin>
          <!-- Ensure generation of OpenAPI json specification -->
          <groupId>org.springdoc</groupId>
          <artifactId>springdoc-openapi-maven-plugin</artifactId>
          <version>${org.springdoc-version}</version>
          <configuration>
            <apiDocsUrl>
														${springdocs.host}:${springdocs.port}/v3/api-docs
												</apiDocsUrl>
            <outputFileName>openapi.json</outputFileName>
            <outputDir>${project.build.directory}/springdoc</outputDir>
          </configuration>
          <executions>
            <execution>
              <id>integration-test</id>
              <goals>
                <goal>generate</goal>
              </goals>
            </execution>
          </executions>
        </plugin>
      </plugins>
    </build>
  </profile>
</profiles>

Published on 2024-08-21T19:43:19.906427Z
Last updated on 2024-08-21T19:43:19.906427Z