Skip to content

Commit 4af75cb

Browse files
authored
docs: Improve Kotlin scripting documentation (#26)
docs - Improve Kotlin scripting documentation
1 parent 77da431 commit 4af75cb

File tree

1 file changed

+41
-19
lines changed

1 file changed

+41
-19
lines changed

README.md

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,8 @@ asyncApi {
117117
### <a name="spring-web-usage"></a>Spring Web
118118
To serve your AsyncAPI specification via Spring Web:
119119
- add the `kotlin-asyncapi-spring-web` dependency
120-
- enable the autoconfiguration by annotating a configuration class with `@EnableAsyncApi`
121-
- [optional] provide `AsyncApiExtension` beans for the application context
122-
- [optional] add a Kotlin build script to the classpath -> see [Kotlin script usage](#kotlin-script-usage)
123-
124-
The library registers a default `info` extension using the `Implementation-Title` and `Implementation-Version` of the `MANIFEST.md` file. Please note that this information is only available if the application was started from a JAR file. The default info will **not** work if you start the application from your IDE.
120+
- enable the auto-configuration by annotating a configuration class with `@EnableAsyncApi`
121+
- document your API with `AsyncApiExtension` beans and/or Kotlin scripting (see [Kotlin script usage](#kotlin-script-usage))
125122

126123
You can register multiple extensions to extend and override AsyncAPI components. Extensions with a higher order override extensions with a lower order. Please note that you can only extend top-level components for now (`info`, `channels`, `servers`...). Subcomponents will always be overwritten.
127124

@@ -154,17 +151,19 @@ class AsyncApiConfiguration {
154151
```
155152

156153
### <a name="kotlin-script-usage"></a>Kotlin Script
157-
[Kotlin Scripting](https://github.com/Kotlin/KEEP/blob/b0c8a37db684eaf74bb1305f3c180b5d2537d787/proposals/scripting-support.md) allows us to execute a piece of code in a provided context. The IDE can still provide features like autocompletion and syntax highlighting. Furthermore, it provides the following benefits:
154+
[Kotlin scripting](https://github.com/Kotlin/KEEP/blob/b0c8a37db684eaf74bb1305f3c180b5d2537d787/proposals/scripting-support.md) allows us to execute a piece of code in a provided context. The IDE can still provide features like autocompletion and syntax highlighting. Furthermore, it provides the following benefits:
158155
- separate AsyncAPI documentation from application source code
159156
- focus on AsyncAPI content and don't worry about the build context or spring web integration
160157
- use AsyncAPI Kotlin DSL in Java projects
161158

162159
You have two options to use Kotlin scripting in your project:
163-
- let your Spring Boot application evaluate the script at runtime
164-
- let the Maven plugin evaluate the script during build time
160+
- [Plugin] let the Maven plugin evaluate the script during build time (recommended)
161+
- [Embedded] let your Spring Boot application evaluate the script at runtime
165162

166-
#### Embedded Scripting
167-
Your Spring Boot application can pick up your build script from the classpath and convert it to an `AsyncApiExtension`. You just have to place your script with the file extension `asyncapi.kts` in your `resources` folder. By default, the library expects the script to be named `build.asyncapi.kts` and placed in the root of the `resources` folder. This can be changed in the application properties.
163+
#### Maven Plugin
164+
The Maven plugin evaluates your `asyncapi.kts` script, generates a valid AsyncAPI JSON file and adds it to the project resources. The `kotlin-asyncapi-spring-web` module picks the generated resource up and converts it to an `AsyncApiExtension`.
165+
166+
By default, the plugin expects the script to be named `build.asyncapi.kts` and placed in the project root. The script path and resource target path can be changed in the plugin configuration.
168167

169168
**Example** (simplified version of [Gitter example](https://github.com/asyncapi/spec/blob/22c6f2c7a61846338bfbd43d81024cb12cf4ed5f/examples/gitter-streaming.yml))
170169
```kotlin
@@ -179,15 +178,6 @@ servers {
179178

180179
// ...
181180
```
182-
Embedded scripting is enabled by default for Spring Boot applications. If you don't want to include the Kotlin compiler in your build, you can also evaluate the script with the Maven plugin and use the packaged script resources in your Spring Boot application.
183-
184-
You just need to exclude the `kotlin-scripting-jvm-host` dependency from the `kotlin-asyncapi-spring-web` artifact. The Spring Web integration automatically picks up the script resource from the `resource-path` and converts it to an `AsyncApiExtension`.
185-
186-
#### Maven Plugin
187-
The Maven plugin will evaluate the script and put the generated AsyncAPI JSON on the package classpath. Your application can convert the resource to an AsyncApi model object.
188-
189-
By default, the plugin expects the script to be named `build.asyncapi.kts` and placed in the project root. The script path and resource target path can be changed in the plugin configuration.
190-
191181
```xml
192182
<plugin>
193183
<groupId>org.openfolder</groupId>
@@ -203,6 +193,38 @@ By default, the plugin expects the script to be named `build.asyncapi.kts` and p
203193
</plugin>
204194
```
205195

196+
#### Embedded Scripting
197+
If you can't use the Maven plugin for your project, you can also let your Spring Boot application evaluate the script at runtime.
198+
199+
You have to place your script in your `resources` folder. Similarly to the plugin, the `kotlin-asyncapi-spring-web` module will pick up the script and convert it to an `AsyncApiExtension`. By default, the library expects the script to be named `build.asyncapi.kts` and placed in the root of the `resources` folder. This can be changed in the application properties.
200+
201+
In order to enable embedded scripting, you need to make some additional configurations:
202+
- add `kotlin-scripting-jvm-host` to the classpath
203+
- unpack `kotlin-compiler-embeddable` from the Spring Boot executable JAR file
204+
205+
```xml
206+
<dependency>
207+
<groupId>org.jetbrains.kotlin</groupId>
208+
<artifactId>kotlin-scripting-jvm-host</artifactId>
209+
<version>${kotlin.version}</version>
210+
<scope>runtime</scope>
211+
</dependency>
212+
```
213+
```xml
214+
<plugin>
215+
<groupId>org.springframework.boot</groupId>
216+
<artifactId>spring-boot-maven-plugin</artifactId>
217+
<configuration>
218+
<requiresUnpack>
219+
<dependency>
220+
<groupId>org.jetbrains.kotlin</groupId>
221+
<artifactId>kotlin-compiler-embeddable</artifactId>
222+
</dependency>
223+
</requiresUnpack>
224+
</configuration>
225+
</plugin>
226+
```
227+
206228
## Configuration
207229
### <a name="spring-web-configuration"></a>Spring Web
208230
You can configure the Spring Web integration in the application properties:

0 commit comments

Comments
 (0)