{"id":25777310,"url":"https://github.com/GoogleCloudPlatform/functions-framework-java","last_synced_at":"2025-02-27T06:07:27.024Z","repository":{"id":39926902,"uuid":"180757477","full_name":"GoogleCloudPlatform/functions-framework-java","owner":"GoogleCloudPlatform","description":"FaaS (Function as a service) framework for writing portable Java functions","archived":false,"fork":false,"pushed_at":"2025-02-20T00:19:33.000Z","size":663,"stargazers_count":143,"open_issues_count":21,"forks_count":66,"subscribers_count":33,"default_branch":"main","last_synced_at":"2025-02-23T05:04:30.196Z","etag":null,"topics":["functions-as-a-service","google-cloud","java"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/GoogleCloudPlatform.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-04-11T09:19:18.000Z","updated_at":"2025-02-20T00:19:25.000Z","dependencies_parsed_at":"2023-10-11T04:13:26.350Z","dependency_job_id":"d5720161-09c8-4361-afb1-24e3d0567ac8","html_url":"https://github.com/GoogleCloudPlatform/functions-framework-java","commit_stats":{"total_commits":298,"total_committers":34,"mean_commits":8.764705882352942,"dds":0.6442953020134228,"last_synced_commit":"bb3d014eef2abef765740029bc31ab2c93c3a86f"},"previous_names":[],"tags_count":36,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GoogleCloudPlatform%2Ffunctions-framework-java","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GoogleCloudPlatform%2Ffunctions-framework-java/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GoogleCloudPlatform%2Ffunctions-framework-java/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GoogleCloudPlatform%2Ffunctions-framework-java/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/GoogleCloudPlatform","download_url":"https://codeload.github.com/GoogleCloudPlatform/functions-framework-java/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240987436,"owners_count":19889335,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["functions-as-a-service","google-cloud","java"],"created_at":"2025-02-27T06:01:40.740Z","updated_at":"2025-02-27T06:07:27.018Z","avatar_url":"https://github.com/GoogleCloudPlatform.png","language":"Java","readme":"# Functions Framework for Java\n\n[![Maven Central (functions-framework-api)](https://img.shields.io/maven-central/v/com.google.cloud.functions/functions-framework-api.svg?label=functions-framework-api)](https://search.maven.org/artifact/com.google.cloud.functions/functions-framework-api)\n[![Maven Central (java-function-invoker)](https://img.shields.io/maven-central/v/com.google.cloud.functions.invoker/java-function-invoker.svg?label=java-function-invoker)](https://search.maven.org/artifact/com.google.cloud.functions.invoker/java-function-invoker)\n[![Maven Central (function-maven-plugin)](https://img.shields.io/maven-central/v/com.google.cloud.functions/function-maven-plugin.svg?label=function-maven-plugin)](https://search.maven.org/artifact/com.google.cloud.functions/function-maven-plugin)\n\n[![Java Unit CI](https://github.com/GoogleCloudPlatform/functions-framework-java/actions/workflows/unit.yaml/badge.svg)](https://github.com/GoogleCloudPlatform/functions-framework-java/actions/workflows/unit.yaml)\n[![Java Lint CI](https://github.com/GoogleCloudPlatform/functions-framework-java/actions/workflows/lint.yaml/badge.svg)](https://github.com/GoogleCloudPlatform/functions-framework-java/actions/workflows/lint.yaml)\n[![Java Conformance CI](https://github.com/GoogleCloudPlatform/functions-framework-java/actions/workflows/conformance.yaml/badge.svg)](https://github.com/GoogleCloudPlatform/functions-framework-java/actions/workflows/conformance.yaml)\n![Security Scorecard](https://api.securityscorecards.dev/projects/github.com/GoogleCloudPlatform/functions-framework-java/badge)\n\nAn open source FaaS (Function as a service) framework for writing portable\nJava functions.\n\nThe Functions Framework lets you write lightweight functions that run in many\ndifferent environments, including:\n\n*   [Google Cloud Run functions](https://cloud.google.com/functions/)\n*   Your local development machine\n*   [Knative](https://github.com/knative/)-based environments\n\n## Installation\n\nThe Functions Framework for Java uses\n[Java](https://java.com/en/download/help/download_options.xml) and\n[Maven](http://maven.apache.org/install.html) (the `mvn` command),\nfor building and deploying functions from source.\n\nHowever, it is also possible to build your functions using\n[Gradle](https://gradle.org/), as JAR archives, that you will deploy with the \n`gcloud` command-line.\n\n## Quickstart: Hello, World on your local machine\n\nA function is typically structured as a Maven project. We recommend using an IDE\nthat supports Maven to create the Maven project. Add this dependency in the\n`pom.xml` file of your project:\n\n```xml\n    \u003cdependency\u003e\n      \u003cgroupId\u003ecom.google.cloud.functions\u003c/groupId\u003e\n      \u003cartifactId\u003efunctions-framework-api\u003c/artifactId\u003e\n      \u003cversion\u003e1.1.2\u003c/version\u003e\n      \u003cscope\u003eprovided\u003c/scope\u003e\n    \u003c/dependency\u003e\n```\n\nIf you are using Gradle to build your functions, you can define the Functions\nFramework dependency in your `build.gradle` project file as follows:\n\n```groovy\n    dependencies {\n        implementation 'com.google.cloud.functions:functions-framework-api:1.1.2'\n    }\n\n```\n\n### Writing an HTTP function\n\nCreate a file `src/main/java/com/example/HelloWorld.java` with the following\ncontents:\n\n```java\npackage com.example;\n\nimport com.google.cloud.functions.HttpFunction;\nimport com.google.cloud.functions.HttpRequest;\nimport com.google.cloud.functions.HttpResponse;\n\npublic class HelloWorld implements HttpFunction {\n  @Override\n  public void service(HttpRequest request, HttpResponse response)\n      throws Exception {\n    response.getWriter().write(\"Hello, World\\n\");\n  }\n}\n```\n\n\n## Quickstart: Create a Background Function\n\nThere are two ways to write a Background function, which differ in how the\npayload of the incoming event is represented. In a \"raw\" background function\nthis payload is presented as a JSON-encoded Java string. In a \"typed\" background\nfunction the Functions Framework deserializes the JSON payload into a Plain Old\nJava Object (POJO).\n\n### Writing a Raw Background Function\n\nCreate a file `src/main/java/com/example/Background.java` with the following\ncontents:\n\n```java\npackage com.example;\n\nimport com.google.cloud.functions.Context;\nimport com.google.cloud.functions.RawBackgroundFunction;\nimport com.google.gson.Gson;\nimport com.google.gson.JsonObject;\nimport java.util.logging.Logger;\n\npublic class Background implements RawBackgroundFunction {\n  private static final Logger logger =\n      Logger.getLogger(Background.class.getName());\n\n  @Override\n  public void accept(String json, Context context) {\n    Gson gson = new Gson();\n    JsonObject jsonObject = gson.fromJson(json, JsonObject.class);\n    logger.info(\"Received JSON object: \" + jsonObject);\n  }\n}\n```\n\n### Writing a Typed Background Function\n\nCreate a file `src/main/java/com/example/PubSubBackground` with the following\ncontents:\n\n```java\npackage com.example;\n\nimport com.google.cloud.functions.BackgroundFunction;\nimport com.google.cloud.functions.Context;\nimport java.util.Map;\nimport java.util.logging.Logger;\n\n// This is the Pub/Sub message format from the Pub/Sub emulator. \nclass PubSubMessage {\n  String data;\n  Map\u003cString, String\u003e attributes;\n  String messageId;\n  String publishTime;\n}\n\npublic class PubSubBackground implements BackgroundFunction\u003cPubSubMessage\u003e {\n  private static final Logger logger =\n      Logger.getLogger(PubSubBackground.class.getName());\n\n  @Override\n  public void accept(PubSubMessage pubSubMessage, Context context) {\n    logger.info(\"Received message with id \" + context.eventId());\n  }\n}\n```\n\n\n## Running a function with the Maven plugin\n\nThe Maven plugin called `function-maven-plugin` allows you to run functions\non your development machine.\n\n### Configuration in `pom.xml`\n\nYou can configure the plugin in `pom.xml`:\n\n```xml\n\u003cplugin\u003e\n  \u003cgroupId\u003ecom.google.cloud.functions\u003c/groupId\u003e\n  \u003cartifactId\u003efunction-maven-plugin\u003c/artifactId\u003e\n  \u003cversion\u003e0.10.1\u003c/version\u003e\n  \u003cconfiguration\u003e\n    \u003cfunctionTarget\u003ecom.example.HelloWorld\u003c/functionTarget\u003e\n  \u003c/configuration\u003e\n\u003c/plugin\u003e\n```\n\nThen run it from the command line:\n\n```sh\nmvn function:run\n```\n\n### Configuration on the command line\n\nYou can alternatively configure the plugin with properties on the command line:\n\n```sh\n  mvn com.google.cloud.functions:function-maven-plugin:0.10.1:run \\\n      -Drun.functionTarget=com.example.HelloWorld\n```\n\n### Running the Functions Framework directly\n\nYou can also run a function by using the Functions Framework jar directly.\nCopy the Functions Framework jar to a local location like this:\n\n```sh\nmvn dependency:copy \\\n    -Dartifact='com.google.cloud.functions.invoker:java-function-invoker:1.3.2' \\\n    -DoutputDirectory=.\n```\n\nIn this example we use the current directory `.` but you can specify any other\ndirectory to copy to. Then run your function:\n\n```sh\njava -jar java-function-invoker-1.3.2 \\\n    --classpath myfunction.jar \\\n    --target com.example.HelloWorld\n```\n\n\n## Running a function with Gradle\n\nFrom Gradle, similarily to running functions with the Functions Framework jar,\nwe can invoke the `Invoker` class with a `JavaExec` task.\n\n### Configuration in `build.gradle`\n\n```groovy\nconfigurations {\n    invoker\n}\n\ndependencies {\n    implementation 'com.google.cloud.functions:functions-framework-api:1.1.2'\n    invoker 'com.google.cloud.functions.invoker:java-function-invoker:1.3.2'\n}\n\ntasks.register(\"runFunction\", JavaExec) {\n    main = 'com.google.cloud.functions.invoker.runner.Invoker'\n    classpath(configurations.invoker)\n    inputs.files(configurations.runtimeClasspath, sourceSets.main.output)\n    args(\n            '--target', project.findProperty('run.functionTarget'),\n            '--port', project.findProperty('run.port') ?: 8080\n    )\n    doFirst {\n        args('--classpath', files(configurations.runtimeClasspath, sourceSets.main.output).asPath)\n    }\n}\n```\n\nThen in your terminal or IDE, you will be able to run the function locally with:\n\n```sh\ngradle runFunction -Prun.functionTarget=com.example.HelloWorld \\\n                   -Prun.port=8080\n```\n\nOr if you use the Gradle wrapper provided by your Gradle project build:\n\n```sh\n./gradlew runFunction -Prun.functionTarget=com.example.HelloWorld \\\n                      -Prun.port=8080\n```\n\n## Functions Framework configuration\n\nThere are a number of options that can be used to configure the Functions\nFramework, whether run directly or on the command line.\n\n### Which function to run\n\nA function is a Java class. You must specify the name of that class when running\nthe Functions Framework:\n\n```\n--target com.example.HelloWorld\n\u003cfunctionTarget\u003ecom.example.HelloWorld\u003c/functionTarget\u003e\n-Drun.functionTarget=com.example.HelloWorld\n-Prun.functionTarget=com.example.HelloWorld\n```\n\n* Invoker argument: `--target com.example.HelloWorld`\n* Maven `pom.xml`: `\u003cfunctionTarget\u003ecom.example.HelloWorld\u003c/functionTarget\u003e`\n* Maven CLI argument: `-Drun.functionTarget=com.example.HelloWorld`\n* Gradle CLI argument: `-Prun.functionTarget=com.example.HelloWorld`\n\n### Which port to listen on\n\nThe Functions Framework is an HTTP server that directs incoming HTTP requests to\nthe function code. By default this server listens on port 8080. Specify an\nalternative value like this:\n\n* Invoker argument: `--port 12345`\n* Maven `pom.xml`: `\u003cport\u003e12345\u003c/port\u003e`\n* Maven CLI argument: `-Drun.port=12345`\n* Gradle CLI argument: `-Prun.port=12345`\n\n### Function classpath\n\nFunction code runs with a classpath that includes the function code itself and\nits dependencies. The Maven plugin automatically computes the classpath based\non the dependencies expressed in `pom.xml`. When invoking the Functions\nFramework directly, you must use `--classpath` to indicate how to find the code\nand its dependencies. For example:\n\n```\njava -jar java-function-invoker-1.3.2 \\\n    --classpath 'myfunction.jar:/some/directory:/some/library/*' \\\n    --target com.example.HelloWorld\n```\n\nThe `--classpath` option works like\n[`java -classpath`](https://docs.oracle.com/en/java/javase/13/docs/specs/man/java.html#standard-options-for-java).\nIt is a list of entries separated by `:` (`;` on Windows), where each entry is:\n\n* a directory, in which case class `com.example.Foo` is looked for in a file\n  `com/example/Foo.class` under that directory;\n* a jar file, in which case class `com.example.Foo` is looked for in a file\n  `com/example/Foo.class` in that jar file;\n* a directory followed by `/*` (`\\*` on Windows), in which case each jar file\n  in that directory (file called `foo.jar`) is treated the same way as if it\n  had been named explicitly.\n\n#### Simplifying the classpath\n\nSpecifying the right classpath can be tricky. A simpler alternative is to\nbuild the function as a \"fat jar\", where the function code and all its\ndependencies are in a single jar file. Then `--classpath myfatfunction.jar`\nis enough. An example of how this is done is the Functions Framework jar itself,\nas seen\n[here](https://github.com/GoogleCloudPlatform/functions-framework-java/blob/b627f28/invoker/core/pom.xml#L153).\n\nAlternatively, you can arrange for your jar to have its own classpath, as\ndescribed\n[here](https://maven.apache.org/shared/maven-archiver/examples/classpath.html).\n","funding_links":[],"categories":["Serverless"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FGoogleCloudPlatform%2Ffunctions-framework-java","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FGoogleCloudPlatform%2Ffunctions-framework-java","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FGoogleCloudPlatform%2Ffunctions-framework-java/lists"}