Jika Anda membuat fungsi baru, lihat Panduan Memulai Konsol di Cloud Run. Konten di halaman ini hanya berlaku untuk fungsi lama yang sudah ada yang dibuat dengan Cloud Functions v1 API.
Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Menentukan dependensi di Java
Anda dapat menggunakan library apa pun yang kompatibel dengan Java dengan
runtime Java yang didukung untuk menulis
fungsi Cloud Run di Java. Anda dapat menggunakan
Maven atau
Gradle untuk mengelola dependensi
untuk fungsi Cloud Run Java Anda.
Mendeklarasikan dan mengelola dependensi
Anda bisa mendeklarasikan dan mengelola dependensi menggunakan Maven atau Gradle:
Untuk mengelola dependensi menggunakan Maven:
Tentukan dependensi di bagian <dependencies> di dalam file
pom.xml
project Anda.
Untuk mengelola dependensi project di Maven itu sendiri, Anda dapat menggunakan
Maven Wrapper. Jika Anda tidak menggunakan
Maven Wrapper, fungsi Cloud Run secara default akan menggunakan Maven versi
terbaru saat menjalankan gcloud functions deploy.
Untuk mengelola dependensi menggunakan Gradle, tentukan dependensi dalam
file build.gradle
project Anda.
Functions Framework adalah dependensi wajib untuk semua fungsi. Meskipun fungsi Cloud Run
menginstalnya untuk Anda saat fungsi tersebut dibuat, sebaiknya
sertakan sebagai dependensi eksplisit agar lebih jelas.
Jika fungsi
Anda bergantung pada dependensi pribadi, sebaiknya
cerminkan functions-framework ke registry pribadi Anda. Sertakan functions-framework
yang diduplikasi sebagai dependensi ke fungsi Anda untuk menghindari penginstalan
paket dari internet publik.
Menggunakan Library Klien Google Cloud untuk Java
Library Klien Google Cloud untuk
Java menyediakan akses idiomatis
ke layanan Google Cloud. Untuk menggunakan library, deklarasikan sebagai dependensi.
Biasanya, Anda hanya mendeklarasikan dependensi pada library tertentu yang diperlukan
aplikasi Anda. Contoh:
Perhatikan bahwa file build.gradle ini menyertakan tugas kustom untuk membantu Anda menjalankan
fungsi secara lokal. Untuk pembahasan lebih lanjut tentang pengujian lokal, lihat Fungsi Pertama
Anda:
Java.
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)}}
[[["Mudah dipahami","easyToUnderstand","thumb-up"],["Memecahkan masalah saya","solvedMyProblem","thumb-up"],["Lainnya","otherUp","thumb-up"]],[["Sulit dipahami","hardToUnderstand","thumb-down"],["Informasi atau kode contoh salah","incorrectInformationOrSampleCode","thumb-down"],["Informasi/contoh yang saya butuhkan tidak ada","missingTheInformationSamplesINeed","thumb-down"],["Masalah terjemahan","translationIssue","thumb-down"],["Lainnya","otherDown","thumb-down"]],["Terakhir diperbarui pada 2025-08-19 UTC."],[[["\u003cp\u003eJava dependencies for Cloud Run functions can be managed using either Maven or Gradle.\u003c/p\u003e\n"],["\u003cp\u003eMaven dependencies are declared within the \u003ccode\u003e<dependencies>\u003c/code\u003e section of the \u003ccode\u003epom.xml\u003c/code\u003e file, while Gradle dependencies are specified in the \u003ccode\u003ebuild.gradle\u003c/code\u003e file.\u003c/p\u003e\n"],["\u003cp\u003eThe Functions Framework is a required dependency for all Cloud Run functions and is recommended to be explicitly included in your project dependencies.\u003c/p\u003e\n"],["\u003cp\u003eGoogle Cloud Client Libraries for Java can be used by declaring them as dependencies in either Maven or Gradle, allowing you to access Google Cloud services.\u003c/p\u003e\n"],["\u003cp\u003eMirroring the function framework to a private registry is recommended if your function relies on private dependencies.\u003c/p\u003e\n"]]],[],null,["# Specify dependencies in Java\n============================\n\nYou can use any Java compatible libraries with a\n[supported Java runtime](/functions/1stgendocs/runtime-support#java) to write\nCloud Run functions in Java. You can use either\n[Maven](https://maven.apache.org/) or\n[Gradle](https://gradle.org/) to manage dependencies\nfor 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 do not use the\n Maven Wrapper, Cloud Run functions defaults to using a recent version of\n 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](/functions/docs/functions-framework) is a\nrequired dependency for all functions. Although Cloud Run functions\ninstalls it on your behalf when the function is created, we recommend\nthat you include it as an explicit dependency for clarity.\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. For more discussion of local testing, see [Your First\nFunction:\nJava](/functions/1stgendocs/create-deploy-http-java-1st-gen#building_and_testing_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 }"]]