Skip to content

Commit a88a3d9

Browse files
Merge pull request #99 from fnproject/angie-javaex-gradle
Update Java FDK gradle-build example to latest code
2 parents b4e1551 + 83cbfcf commit a88a3d9

File tree

5 files changed

+55
-17
lines changed

5 files changed

+55
-17
lines changed

fdks/fdk-java/examples/gradle-build/Dockerfile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
FROM gradle:4.5.1-jdk8 as build-stage
1+
FROM gradle:5.6.2-jdk11 as build-stage
2+
23
WORKDIR /function
34
# needed for gradle?
45
USER root
@@ -14,7 +15,7 @@ COPY src /function/src
1415

1516
RUN ["gradle", "-s", "--no-daemon","--console","plain","build"]
1617
# Container build
17-
FROM fnproject/fn-java-fdk:1.0.56
18+
FROM fnproject/fn-java-fdk:jre11-1.0.98
1819
WORKDIR /function
1920
COPY --from=build-stage /function/build/libs/*.jar /function/build/deps/*.jar /function/app/
2021
CMD ["com.example.fn.HelloFunction::handleRequest"]
Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,41 @@
1-
# Fn Gradle + Java fdk example
1+
# Example Java Functions: Fn Gradle + Java fdk
22

3-
Fn uses Maven by default for builds. This is an example that uses Fn's `docker` runtime format to build a Java function using the [Fn Java FDK](https://github.com/fnproject/fdk-java).
3+
This example shows you how to build a Java function by using a Dockerfile.
44

5-
The example consists of a `Dockerfile` that builds the function using gradle and copies the function's dependencies to `build/deps` and a func.yaml that uses that `Dockerfile` to build the function.
65

7-
Note that fdk.versions are hard-coded in this example, you may need to update them manually to more recent version.
6+
## Key points:
87

9-
Key points:
8+
* [Dockerfile](Dockerfile) - contains the containerized Docker build (based on dockerhub library/gradle images) and image build - this includes the gradle invocation
9+
* The `cacheDeps` task in `build.gradle` pulls down dependencies into the container gradle cache to speed up docker builds.
10+
* The `copyDeps` task in `build.gradle` copies the functions compile deps.
11+
* This uses JDK 11 by default
1012

11-
* [Dockerfile](Dockerfile) - contains the containerised docker build (based on dockerhub library/gradle images) and image build - this includes the gradle invocation
12-
* The `cacheDeps` task in `build.gradle` is invoked within the Dockerfile - The task pulls down dependencies into the container gradle cache to speed up docker builds.
13-
* The `copyDeps` task in `build.gradle` copies the functions compile deps
14-
* This uses JDK 8 by default - you can change this to Java 11 by changing : `FROM gradle:4.5.1-jdk8 as build-stage` to `FROM gradle:4.5.1-jre11 as build-stage` and `FROM fnproject/fn-java-fdk:1.0.85` to `FROM fnproject/fn-java-fdk:jre11-1.0.85`
13+
## Step by step
14+
15+
Ensure you have the Fn server running to host your function:
16+
17+
(1) Start the server
18+
19+
```sh
20+
$ fn start
21+
```
22+
23+
(2) Create an app for the function
24+
25+
```sh
26+
$ fn create app gradle-build-app
27+
```
28+
29+
(3) Deploy the function to your app from the `gradle-build` directory.
30+
31+
```sh
32+
fn deploy --app gradle-build-app --local
33+
```
34+
35+
(4) Invoke the function
36+
37+
```sh
38+
fn invoke gradle-build-app gradle_build
39+
```
40+
41+

fdks/fdk-java/examples/gradle-build/build.gradle

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,31 @@
11
apply plugin: 'java'
22

33
ext {
4-
fdkVersion = '1.0.56'
4+
fdkVersion = '1.0.98'
55
}
66

77
repositories {
88
mavenCentral()
99
maven {
1010
url 'https://dl.bintray.com/fnproject/fnproject'
11-
}
11+
}
1212
}
1313

1414
dependencies {
1515
runtime "com.fnproject.fn:api:$fdkVersion"
1616
// runtime "com.fnproject.fn:runtime:$fdkVersion" // this is optional and included with its deps in the base image to reduce layer size
1717

1818
testCompile "junit:junit:4.12"
19-
testCompile "com.fnproject.fn:testing:$fdkVersion"
19+
testCompile "com.fnproject.fn:testing-core:$fdkVersion"
20+
testCompile "com.fnproject.fn:testing-junit4:$fdkVersion"
2021
}
2122

2223
task cacheDeps(type: Exec) {
23-
configurations.testRuntime.files
24+
configurations.testCompile.files
2425
commandLine 'echo', 'Downloaded all dependencies'
2526
}
2627

28+
2729
task copyDeps(type: Copy) {
2830
from configurations.compile
2931
into "${project.buildDir}/deps"
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1+
schema_version: 20180708
12
name: gradle_build
2-
version: 0.0.3
3+
version: 0.0.41
34
runtime: docker
4-
format: http

fdks/fdk-java/examples/gradle-build/src/test/java/com/example/fn/HelloFunctionTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
import static org.junit.Assert.*;
44

5+
import org.junit.Rule;
6+
import org.junit.Test;
7+
8+
import com.fnproject.fn.testing.FnResult;
9+
import com.fnproject.fn.testing.FnTestingRule;
10+
11+
12+
513
public class HelloFunctionTest {
614

715
@Rule

0 commit comments

Comments
 (0)