I have an API written in Swagger for which I want to generate both the Service implementation and also the client, and they have to be in separate maven modules.
I was thinking of separating them into 3 separate Maven modules (or submodules of the same parent pom).
parent
+- api
+- src/main/resources/api/service.yaml
+- client
+- service
Then in both client and service I would have the swagger-codegen-maven-plugin
. This way both will be in sync and I will only maintain the service from one place. Other clients can also depend on the api
artifact and generate their code from the service.yaml
Swagger API definition.
My difficulty is how do I make the service and client refer to the service.yaml
in another Maven dependency?
This is what I currently have in the service pom.xml
, but it refers to the local resources of the service module, not to the api
maven dependency.
<plugin>
<groupId>io.swagger</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<version>${io.swagger.codegen.version}</version>
<executions>
<execution>
<id>api</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<!-- can this refer to another maven dependency's resources? -->
<inputSpec>${basedir}/src/main/resources/api/service.yaml</inputSpec>
<language>spring</language>
<library>spring-boot</library>
<modelPackage>com.test.model</modelPackage>
<apiPackage>com.test.api</apiPackage>
<generateSupportingFiles>false</generateSupportingFiles>
<configOptions>
<java8>true</java8>
<dateLibrary>java8</dateLibrary>
<interfaceOnly>true</interfaceOnly>
</configOptions>
</configuration>
</execution>
</executions>
</plugin>
Not sure if this is something I have to do from Maven, to refer to resources from another maven dependency, or something I have to do in the swagger plugin configuration.
The Swagger Codegen is an open source code-generator to build server stubs and client SDKs directly from a Swagger defined RESTful API. The source code for the Swagger Codegen can be found in GitHub.
This plugin enables your Swagger-annotated project to generate Swagger specs and customizable, templated static documents during the maven build phase.
Swagger Codegen is an open source project which allows generation of API client libraries (SDK generation), server stubs, and documentation automatically from an OpenAPI Specification.
The solution I managed to find is to use the maven-remote-resources-plugin
. In the pom.xml
of the maven project that needs to expose resources you can put:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-remote-resources-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>bundle</goal>
</goals>
</execution>
</executions>
<configuration>
<includes>
<include>**/*.yaml</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
Then in the project that needs to import them, the project needs to be referred to as follows:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-remote-resources-plugin</artifactId>
<configuration>
<resourceBundles>
<resourceBundle>group:api:version</resourceBundle>
</resourceBundles>
</configuration>
<executions>
<execution>
<phase>
generate-sources
</phase>
<goals>
<goal>process</goal>
</goals>
</execution>
</executions>
</plugin>
where group:api:version
is the group ID, artifact ID and version of the maven dependency exposing the resources.
Finally, inside the swagger-codegen-maven-plugin
configuration, the yaml file can be referred to as:
<inputSpec>${project.build.directory}/maven-shared-archive-resources/api/service.yaml</inputSpec>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With