These plugins will create an openapi.yaml file based on my REST Controller:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>3.0.4</version>
<configuration>
<jvmArguments>-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>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-maven-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<id>integration-test</id>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<outputFileName>openapi.yaml</outputFileName>
</configuration>
</plugin>
The file will be created when i run mvn verify
.mvn verify
will also run my tests which I dont need. When I run mvn verify
in my dockerfile, it gives me this error:
"Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections."
This happens because my database container is not created/running before mvn verify
. If I run mvn package -DskipTests there is no problem.
Is there a way I can change the phase to package which will trigger the plugin and creates a yaml for me? I dont need the tests.
I tried with <phase>package</phase>
but this would still run the tests which will cause me an error:
As described in the documentation the file can only be generated while the application is up and running. That's why you are already using Spring Boot maven plugin goals start
and stop
. This way your application is running when the openapi
generate the yaml file. This is the correct setup. However it means that the application.properties
file will be used and as consequence the real database will be connected.
I suggest to configure an embedded in-memory database instead of the target one. Spring Boot JPA will create it during the startup of application. To get it work without changing the original application.properties
you can use a dedicated profile. Let's name it inmemory
.
Step 1:
Add H2 dependency to the pom.xml like this:
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
Step 2:
Create a file application-inmemory.properties
in the src/main/resources
folder with this content:
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:db
spring.datasource.username=sa
spring.datasource.password=sa
Step 3:
Define active profile in pom.xml
like this:
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<jvmArguments>-Dspring.profiles.active=inmemory</jvmArguments>
</configuration>
Now you can just run mvn verify
. In the logs you will see that in the pre-integration-test
phase the jdbc:h2:mem:db
will be used.