在 Java 中指定依附元件

您可以使用任何 Java 相容程式庫,搭配支援的 Java 執行階段,在 Java 中編寫 Cloud Run 函式。您可以使用 MavenGradle 來管理 Java Cloud Run 函式的依附元件。

宣告及管理依附元件

您可以使用 Maven 或 Gradle 宣告及管理依附元件:

  • 如何使用 Maven 管理依附元件:

    • 在專案的 pom.xml 檔案中 <dependencies> 區段中指定依附元件。

    • 如要管理專案對 Maven 的依附元件,您可以使用 Maven 包裝函式。如果您未使用 Maven 包裝函式,Cloud Run 函式在執行 gcloud functions deploy 時,預設會使用最新版本的 Maven。

  • 如要使用 Gradle 管理依附元件,請在專案的 build.gradle 檔案中指定依附元件。

Functions Framework 是所有函式的必要依附元件。雖然 Cloud Run 函式會在函式建立時代您安裝此套件,但為了清楚起見,建議您將其納入為明確的依附元件。

如果函式需要私人依附元件,建議您將 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 檔案包含自訂工作,可協助您在本機執行函式。

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)
  }
}