From b3bcc8af1428a137a1a553347f2f9bc24b6355f3 Mon Sep 17 00:00:00 2001 From: Nitishkumar Singh Date: Sat, 19 Dec 2020 19:07:06 +0530 Subject: [PATCH] included support of java 11 with maven Signed-off-by: Nitishkumar Singh --- .gitignore | 1 + README.md | 1 + template/java11-maven/Dockerfile | 57 ++++++++++ template/java11-maven/README.md | 24 ++++ template/java11-maven/function/pom.xml | 104 ++++++++++++++++++ .../function/src/assembly/dep.xml | 17 +++ .../java/com/openfaas/function/Handler.java | 16 +++ .../com/openfaas/function/HandlerTest.java | 14 +++ template/java11-maven/pom.xml | 12 ++ template/java11-maven/template.yml | 4 + 10 files changed, 250 insertions(+) create mode 100644 template/java11-maven/Dockerfile create mode 100644 template/java11-maven/README.md create mode 100644 template/java11-maven/function/pom.xml create mode 100644 template/java11-maven/function/src/assembly/dep.xml create mode 100644 template/java11-maven/function/src/main/java/com/openfaas/function/Handler.java create mode 100644 template/java11-maven/function/src/test/java/com/openfaas/function/HandlerTest.java create mode 100644 template/java11-maven/pom.xml create mode 100644 template/java11-maven/template.yml diff --git a/.gitignore b/.gitignore index 06ce87ad..f2c4c72b 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ build .idea **/bin/** .settings +target \ No newline at end of file diff --git a/README.md b/README.md index 638b5d49..01794978 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,7 @@ This repository contains the Classic OpenFaaS templates, but many more are avail | python | Python | 2.7 | Alpine Linux | classic | [Python 2.7 template](https://github.com/openfaas/templates/tree/master/template/python) | java11-vert-x | Java and [Vert.x](https://vertx.io/) | 11 | Debian GNU/Linux | of-watchdog | [Java LTS template](https://github.com/openfaas/templates/tree/master/template/java11) | java11 | Java | 11 | Debian GNU/Linux | of-watchdog | [Java LTS template](https://github.com/openfaas/templates/tree/master/template/java11) +| java11-maven | Java | 11 | Debian GNU/Linux | of-watchdog | [Java Maven LTS template](https://github.com/openfaas/templates/tree/master/template/java11-maven) | ruby | Ruby | 2.7 | Alpine Linux 3.11 | classic| [Ruby template](https://github.com/openfaas/templates/tree/master/template/ruby) | php7 | PHP | 7.2 | Alpine Linux | classic | [PHP 7 template](https://github.com/openfaas/templates/tree/master/template/php7) | csharp | C# | N/A | Debian GNU/Linux 9 | classic | [C# template](https://github.com/openfaas/templates/tree/master/template/csharp) diff --git a/template/java11-maven/Dockerfile b/template/java11-maven/Dockerfile new file mode 100644 index 00000000..066f9a24 --- /dev/null +++ b/template/java11-maven/Dockerfile @@ -0,0 +1,57 @@ +FROM openjdk:11-jdk-slim as builder + +ENV MAVEN_VER=3.6.3 +RUN apt-get update -qqy \ + && apt-get install -qqy \ + --no-install-recommends \ + curl \ + ca-certificates \ + unzip + +RUN mkdir -p /opt/ && cd /opt/ \ + && echo "Downloading maven.." \ + && curl -sSfL "https://mirrors.estointernet.in/apache/maven/maven-3/${MAVEN_VER}/binaries/apache-maven-${MAVEN_VER}-bin.zip" -o apache-maven-$MAVEN_VER-bin.zip \ + && unzip apache-maven-$MAVEN_VER-bin.zip -d /opt/ \ + && rm apache-maven-$MAVEN_VER-bin.zip + +# Export some environment variables +ENV MAVEN_HOME=/opt/apache-maven-$MAVEN_VER/ +ENV PATH=$PATH:$MAVEN_HOME/bin + +WORKDIR /home/app + +COPY . /home/app/ + +RUN mvn clean package +RUN find . + +FROM openfaas/of-watchdog:0.7.6 as watchdog + +FROM openjdk:11-jre-slim as ship +RUN apt-get update -qqy \ + && apt-get install -qqy \ + --no-install-recommends \ + unzip +RUN addgroup --system app \ + && adduser --system --ingroup app app + +COPY --from=watchdog /fwatchdog /usr/bin/fwatchdog +RUN chmod +x /usr/bin/fwatchdog + +WORKDIR /home/app +COPY --from=builder /home/app/function/target/function-0.1.0-bin.zip ./function-0.1.0.zip +user app +RUN unzip ./function-0.1.0.zip + +WORKDIR /home/app/ + +ENV upstream_url="http://127.0.0.1:8082" +ENV mode="http" +ENV CLASSPATH="/home/app/function-0.1.0/function-0.1.0.jar:/home/app/function-0.1.0/lib/*" + +ENV fprocess="java -XX:+UseContainerSupport com.openfaas.entrypoint.App" +EXPOSE 8080 + +HEALTHCHECK --interval=5s CMD [ -e /tmp/.lock ] || exit 1 + +CMD ["fwatchdog"] \ No newline at end of file diff --git a/template/java11-maven/README.md b/template/java11-maven/README.md new file mode 100644 index 00000000..f95b450a --- /dev/null +++ b/template/java11-maven/README.md @@ -0,0 +1,24 @@ +## Template: java11 + +The Java11 template uses maven as a build system. + +Maven version: 5.5.1 + +### Structure + +There are three projects which make up a single maven build: + +- model - (Library) classes for parsing request/response +- function - (Library) your function code as a developer, you will only ever see this folder +- entrypoint - (App) HTTP server for re-using the JVM between requests + +### Handler + +The handler is written in the `./src/main/Handler.java` folder + +Tests are supported with junit via files in `./src/test` + +### External dependencies + +External dependencies can be specified in ./pom.xml in the normal way using Maven Repository, a local JAR or some other remote repository. + diff --git a/template/java11-maven/function/pom.xml b/template/java11-maven/function/pom.xml new file mode 100644 index 00000000..2ca9f107 --- /dev/null +++ b/template/java11-maven/function/pom.xml @@ -0,0 +1,104 @@ + + + 4.0.0 + + org.openfaas + function + jar + 0.1.0 + + 11 + 11 + 0.1.1 + 0.1.0 + 4.13.1 + 3.2.4 + 3.8.1 + 2.5.3 + + + + + + com.openfaas + model + ${openfaas.model.version} + + + + com.openfaas + entrypoint + ${openfaas.entrypoint.version} + + + junit + junit + ${junit.version} + test + + + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven.compiler.plugin.version} + + + org.apache.maven.plugins + maven-dependency-plugin + + + copy-dependencies + prepare-package + + copy-dependencies + + + ${project.build.directory}/lib + false + false + true + + + + + + + maven-assembly-plugin + ${maven.assembly.plugin.version} + + src/assembly/dep.xml + + + + create-archive + package + + single + + + + + + + \ No newline at end of file diff --git a/template/java11-maven/function/src/assembly/dep.xml b/template/java11-maven/function/src/assembly/dep.xml new file mode 100644 index 00000000..0bc5c944 --- /dev/null +++ b/template/java11-maven/function/src/assembly/dep.xml @@ -0,0 +1,17 @@ + +bin + + zip + + + + ${project.build.directory} + / + + *.jar + lib/ + + + + \ No newline at end of file diff --git a/template/java11-maven/function/src/main/java/com/openfaas/function/Handler.java b/template/java11-maven/function/src/main/java/com/openfaas/function/Handler.java new file mode 100644 index 00000000..0aec2aaa --- /dev/null +++ b/template/java11-maven/function/src/main/java/com/openfaas/function/Handler.java @@ -0,0 +1,16 @@ +package com.openfaas.function; + +import com.openfaas.model.IHandler; +import com.openfaas.model.IResponse; +import com.openfaas.model.IRequest; +import com.openfaas.model.Response; + +public class Handler extends com.openfaas.model.AbstractHandler { + + public IResponse Handle(IRequest req) { + Response res = new Response(); + res.setBody("Hello, world!"); + + return res; + } +} diff --git a/template/java11-maven/function/src/test/java/com/openfaas/function/HandlerTest.java b/template/java11-maven/function/src/test/java/com/openfaas/function/HandlerTest.java new file mode 100644 index 00000000..26493e28 --- /dev/null +++ b/template/java11-maven/function/src/test/java/com/openfaas/function/HandlerTest.java @@ -0,0 +1,14 @@ +package com.openfaas.function; + +import org.junit.Test; +import static org.junit.Assert.*; + +import com.openfaas.function.Handler; +import com.openfaas.model.IHandler; + +public class HandlerTest { + @Test public void handlerIsNotNull() { + IHandler handler = new Handler(); + assertTrue("Expected handler not to be null", handler != null); + } +} diff --git a/template/java11-maven/pom.xml b/template/java11-maven/pom.xml new file mode 100644 index 00000000..d099d9e1 --- /dev/null +++ b/template/java11-maven/pom.xml @@ -0,0 +1,12 @@ + + + 4.0.0 + + org.openfaas + java11 + pom + 0.1.0 + + function + + \ No newline at end of file diff --git a/template/java11-maven/template.yml b/template/java11-maven/template.yml new file mode 100644 index 00000000..95934d29 --- /dev/null +++ b/template/java11-maven/template.yml @@ -0,0 +1,4 @@ +language: java11 +welcome_message: | + You have created a function using the java11 template which uses an LTS + version of the OpenJDK.