DS FN Gradle Plugin Jun 2nd, 2019

Creating a Groovy function

Before starting ensure that the Prerequisites are installed.

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

1
mkdir groovy-fn; cd groovy-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

build.gradle

1
2
3
4
5
6
7
8
9
plugins {
  id 'groovy'
  id 'com.devsoap.fn' version '0.2'
}

dependencies {
  compile fn.groovy()
}

To build a Groovy project you will need to include the Groovy Gradle plugin in addition to the DS Vaadin Flow Gradle Plugin.

You also will need to add Groovy as a compile time dependency. The DS Vaadin Flow Gradle Plugin provides a nice helper method fn.groovy() for that purpose.

Bootstrap project

Once we have the build.gradle file set up we can create the project using the fnCreateFunction task.

1
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 Groovy 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.groovy


3 directories, 2 files

The only other thing that was created was one single Groovy file, MyFunction.groovy.

The function class

MyFunction.java

1
2
3
4
5
6
class MyFunction {

    String handleRequest(String input) {
        (input ?: 'world').tap { "Hello, $it!" }
    }
}

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