DS FN Gradle Plugin Jun 2nd, 2019

Creating a Java function

Before starting ensure that the Prerequisites are installed.

To create a new project create a new folder on your filesystem

mkdir java-fn; cd java-fn

Create an initial build file

In the folder you want to create the function create a new build.gradle file with the following content


plugins {
  id 'com.devsoap.fn' version '0.2'

The build file only will need to reference the Gradle plugin, everything else is optional.

Bootstrap project

Finally, to create the project run the fnCreateFunction task

gradle fnCreateFunction

fnCreateFunction Task Description

Creates a new FN function either in this project or as a sub-project to the current project

Usage: gradle fnCreateFunction [ARGS]

Optional arguments:
  --name          Function Class name
  --package       Function Class package
  --method        The name of the function method entrypoint Java method
  --module        Create function as an inner sub-module

Once the task has run, the following folder structure is created:

├── build.gradle
└── src
    └── main
        └── java
            └── MyFunction.java

3 directories, 2 files

The only other thing that was created was one single Java file, MyFunction.java.

The function class


public class MyFunction {

    public String handleRequest(String input) {
        String name = ofNullable(input).filter(s -> !s.isEmpty()).orElse("world");
        return "Hello, " + name + "!";

On line 1 we define a new function class.
On line 3 we defined the entry point method. The name of the method can be anything, but the return value is what will be returned in the HTTP request response body and the method argument is what is provided in the HTTP request body.
On line 4-5 We define a trivial example return value.

Once you have created the function you will be able to run it by following the Running functions locally tutorial.