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

  1. buildscript {
  2. repositories {
  3. maven { url "https://plugins.gradle.org/m2/" } (1)
  4. }
  5. dependencies {
  6. classpath "com.github.jengelman.gradle.plugins:shadow:2.0.4"
  7. ...
  8. }
  9. }
  10. apply plugin:"com.github.johnrengelman.shadow"
  11. shadowJar {
  12. mergeServiceFiles()
  13. }
1The 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

  1. $ ./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

  1. $ 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.