Skip to content

Instantly share code, notes, and snippets.

@joaomlneto
Last active July 29, 2022 14:15
Show Gist options
  • Save joaomlneto/edc6d76c921634f86d4e608eb43ec78c to your computer and use it in GitHub Desktop.
Save joaomlneto/edc6d76c921634f86d4e608eb43ec78c to your computer and use it in GitHub Desktop.
Generate AsyncAPI during Maven build

This is an example of how to invoke AsyncAPI generator during a maven build, in three steps:

  1. Use download-maven-plugin to download the required files. A bit hacky, but required if you need authentication to access it... The downside is that you can't split your definition in multiple files, or you'll have to download them all. Feedback is welcome.
  2. Use exec-maven-plugin to invoke the AsyncAPI generator with the specified arguments
  3. Use build-helper-maven-plugin to signal maven to also include the newly-generated sources as source folders.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.10</version>
<relativePath/>
</parent>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- step one: download the files (if required) -->
<!-- the downside is that using this method we -->
<!-- dont support splitting schema in multiple -->
<!-- files... -->
<plugin>
<groupId>com.googlecode.maven-download-plugin</groupId>
<artifactId>download-maven-plugin</artifactId>
<version>1.6.8</version>
<executions>
<execution>
<id>download-asyncapi-one-spec</id>
<phase>initialize</phase>
<goals>
<goal>wget</goal>
</goals>
<configuration>
<url>https://example.com/my-api-one.yaml</url>
</configuration>
</execution>
<execution>
<id>download-asyncapi-two-spec</id>
<phase>initialize</phase>
<goals>
<goal>wget</goal>
</goals>
<configuration>
<url>https://example.com/my-api-two.yaml</url>
</configuration>
</execution>
</executions>
<configuration>
<outputDirectory>${project.build.directory}/generated-sources/asyncapi</outputDirectory>
<skipCache>true</skipCache>
<headers>
<authorization>token gh_xxxxxxxxxxxxxxxxxxxx</authorization>
</headers>
</configuration>
</plugin>
<!-- step two: use the exec-maven-plugin to invoke the asyncapi generator -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>asyncapi-generate-worker-client</id>
<phase>generate-sources</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>ag</executable>
<arguments>
<argument>${project.build.directory}/generated-sources/asyncapi/one.yaml</argument>
<argument>@joaomlneto/asyncapi-spring-websockets-template</argument>
<argument>-o</argument>
<argument>${project.build.directory}/generated-sources/asyncapi-one-client</argument>
<argument>-p</argument>
<argument>javaPackage=myorg.api.one</argument>
</arguments>
</configuration>
</execution>
<execution>
<id>asyncapi-generate-scheduler-client</id>
<phase>generate-sources</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>ag</executable>
<arguments>
<argument>${project.build.directory}/generated-sources/asyncapi/two.yaml</argument>
<argument>@joaomlneto/asyncapi-spring-websockets-template</argument>
<argument>-o</argument>
<argument>${project.build.directory}/generated-sources/asyncapi-two-client</argument>
<argument>-p</argument>
<argument>javaPackage=myorg.api.two</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
<!-- step three: signal maven to consider the newly-generated directories as source directories -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>add-asyncapi-sources</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${project.build.directory}/generated-sources/asyncapi-scheduler-client/src/main/java</source>
<source>${project.build.directory}/generated-sources/asyncapi-worker-client/src/main/java</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment