Specify dependencies in Java

You can use any Java compatible libraries with asupported Java runtimeto write Cloud Run functions in Java. You can use eitherMaven orGradle to managedependencies 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 thepom.xmlfile of your project.

    • To manage your project's dependency on Maven itself, you can use theMaven Wrapper. If you don't use theMaven Wrapper, Cloud Run functions defaults to using a recentversion of Maven when runninggcloud functions deploy.

  • To manage dependencies using Gradle, you specify the dependencies in thebuild.gradlefile of your project.

TheFunctions Frameworkis a required dependency for all functions. AlthoughCloud Run functions installs it on your behalf when the functionis created, we recommend that you include it as an explicit dependency forclarity.

If yourfunction relies on private dependencies, we recommend that youmirrorfunctions-framework to your private registry. Include the mirroredfunctions-framework as a dependency to your function to avoid installing thepackage from the public internet.

Using the Google Cloud Client Libraries for Java

Google Cloud Client Libraries forJava provide idiomatic accessto Google Cloud services. To use a library, declare it as a dependency.

Typically you only declare dependencies on the specific libraries that yourfunction needs. For example:

Maven

<projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.example.functions</groupId><artifactId>functions-hello-world</artifactId><version>1.0.0-SNAPSHOT</version><properties><maven.compiler.target>11</maven.compiler.target><maven.compiler.source>11</maven.compiler.source></properties><dependencies><!--RequiredforFunctionprimitives--><dependency><groupId>com.google.cloud.functions</groupId><artifactId>functions-framework-api</artifactId><version>1.1.0</version><scope>provided</scope></dependency></dependencies><build><plugins><plugin><!--GoogleCloudFunctionsFrameworkMavenpluginThispluginallowsyoutorunCloudFunctionsJavacodelocally.Usethefollowingterminalcommandtorunagivenfunctionlocally:mvnfunction:run-Drun.functionTarget=your.package.yourFunction--><groupId>com.google.cloud.functions</groupId><artifactId>function-maven-plugin</artifactId><version>0.11.0</version><configuration><functionTarget>functions.HelloWorld</functionTarget></configuration></plugin></plugins></build></project>

Gradle

Note that thisbuild.gradle file includes a custom task to help you runfunctions 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)}}

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2026-02-19 UTC.