Make swagger codegen maven plugin access yaml files from another maven dependency
Asked Answered
M

1

8

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.

Missus answered 18/11, 2018 at 13:12 Comment(0)
M
6

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>
Missus answered 20/11, 2018 at 14:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.