Stay organized with collections
Save and categorize content based on your preferences.
You can use any Java compatible libraries with a
supported Java runtime
to write Cloud Run functions in Java. You can use either
Maven or Gradle to manage
dependencies for your Java Cloud Run functions.
Declaring and managing dependencies
You can declare and manage dependencies using either Maven or Gradle:
To manage dependencies using Maven:
Specify the dependencies in the <dependencies> section inside the
pom.xml
file of your project.
To manage your project's dependency on Maven itself, you can use the
Maven Wrapper. If you don't use the
Maven Wrapper, Cloud Run functions defaults to using a recent
version of Maven when running gcloud functions deploy.
To manage dependencies using Gradle, you specify the dependencies in the
build.gradle
file of your project.
The Functions Framework
is a required dependency for all functions. Although
Cloud Run functions installs it on your behalf when the function
is created, we recommend that you include it as an explicit dependency for
clarity.
If your
function relies on private dependencies, we recommend that you
mirror functions-framework to your private registry. Include the mirrored
functions-framework as a dependency to your function to avoid installing the
package from the public internet.
Note that this build.gradle file includes a custom task to help you run
functions locally.
applyplugin:'java'repositories{jcenter()mavenCentral()}configurations{invoker}dependencies{// Every function needs this dependency to get the Functions Framework API.compileOnly'com.google.cloud.functions:functions-framework-api:1.1.0'// To run function locally using Functions Framework's local invokerinvoker'com.google.cloud.functions.invoker:java-function-invoker:1.3.1'// These dependencies are only used by the tests.testImplementation'com.google.cloud.functions:functions-framework-api:1.1.0'testImplementation'junit:junit:4.13.2'testImplementation'com.google.truth:truth:1.4.0'testImplementation'org.mockito:mockito-core:5.10.0'}// Register a "runFunction" task to run the function locallytasks.register("runFunction",JavaExec){main='com.google.cloud.functions.invoker.runner.Invoker'classpath(configurations.invoker)inputs.files(configurations.runtimeClasspath,sourceSets.main.output)args('--target',project.findProperty('run.functionTarget')?:'','--port',project.findProperty('run.port')?:8080)doFirst{args('--classpath',files(configurations.runtimeClasspath,sourceSets.main.output).asPath)}}
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-28 UTC."],[],[],null,["# Specify dependencies in Java\n\nYou can use any Java compatible libraries with a\n[supported Java runtime](/run/docs/configuring/services/runtime-base-images#java)\nto write Cloud Run functions in Java. You can use either\n[Maven](https://maven.apache.org) or [Gradle](https://gradle.org) to manage\ndependencies for your Java Cloud Run functions.\n\nDeclaring and managing dependencies\n-----------------------------------\n\nYou can declare and manage dependencies using either Maven or Gradle:\n\n- To manage dependencies using Maven:\n\n - Specify the dependencies in the `\u003cdependencies\u003e` section inside the\n [`pom.xml`](http://maven.apache.org/guides/introduction/introduction-to-the-pom.html)\n file of your project.\n\n - To manage your project's dependency on Maven itself, you can use the\n [Maven Wrapper](https://maven.apache.org/wrapper/). If you don't use the\n Maven Wrapper, Cloud Run functions defaults to using a recent\n version of Maven when running [`gcloud functions deploy`](/sdk/gcloud/reference/functions/deploy).\n\n- To manage dependencies using Gradle, you specify the dependencies in the\n [`build.gradle`](https://docs.gradle.org/current/userguide/tutorial_using_tasks.html)\n file of your project.\n\nThe [Functions Framework](https://github.com/GoogleCloudPlatform/functions-framework-java)\nis a required dependency for all functions. Although\nCloud Run functions installs it on your behalf when the function\nis created, we recommend that you include it as an explicit dependency for\nclarity.\n\nIf your\nfunction relies on private dependencies, we recommend that you\nmirror `functions-framework` to your private registry. Include the mirrored\n`functions-framework` as a dependency to your function to avoid installing the\npackage from the public internet.\n\nUsing the Google Cloud Client Libraries for Java\n------------------------------------------------\n\n[Google Cloud Client Libraries for\nJava](https://github.com/googleapis/google-cloud-java) provide idiomatic access\nto Google Cloud services. To use a library, declare it as a dependency.\n\nTypically you only declare dependencies on the specific libraries that your\nfunction needs. For example: \n\n### Maven\n\n \u003cproject xmlns=\"http://maven.apache.org/POM/4.0.0\"\n xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\"\u003e\n \u003cmodelVersion\u003e4.0.0\u003c/modelVersion\u003e\n\n \u003cgroupId\u003ecom.example.functions\u003c/groupId\u003e\n \u003cartifactId\u003efunctions-hello-world\u003c/artifactId\u003e\n \u003cversion\u003e1.0.0-SNAPSHOT\u003c/version\u003e\n \u003cproperties\u003e\n \u003cmaven.compiler.target\u003e11\u003c/maven.compiler.target\u003e\n \u003cmaven.compiler.source\u003e11\u003c/maven.compiler.source\u003e\n \u003c/properties\u003e\n\n \u003cdependencies\u003e\n \u003c!-- Required for Function primitives --\u003e\n \u003cdependency\u003e\n \u003cgroupId\u003ecom.google.cloud.functions\u003c/groupId\u003e\n \u003cartifactId\u003efunctions-framework-api\u003c/artifactId\u003e\n \u003cversion\u003e1.1.0\u003c/version\u003e\n \u003cscope\u003eprovided\u003c/scope\u003e\n \u003c/dependency\u003e\n \u003c/dependencies\u003e\n\n \u003cbuild\u003e\n \u003cplugins\u003e\n \u003cplugin\u003e\n \u003c!--\n Google Cloud Functions Framework Maven plugin\n\n This plugin allows you to run Cloud Functions Java code\n locally. Use the following terminal command to run a\n given function locally:\n\n mvn function:run -Drun.functionTarget=your.package.yourFunction\n --\u003e\n \u003cgroupId\u003ecom.google.cloud.functions\u003c/groupId\u003e\n \u003cartifactId\u003efunction-maven-plugin\u003c/artifactId\u003e\n \u003cversion\u003e0.11.0\u003c/version\u003e\n \u003cconfiguration\u003e\n \u003cfunctionTarget\u003efunctions.HelloWorld\u003c/functionTarget\u003e\n \u003c/configuration\u003e\n \u003c/plugin\u003e\n \u003c/plugins\u003e\n \u003c/build\u003e\n \u003c/project\u003e\n\n### Gradle\n\nNote that this `build.gradle` file includes a custom task to help you run\nfunctions locally. \n\n apply plugin: 'java'\n\n repositories {\n jcenter()\n mavenCentral()\n }\n configurations {\n invoker\n }\n\n dependencies {\n // Every function needs this dependency to get the Functions Framework API.\n compileOnly 'com.google.cloud.functions:functions-framework-api:1.1.0'\n\n // To run function locally using Functions Framework's local invoker\n invoker 'com.google.cloud.functions.invoker:java-function-invoker:1.3.1'\n\n // These dependencies are only used by the tests.\n testImplementation 'com.google.cloud.functions:functions-framework-api:1.1.0'\n testImplementation 'junit:junit:4.13.2'\n testImplementation 'com.google.truth:truth:1.4.0'\n testImplementation 'org.mockito:mockito-core:5.10.0'\n\n }\n\n // Register a \"runFunction\" task to run the function locally\n tasks.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 }"]]