指定 Java 依赖项

您可以将任何与 Java 兼容的库与受支持的 Java 运行时结合使用,以使用 Java 语言编写 Cloud Functions 函数。您可以使用 MavenGradle 来管理 Java Cloud Functions 函数的依赖项。

声明和管理依赖项

您可以使用 Maven 或 Gradle 声明和管理依赖项:

  • 如需使用 Maven 管理依赖项,请执行以下操作:

    • 在项目的 pom.xml 文件的 <dependencies> 部分中指定依赖项。

    • 要管理项目的 Maven 依赖项,您可以使用 Maven 封装容器。如果您不使用 Maven 封装容器,则 Cloud Functions 会在运行 gcloud functions deploy 时默认使用最新版本的 Maven。

  • 如需使用 Gradle 管理依赖项,您需要在项目的 build.gradle 文件中指定依赖项。

Functions 框架是所有函数所需的依赖项。虽然 Cloud Functions 会在创建函数时代表您安装该框架,但为清楚起见,我们建议您将其添加为显式依赖项。

如果函数依赖于专用依赖项,我们建议您将 functions-framework 镜像到您的私有注册表。将镜像的 functions-framework 作为依赖项添加到函数中,以避免从公共互联网安装软件包。

使用 Java 版 Google Cloud 客户端库

Java 版 Google Cloud 客户端库提供对 Google Cloud 服务的惯用访问权限。如需使用某个库,请将其声明为依赖项。

通常情况下,您只需要在函数需要的特定库上声明依赖项。例如:

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.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.1.0</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.11.0</version>
        <configuration>
          <functionTarget>functions.HelloWorld</functionTarget>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

Gradle

请注意,此 build.gradle 文件包含一项自定义任务,可以帮助您在本地运行函数。如需详细了解本地测试,请参阅您的第一个函数: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.1.0'

  // To run function locally using Functions Framework's local invoker
  invoker '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 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('run.functionTarget') ?: '',
    '--port', project.findProperty('run.port') ?: 8080
  )
  doFirst {
    args('--classpath', files(configurations.runtimeClasspath, sourceSets.main.output).asPath)
  }
}