รายการวัสดุ (BOM) ของ Google Ads API จะจัดการเวอร์ชันทรัพยากร Dependency เพื่อหลีกเลี่ยงความขัดแย้งของทรัพยากร Dependency กับไลบรารี เช่น Guava และ GAX ที่เฟรมเวิร์กอื่นๆ ใช้ด้วย BOM ช่วยให้มั่นใจได้ว่าคุณใช้ทรัพยากร Dependency เวอร์ชันที่ถูกต้องซึ่งได้รับการทดสอบกับไลบรารีของไคลเอ็นต์ Google Ads
วิธีนี้เป็นวิธีที่แนะนำในการใช้ Artifact ของไลบรารีของไคลเอ็นต์
การกำหนดค่า Maven
เพิ่ม BOM ลงใน <dependencyManagement> และประกาศ google-ads ในไฟล์ pom.xml ดังนี้
<!-- Import the Bill of Materials (BOM) to ensure you're using compatible
versions of all google-ads libraries. -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.google.api-ads</groupId>
<artifactId>google-ads-bom</artifactId>
<version>48.1.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!-- Add the google-ads dependency, without a version. The version is
managed by the BOM. -->
<dependency>
<groupId>com.google.api-ads</groupId>
<artifactId>google-ads</artifactId>
</dependency>
การกำหนดค่า Gradle
Gradle 5.0 ขึ้นไปรองรับการนําเข้า BOM แบบเนทีฟโดยใช้ platform()
ใน Groovy DSL (build.gradle) ให้ทำดังนี้
// Import the Bill of Materials (BOM).
implementation platform('com.google.api-ads:google-ads-bom:48.1.0')
// Add the google-ads dependency, without a version.
implementation 'com.google.api-ads:google-ads'
ใน Kotlin DSL (build.gradle.kts) ให้ทำดังนี้
// Import the Bill of Materials (BOM).
implementation(platform("com.google.api-ads:google-ads-bom:48.1.0"))
// Add the google-ads dependency, without a version.
implementation("com.google.api-ads:google-ads")
การประกาศทรัพยากร Dependency ที่ครอบคลุมโดย BOM
BOM ของ Google Ads API มีการจัดการเวอร์ชันสำหรับไลบรารีทั่วไปหลายรายการ เช่น Guava, Protobuf, GAX และ gRPC เพื่อหลีกเลี่ยงความขัดแย้งของทรัพยากร Dependency ที่อาจเกิดขึ้น คุณต้องไม่ระบุเวอร์ชันเมื่อประกาศทรัพยากร Dependency ที่ BOM ครอบคลุม BOM จะจัดการเวอร์ชันของไลบรารีเหล่านี้โดยอัตโนมัติเพื่อให้มั่นใจว่าเข้ากันได้
เช่น หากต้องการประกาศทรัพยากร Dependency Guava ใน Maven ให้ใช้โค้ดต่อไปนี้
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<!-- NO VERSION SPECIFIED -->
</dependency>
ใน Gradle Groovy DSL (build.gradle) ให้ทำดังนี้
implementation 'com.google.guava:guava' // NO VERSION SPECIFIED
ใน Gradle Kotlin DSL (build.gradle.kts) ให้ทำดังนี้
implementation("com.google.guava:guava") // NO VERSION SPECIFIED
การละเว้นเวอร์ชันจะช่วยให้ BOM จัดการเวอร์ชันได้ ซึ่งจะช่วยป้องกันปัญหาที่เกิดจากเวอร์ชันของทรัพยากร Dependency ที่เข้ากันไม่ได้ ตัวบ่งชี้ที่พบบ่อยของความขัดแย้งของทรัพยากร Dependency ได้แก่ NoSuchMethodError หรือ ClassNotFoundException ซึ่งมักจะแก้ไขได้โดยตรวจสอบว่าทรัพยากร Dependency ทั้งหมดที่จัดการโดย BOM ไม่มีการระบุเวอร์ชัน