9.2.2 Functions as CLI Applications
To execute your function as a CLI application with java -jar
, you will need to package your application as an executable JAR file. For example, in build.gradle
:
build.gradle
buildscript {
repositories {
maven { url "https://plugins.gradle.org/m2/" } (1)
}
dependencies {
classpath "com.github.jengelman.gradle.plugins:shadow:2.0.4"
...
}
}
apply plugin:"com.github.johnrengelman.shadow"
shadowJar {
mergeServiceFiles()
}
1 | The Gradle Shadow plugin is hosted in the http://plugins.gradle.org repository |
You can now package your application using the shadowJar
task.
Packaging a Function as a JAR
$ ./gradlew shadowJar
At this point, you can execute your function using the java -jar
command. To supply input data to the function, simply pipe input via System.in
. For example:
Executing a function via the CLI
$ echo '{value: 3}' | java -jar build/libs/math-function-0.1-all.jar
The above example will provide the JSON {value: 3}
to function which will write the return value to standard out.
This allows functions written with Micronaut to be deployed to Function-as-a-Service (FaaS) platforms that process functions via standard in/out such as OpenFaaS.