Skip to content

Commit

Permalink
chore: add Integrate with maven example (#1579)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaslagoni authored Oct 25, 2023
1 parent 2a29267 commit 69dd4e4
Show file tree
Hide file tree
Showing 16 changed files with 606 additions and 6 deletions.
17 changes: 17 additions & 0 deletions docs/integration.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,20 @@ To be on the safeside, only enable the user to chose between the internal option

## Integrate Modelina in an AsyncAPI generator template
TODO

## Integrate Modelina into Maven

There are at least two ways you can integrate Modelina into your build process for Maven projects, either with the AsyncAPI CLI or with a custom build script. Which one to choose all depends on your scenario, look below:

**Custom build script**
- DO work with other inputs then AsyncAPI
- DO work when needing extensive build options and configurations

Checkout the Maven example here: [Integrate Modelina into Maven](../examples/integrate-modelina-into-maven/maven-project/)

**AsyncAPI CLI**

- DO NOT work if you have other inputs then AsyncAPI
- DO NOT need extensive build options and configuration

We don't have a full example for this, but you can use similar concept as the custom build script. However, instead you just install and call the AsyncAPI CLI directly in the plugin execution when utilizing the [frontend-maven-plugin](https://github.com/eirslett/frontend-maven-plugin) and [more specifically the NPX execution](https://github.com/eirslett/frontend-maven-plugin#npx).
3 changes: 3 additions & 0 deletions docs/languages/Java.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,6 @@ Currently not supported, [let everyone know you need it](https://github.com/asyn

### To and from binary
Currently not supported, [let everyone know you need it](https://github.com/asyncapi/modelina/issues/new?assignees=&labels=enhancement&template=enhancement.md)!

## Integrate Modelina into Maven
We have created an example Maven project to show you how to generate AsyncAPI payload models from your AsyncAPI file and integrate it into the build process. [You can find the integration example here](../integration.md#integrate-modelina-into-maven).
4 changes: 3 additions & 1 deletion examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,9 @@ These are all the basic generator examples that shows a bare minimal example of

## Integrations
These are examples of how you can integrate Modelina into a specific scenario:
- [integrate with React](./integrate-with-react) - A basic example that shows how you can integrate Modelina with React.
- [integrate-with-react](./integrate-with-react) - A basic example that shows how you can integrate Modelina with React.
- [integrate-with-next](./integrate-with-next) - A basic example that shows how you can integrate Modelina with Next.
- [integrate-modelina-into-maven](./integrate-modelina-into-maven) - A basic example that shows how you can integrate Modelina into the Java Maven build process.

## Python
These are all specific examples only relevant to the Python generator:
Expand All @@ -94,6 +95,7 @@ These are all specific examples only relevant to the Java generator:
- [java-generate-equals](./java-generate-equals) - A basic example that shows how to generate models that overwrite the `equal` method
- [java-generate-javax-constraint-annotation](./java-generate-javax-constraint-annotation) - A basic example that shows how Java data models having `javax.validation.constraints` annotations can be generated.
- [java-generate-javadoc](./java-generate-javadoc) - A basic example of how to generate Java models including JavaDocs.
- [integrate-modelina-into-maven](./integrate-modelina-into-maven/) - A basic example that shows how you can integrate Modelina into the Java Maven build process.

## C#
These are all specific examples only relevant to the C# generator:
Expand Down
3 changes: 3 additions & 0 deletions examples/integrate-modelina-into-maven/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Integrate Modelina into Maven

This example show how you can integrate Modelina into Maven with a custom build script. [Please have a look at the Maven project README file for further information](./maven-project/README.md).
25 changes: 25 additions & 0 deletions examples/integrate-modelina-into-maven/maven-project/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

target/
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
pom.xml.next
release.properties
dependency-reduced-pom.xml
buildNumber.properties
.mvn/timing.properties
# https://github.com/takari/maven-wrapper#usage-without-binary-jar
.mvn/wrapper/maven-wrapper.jar

# Eclipse m2e generated files
# Eclipse Core
.project
# JDT-specific (Eclipse Java Development Tools)
.classpath

bin
.settings

# Ignore Modelina generation node modules
scripts/modelina/java_runtime_node
scripts/modelina/node_modules
10 changes: 10 additions & 0 deletions examples/integrate-modelina-into-maven/maven-project/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Integrate Modelina into Maven

This Java Maven project shows an example how to integrate Modelina and AsyncAPI into your build process.

Here is how it works:
- The script `./scripts/modelina/generate.ts` is what generates all the models, and what Maven uses to generate the models. This can also be executed manually through `npm run generate`.
- The input, in this case, is an AsyncAPI document located in the root of the project `./asyncapi.json`. The input can be anything, just alter the generator script.
- The Maven project file `./pom.xml` then utilizes the [frontend-maven-plugin](https://github.com/eirslett/frontend-maven-plugin) to execute the generate script on build so you will always have the up to date models from your AsyncAPI document.

> NOTICE: The only thing you manually have to change for this to work in your project is the dependency entry for `"@asyncapi/modelina": "file:../../../../../",` in the `./scripts/modelina/package.json` file to use the latest Modelina version (we only use a local one for testing purposes).
26 changes: 26 additions & 0 deletions examples/integrate-modelina-into-maven/maven-project/asyncapi.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"asyncapi": "2.2.0",
"info": {
"title": "example",
"version": "0.1.0"
},
"channels": {
"/test": {
"subscribe": {
"message": {
"payload": {
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"additionalProperties": false,
"properties": {
"email": {
"type": "string",
"format": "email"
}
}
}
}
}
}
}
}
113 changes: 113 additions & 0 deletions examples/integrate-modelina-into-maven/maven-project/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?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>

<groupId>com.mycompany.app</groupId>
<artifactId>maven-project</artifactId>
<version>1.0-SNAPSHOT</version>

<name>maven-project</name>
<url>http://www.example.com</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.15.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.15.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hamcrest/hamcrest-all -->
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<plugin>
<groupId>com.github.eirslett</groupId>
<artifactId>frontend-maven-plugin</artifactId>
<version>1.12.1</version>
<configuration>
<workingDirectory>./scripts/modelina</workingDirectory>
<installDirectory>java_runtime_node</installDirectory>
</configuration>
<executions>
<!-- It will install nodejs and npm -->
<execution>
<id>install node and npm</id>
<phase>generate-sources</phase>
<goals>
<goal>install-node-and-npm</goal>
</goals>
<configuration>
<nodeVersion>v18.15.0</nodeVersion>
</configuration>
</execution>
<execution>
<id>Generate sources</id>
<phase>generate-sources</phase>
<goals>
<goal>npm</goal>
</goals>
<configuration>
<arguments>run generate</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { JAVA_JACKSON_PRESET, JavaFileGenerator } from '@asyncapi/modelina';
import path from 'path';

// Where should the models be placed relative to root maven project?
const PACKAGE_NAME = 'java/com/mycompany/app';
const MODEL_DIR = `src/main/${PACKAGE_NAME}`;

const FINAL_OUTPUT_PATH = path.resolve(__dirname, '../../', MODEL_DIR);
// Setup the generator and all it's configurations
const generator = new JavaFileGenerator({
presets: [JAVA_JACKSON_PRESET]
});

// Load the input from file, memory, or remotely.
// Here we just use a local AsyncAPI file
import INPUT from '../../asyncapi.json';
const input = INPUT;

// Generate all the files
generator.generateToFiles(input, FINAL_OUTPUT_PATH, {
packageName: 'com.mycompany.app'
});
Loading

0 comments on commit 69dd4e4

Please sign in to comment.