Sub topic 15
Generating the specification during the build
This chapter assumes you have a Spring Boot 3 project with springdoc-openapi on the classpath, so that /v3/api-docs already returns JSON. It also assumes you can read a Maven pom.xml or a Gradle build file and know the basic build lifecycle.
Why generate the specification at build time
So far the OpenAPI document existed only while the application was running. You had to start the app and request /v3/api-docs. Many tasks need the document as a file instead:
- Client code generators, such as OpenAPI Generator, read a specification file.
- API gateways and developer portals import a file.
- Contract checks in continuous integration (CI) compare the current specification with the last released one.
- Reviewers can see a specification change in a pull request when the file is committed.
springdoc builds the document from the running application context, so it cannot read your controllers without starting them. The build plugins work around this. They start the application, download the JSON from the documentation endpoint, write it to a file, and stop the application.
Maven setup
The Maven plugin runs in the integration-test phase. The application must be running by then, so you pair it with the start and stop goals of spring-boot-maven-plugin. Those goals run in pre-integration-test and post-integration-test.
<build>
<plugins>
<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>${springdoc.maven.plugin.version}</version>
<executions>
<execution>
<id>integration-test</id>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<apiDocsUrl>http://localhost:8080/v3/api-docs</apiDocsUrl>
<outputDir>${project.build.directory}</outputDir>
<outputFileName>openapi.json</outputFileName>
</configuration>
</plugin>
</plugins>
</build>${springdoc.maven.plugin.version} is a property you define in the <properties> section. Look up the current release of springdoc-openapi-maven-plugin and set it there.
Run the build up to the verify phase:
mvn verifyThe file appears at target/openapi.json. Because the plugin is bound to integration-test, mvn package does not run it, and neither does mvn test. This is a common surprise.
The configuration options you will use most are:
| Option | Meaning | Default |
|---|---|---|
| `apiDocsUrl` | URL the plugin downloads from | `http://localhost:8080/v3/api-docs` |
| `outputDir` | Directory for the output file | `target` |
| `outputFileName` | Name of the output file | `openapi.json` |
| `skip` | Skip generation | `false` |
| `headers` | Extra request headers for the download | none |
If you changed springdoc.api-docs.path in an earlier chapter, update apiDocsUrl to match. The plugin does not read your application properties.
Gradle setup
In Gradle, one plugin does both jobs. It starts the application with bootRun in a forked process, downloads the document, and stops the process.
plugins {
java
id("org.springframework.boot") version "3.4.0"
id("io.spring.dependency-management") version "1.1.6"
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")
waitTimeInSeconds.set(60)
}Run the task by name:
./gradlew generateOpenApiDocsThe file is written to build/docs/openapi.json. waitTimeInSeconds is how long the plugin waits for the application to answer before it fails. Raise it if your application starts slowly.
What the build needs from the application
The plugin starts your real application. That requirement causes most build-time failures.
- Port. The application must listen on the port in
apiDocsUrl. If another process on the build machine uses port 8080, generation fails or reads the wrong service. For a safer setup, setserver.portexplicitly for the generation run and put the same port inapiDocsUrl. - External services. If startup needs a database, a message broker, or a remote configuration server, the CI machine must provide them. Otherwise the application never becomes ready.
- Profile. Create a dedicated profile for generation. It can use an in-memory database and disable scheduled jobs and message listeners. Activate it only for the generation run.
- Security. If the documentation endpoints require authentication, the download fails with 401 or 403. Either permit them in that profile or pass a header through the plugin's
headersoption. - Documentation enabled. If you set
springdoc.api-docs.enabled=false, the endpoint does not exist and there is nothing to download. Keep it enabled for the generation profile, even if production disables it.
You can activate a profile for the Maven start goal like this:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<profiles>
<profile>openapi</profile>
</profiles>
</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>A matching application-openapi.yml could then hold the settings for the build run:
server:
port: 8080
spring:
datasource:
url: jdbc:h2:mem:openapiGrouped APIs
If you defined GroupedOpenApi beans, each group has its own document at /v3/api-docs/{group}. The plugins download one URL per run. To get a file per group, point apiDocsUrl at the group path. In Maven you can declare several executions of the generate goal. Each execution has its own apiDocsUrl and outputFileName.
<execution>
<id>generate-public</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<apiDocsUrl>http://localhost:8080/v3/api-docs/public</apiDocsUrl>
<outputFileName>openapi-public.json</outputFileName>
</configuration>
</execution>Put this execution inside the springdoc-openapi-maven-plugin entry. Because it sets outputFileName itself, it overrides the shared value from the plugin-level configuration.
Using the generated file
Generating the file is only useful if something consumes it. Three common patterns:
- Publish as a build artifact. Your CI system stores
openapi.jsonwith the build, and downstream teams download it. - Commit it to the repository. Reviewers then see API changes as a diff. This works best with a check that fails the build when the committed file differs from the freshly generated one. Without that check, the committed copy drifts out of date.
- Feed a code generator. A client SDK build reads the file and produces typed clients.
A drift check can be as simple as a CI step after mvn verify:
mvn verify
diff -u docs/openapi.json target/openapi.jsondiff exits with a non-zero status when the files differ, which fails the CI job. A reliable comparison needs stable output. If the specification changes between runs with no code change, look for values that vary, such as timestamps or random example values, and remove them.
Common problems
| Symptom | Likely cause |
|---|---|
| No file after `mvn package` | The plugin runs in `integration-test`. Use `mvn verify`. |
| Build hangs at startup | The application waits for an unavailable database or service. |
| Connection refused | The app uses a different port than `apiDocsUrl`. |
| 401 or 403 during generation | The docs endpoints are secured in the generation profile. |
| File is missing a group's endpoints | `apiDocsUrl` points at the default document, not `/v3/api-docs/{group}`. |
| Gradle task times out | Startup takes longer than `waitTimeInSeconds`. |
Build-time generation uses a running application, so it shows what the application would serve in that profile. If a profile hides endpoints through springdoc.paths-to-match or conditional beans, the file reflects that. Use the same configuration that you want documented.
Exercise
Your team runs mvn package in CI and expects target/openapi.json to exist afterward, but the file is never created. The springdoc Maven plugin is configured with the generate goal. What is wrong, and what two other plugin executions must be present for generation to work?
Show answer
The generate goal is bound to the integration-test phase, which comes after package, so mvn package stops before it runs. Run mvn verify instead. The build also needs spring-boot-maven-plugin executions of the start goal in pre-integration-test and the stop goal in post-integration-test. Without them, no application is running when the springdoc plugin requests /v3/api-docs.
Exercise
Generation works on your laptop but fails in CI with "connection refused" even though the application starts. Name two causes related to configuration, and one way to remove the port-related risk.
Show answer
Possible causes are that the application listens on a different port than the one in apiDocsUrl (for example, server.port is set by an environment variable in CI), and that the docs path was changed with springdoc.api-docs.path while apiDocsUrl still uses /v3/api-docs. To remove the port risk, set server.port explicitly in a dedicated generation profile and use the same port in apiDocsUrl.