Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
Puoi utilizzare qualsiasi libreria compatibile con Java con un
runtime Java supportato
per scrivere funzioni Cloud Run in Java. Puoi utilizzare Maven o Gradle per gestire le dipendenze delle tue funzioni Cloud Run Java.
Dichiarazione e gestione delle dipendenze
Puoi dichiarare e gestire le dipendenze utilizzando Maven o Gradle:
Per gestire le dipendenze utilizzando Maven:
Specifica le dipendenze nella sezione <dependencies> all'interno del file
pom.xml
del tuo progetto.
Per gestire la dipendenza del progetto da Maven, puoi utilizzare
Maven Wrapper. Se non utilizzi
Maven Wrapper, Cloud Run Functions utilizza per impostazione predefinita una versione
recente di Maven quando esegui gcloud functions deploy.
Per gestire le dipendenze utilizzando Gradle, specifica le dipendenze nel file
build.gradle
del tuo progetto.
Functions Framework
è una dipendenza obbligatoria per tutte le funzioni. Sebbene
Cloud Run Functions lo installi per tuo conto quando viene creata la funzione, ti consigliamo di includerlo come dipendenza esplicita per
chiarezza.
Se la tua
funzione si basa su dipendenze private, ti consigliamo di
eseguire il mirroring di functions-framework nel tuo registro privato. Includi functions-framework sottoposto a mirroring come dipendenza della tua funzione per evitare di installare il pacchetto dalla rete internet pubblica.
Utilizzo delle librerie client Google Cloud per Java
Tieni presente che questo file build.gradle include un'attività personalizzata per aiutarti a eseguire
le funzioni localmente.
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)}}
[[["Facile da capire","easyToUnderstand","thumb-up"],["Il problema è stato risolto","solvedMyProblem","thumb-up"],["Altra","otherUp","thumb-up"]],[["Difficile da capire","hardToUnderstand","thumb-down"],["Informazioni o codice di esempio errati","incorrectInformationOrSampleCode","thumb-down"],["Mancano le informazioni o gli esempi di cui ho bisogno","missingTheInformationSamplesINeed","thumb-down"],["Problema di traduzione","translationIssue","thumb-down"],["Altra","otherDown","thumb-down"]],["Ultimo aggiornamento 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 }"]]