Mit Sammlungen den Überblick behalten
Sie können Inhalte basierend auf Ihren Einstellungen speichern und kategorisieren.
Sie können jede Java-kompatible Bibliothek mit einer unterstützten Java-Laufzeit verwenden, um Cloud Run Functions in Java zu schreiben. Sie können entweder Maven oder Gradle verwenden, um Abhängigkeiten für Ihre Java Cloud Run Functions zu verwalten.
Abhängigkeiten deklarieren und verwalten
Sie können Abhängigkeiten mit Maven oder Gradle deklarieren und verwalten:
So verwalten Sie Abhängigkeiten mit Maven:
Geben Sie die Abhängigkeiten im Abschnitt <dependencies> in der Datei pom.xml Ihres Projekts an.
Sie können den Maven-Wrapper verwenden, um die Abhängigkeit Ihres Projekts von Maven selbst zu verwalten. Wenn Sie den Maven-Wrapper nicht verwenden, verwenden Cloud Run Functions standardmäßig eine aktuelle Version von Maven, wenn Sie gcloud functions deploy ausführen.
Wenn Sie Abhängigkeiten mit Gradle verwalten möchten, müssen Sie diese in der Datei build.gradle Ihres Projekts angeben.
Das Functions Framework ist eine erforderliche Abhängigkeit für alle Funktionen. Obwohl Cloud Run-Funktionen sie bei der Erstellung der Funktion für Sie installiert, empfehlen wir, sie zur Klarstellung hinzuzufügen.
Wenn Ihre Funktion private Abhängigkeiten benötigt, sollten Sie functions-framework in Ihre private Registry spiegeln. Fügen Sie die gespiegelte functions-framework als Abhängigkeit zu Ihrer Funktion hinzu, um die Installation des Pakets aus dem öffentlichen Internet zu vermeiden.
Google Cloud-Clientbibliotheken für Java verwenden
Google Cloud-Clientbibliotheken für Java bieten idiomatischen Zugriff auf Google Cloud -Dienste. Wenn Sie eine Bibliothek verwenden möchten, müssen Sie sie als Abhängigkeit deklarieren.
Normalerweise deklarieren Sie Abhängigkeiten nur von den Bibliotheken, die Ihre Funktion benötigt. Beispiel:
Die Datei build.gradle enthält eine benutzerdefinierte Aufgabe, damit Sie Funktionen lokal ausführen können.
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)}}
[[["Leicht verständlich","easyToUnderstand","thumb-up"],["Mein Problem wurde gelöst","solvedMyProblem","thumb-up"],["Sonstiges","otherUp","thumb-up"]],[["Schwer verständlich","hardToUnderstand","thumb-down"],["Informationen oder Beispielcode falsch","incorrectInformationOrSampleCode","thumb-down"],["Benötigte Informationen/Beispiele nicht gefunden","missingTheInformationSamplesINeed","thumb-down"],["Problem mit der Übersetzung","translationIssue","thumb-down"],["Sonstiges","otherDown","thumb-down"]],["Zuletzt aktualisiert: 2025-09-04 (UTC)."],[],[],null,["# Specify dependencies in Java\n\nYou can use any Java compatible libraries with a\n[supported Java runtime](/run/docs/configuring/services/runtime-base-images#java)\nto write Cloud Run functions in Java. You can use either\n[Maven](https://maven.apache.org) or [Gradle](https://gradle.org) to manage\ndependencies for your Java Cloud Run functions.\n\nDeclaring and managing dependencies\n-----------------------------------\n\nYou can declare and manage dependencies using either Maven or Gradle:\n\n- To manage dependencies using Maven:\n\n - Specify the dependencies in the `\u003cdependencies\u003e` section inside the\n [`pom.xml`](http://maven.apache.org/guides/introduction/introduction-to-the-pom.html)\n file of your project.\n\n - To manage your project's dependency on Maven itself, you can use the\n [Maven Wrapper](https://maven.apache.org/wrapper/). If you don't use the\n Maven Wrapper, Cloud Run functions defaults to using a recent\n version of Maven when running [`gcloud functions deploy`](/sdk/gcloud/reference/functions/deploy).\n\n- To manage dependencies using Gradle, you specify the dependencies in the\n [`build.gradle`](https://docs.gradle.org/current/userguide/tutorial_using_tasks.html)\n file of your project.\n\nThe [Functions Framework](https://github.com/GoogleCloudPlatform/functions-framework-java)\nis a required dependency for all functions. Although\nCloud Run functions installs it on your behalf when the function\nis created, we recommend that you include it as an explicit dependency for\nclarity.\n\nIf your\nfunction relies on private dependencies, we recommend that you\nmirror `functions-framework` to your private registry. Include the mirrored\n`functions-framework` as a dependency to your function to avoid installing the\npackage from the public internet.\n\nUsing the Google Cloud Client Libraries for Java\n------------------------------------------------\n\n[Google Cloud Client Libraries for\nJava](https://github.com/googleapis/google-cloud-java) provide idiomatic access\nto Google Cloud services. To use a library, declare it as a dependency.\n\nTypically you only declare dependencies on the specific libraries that your\nfunction needs. For example: \n\n### Maven\n\n \u003cproject xmlns=\"http://maven.apache.org/POM/4.0.0\"\n xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\"\u003e\n \u003cmodelVersion\u003e4.0.0\u003c/modelVersion\u003e\n\n \u003cgroupId\u003ecom.example.functions\u003c/groupId\u003e\n \u003cartifactId\u003efunctions-hello-world\u003c/artifactId\u003e\n \u003cversion\u003e1.0.0-SNAPSHOT\u003c/version\u003e\n \u003cproperties\u003e\n \u003cmaven.compiler.target\u003e11\u003c/maven.compiler.target\u003e\n \u003cmaven.compiler.source\u003e11\u003c/maven.compiler.source\u003e\n \u003c/properties\u003e\n\n \u003cdependencies\u003e\n \u003c!-- Required for Function primitives --\u003e\n \u003cdependency\u003e\n \u003cgroupId\u003ecom.google.cloud.functions\u003c/groupId\u003e\n \u003cartifactId\u003efunctions-framework-api\u003c/artifactId\u003e\n \u003cversion\u003e1.1.0\u003c/version\u003e\n \u003cscope\u003eprovided\u003c/scope\u003e\n \u003c/dependency\u003e\n \u003c/dependencies\u003e\n\n \u003cbuild\u003e\n \u003cplugins\u003e\n \u003cplugin\u003e\n \u003c!--\n Google Cloud Functions Framework Maven plugin\n\n This plugin allows you to run Cloud Functions Java code\n locally. Use the following terminal command to run a\n given function locally:\n\n mvn function:run -Drun.functionTarget=your.package.yourFunction\n --\u003e\n \u003cgroupId\u003ecom.google.cloud.functions\u003c/groupId\u003e\n \u003cartifactId\u003efunction-maven-plugin\u003c/artifactId\u003e\n \u003cversion\u003e0.11.0\u003c/version\u003e\n \u003cconfiguration\u003e\n \u003cfunctionTarget\u003efunctions.HelloWorld\u003c/functionTarget\u003e\n \u003c/configuration\u003e\n \u003c/plugin\u003e\n \u003c/plugins\u003e\n \u003c/build\u003e\n \u003c/project\u003e\n\n### Gradle\n\nNote that this `build.gradle` file includes a custom task to help you run\nfunctions locally. \n\n apply plugin: 'java'\n\n repositories {\n jcenter()\n mavenCentral()\n }\n configurations {\n invoker\n }\n\n dependencies {\n // Every function needs this dependency to get the Functions Framework API.\n compileOnly 'com.google.cloud.functions:functions-framework-api:1.1.0'\n\n // To run function locally using Functions Framework's local invoker\n invoker 'com.google.cloud.functions.invoker:java-function-invoker:1.3.1'\n\n // These dependencies are only used by the tests.\n testImplementation 'com.google.cloud.functions:functions-framework-api:1.1.0'\n testImplementation 'junit:junit:4.13.2'\n testImplementation 'com.google.truth:truth:1.4.0'\n testImplementation 'org.mockito:mockito-core:5.10.0'\n\n }\n\n // Register a \"runFunction\" task to run the function locally\n tasks.register(\"runFunction\", JavaExec) {\n main = 'com.google.cloud.functions.invoker.runner.Invoker'\n classpath(configurations.invoker)\n inputs.files(configurations.runtimeClasspath, sourceSets.main.output)\n args(\n '--target', project.findProperty('run.functionTarget') ?: '',\n '--port', project.findProperty('run.port') ?: 8080\n )\n doFirst {\n args('--classpath', files(configurations.runtimeClasspath, sourceSets.main.output).asPath)\n }\n }"]]