Sub topic 15
Generating the specification during the build
This chapter assumes you have a working springdoc setup, where /v3/api-docs returns JSON while the application runs. It also assumes you use Maven or Gradle.
Why generate at build time
springdoc builds the OpenAPI document by inspecting controllers in a running application. Sometimes you need the document as a file, without anyone starting the app by hand. Common uses are:
- Generating client SDKs in CI.
- Publishing the specification to an API portal.
- Diffing the specification between commits to catch breaking changes.
The build plugins solve this by starting your application, downloading /v3/api-docs, saving it to a file and stopping the application.
Maven
Two plugins cooperate. The Spring Boot plugin starts and stops the application around the integration-test phase. The springdoc plugin runs in between and downloads the document.
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<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>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-maven-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<id>integration-test</id>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<apiDocsUrl>http://localhost:8080/v3/api-docs</apiDocsUrl>
<outputFileName>openapi.json</outputFileName>
<outputDir>${project.build.directory}</outputDir>
</configuration>
</plugin>Run mvn verify. The file appears at target/openapi.json. A plain mvn package does not reach the integration-test phase, so no file is produced.
Gradle
The springdoc Gradle plugin adds a generateOpenApiDocs task. It starts the application with bootRun, fetches the document and stops the application.
plugins {
id("org.springframework.boot") version "3.3.0"
id("org.springdoc.openapi-gradle-plugin") version "1.9.0"
}
openApi {
apiDocsUrl.set("http://localhost:8080/v3/api-docs")
outputDir.set(layout.buildDirectory.dir("docs"))
outputFileName.set("openapi.json")
}Run ./gradlew generateOpenApiDocs. The output is build/docs/openapi.json.
Common errors
- Port conflict. The plugin calls the URL you configure. If port 8080 is taken, or the app uses a different port or context path,
apiDocsUrlmust match it. - Docs disabled. If you set
springdoc.api-docs.enabled=falsein the profile used for the build, the endpoint returns 404. Disable the docs only in the production profile. - Secured endpoint. If Spring Security blocks
/v3/api-docs, the plugin gets a 401 or 403. Allow the path in the build profile. - Stale output. The file reflects the code at build time. Commit it only if your team regenerates it on every change.
Exercise
A teammate runs mvn package and cannot find openapi.json in target. The plugins are configured as shown above. What is the cause, and what command fixes it?
Show answer
The springdoc generate goal is bound to the integration-test phase, and mvn package stops before that phase. Running mvn verify (or mvn integration-test) starts the application, generates the file and stops the application.