Java で依存関係を指定する

Java 互換ライブラリをサポートされている Java ランタイムとともに使用することで、Java で Cloud Functions の関数を作成できます。Maven または Gradle を使用して、Java Cloud Functions の関数の依存関係を管理できます。

依存関係の宣言と管理

依存関係の宣言と管理は、Maven または Gradle を使用して行うことができます。

  • Maven を使用して依存関係を管理するには:

    • プロジェクトの pom.xml ファイルにある <dependencies> セクションで依存関係を指定します。

    • プロジェクトの Maven 自体への依存関係を管理するには、Maven ラッパーを使用します。Maven ラッパーを使用しない場合、Cloud Functions は gcloud functions deploy の実行時にデフォルトで最新バージョンの Maven を使用します。

  • Gradle を使用して依存関係を管理するには、プロジェクトの build.gradle ファイルで依存関係を指定します。

Functions Framework は、すべての関数に必須の依存関係です。Cloud Functions は、関数の作成時にユーザーの代理でこの依存関係をインストールしますが、明確化のため明示的な依存関係として含めておくことをおすすめします。

関数がプライベート依存関係を利用している場合は、functions-framework をプライベート レジストリにミラーリングすることをおすすめします。ミラーリング対象の functions-framework を依存関係として関数に含めることで、公共のインターネットからパッケージがインストールされないようにします。

Java 用 Google Cloud クライアント ライブラリを使用する

Google Cloud サービスへの汎用的なアクセスには、Java 用 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)
  }
}