Specifying dependencies in Java

You can use any Java compatible libraries with the Java 11 runtime to write Cloud Functions in Java. You can use either Maven or Gradle to manage dependencies for your Java Cloud Functions.

Declaring and managing dependencies

You can declare and manage dependencies using either Maven or Gradle:

  • To manage dependencies using Maven, you specify the dependencies in the <dependencies> section inside the pom.xml file of your project.

  • To manage dependencies using Gradle, you specify the dependencies in the build.gradle file of your project.

Using the Google Cloud Client Libraries for Java

Google Cloud Client Libraries for Java provide idiomatic access to Google Cloud services. To use a library, declare it as a dependency.

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

Maven

<project xmlns="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.cloud.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>
    <!-- Required for Function primitives -->
    <dependency>
      <groupId>com.google.cloud.functions</groupId>
      <artifactId>functions-framework-api</artifactId>
      <version>1.0.1</version>
      <scope>provided</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <!--
          Google Cloud Functions Framework Maven plugin

          This plugin allows you to run Cloud Functions Java code
          locally. Use the following terminal command to run a
          given function locally:

          mvn function:run -Drun.functionTarget=your.package.yourFunction
        -->
        <groupId>com.google.cloud.functions</groupId>
        <artifactId>function-maven-plugin</artifactId>
        <version>0.9.4</version>
        <configuration>
          <functionTarget>functions.HelloWorld</functionTarget>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

Gradle

Note that this build.gradle file includes a custom task to help you run functions locally. For more discussion of local testing, see Your First Function: Java.

apply plugin: '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.0.1'

  // To run function locally using Functions Framework's local invoker
  invoker 'com.google.cloud.functions.invoker:java-function-invoker:1.0.0-alpha-2-rc5'

  // These dependencies are only used by the tests.
  testImplementation 'com.google.cloud.functions:functions-framework-api:1.0.1'
  testImplementation 'junit:junit:4.12'
  testImplementation 'com.google.truth:truth:1.0.1'
  testImplementation 'org.mockito:mockito-core:3.4.0'

}

// Register a "runFunction" task to run the function locally
tasks.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('runFunction.target') ?: '',
    '--port', project.findProperty('runFunction.port') ?: 8080
  )
  doFirst {
    args('--classpath', files(configurations.runtimeClasspath, sourceSets.main.output).asPath)
  }
}