การประกาศการพึ่งพาของโครงการ
เรียนรู้วิธีต่าง ๆ ในการประกาศการพึ่งพาใน `build.gradle` สำหรับขอบเขตและชนิดต่าง ๆ
การประกาศการพึ่งพาของโครงการ เป็นบทเรียน Groovy & Gradle: JVM Automation and Build Engineering ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Groovy & Gradle: JVM Automation and Build Engineering และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Groovy & Gradle: JVM Automation and Build Engineering มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Project Needs & Dependencies
Modern software projects rarely work in isolation. They often rely on external code, known as dependencies, to perform various tasks.
Think of dependencies as pre-built tools or libraries that save you from writing common functionalities from scratch, like logging, parsing JSON, or connecting to a database.
- Libraries: Collections of code (e.g., Apache Commons).
- Frameworks: Structured foundations for applications (e.g., Spring Boot).
- Modules: Components of a larger system.
Gradle's `dependencies` Block
In Gradle, you declare all your project's dependencies within a dedicated dependencies { ... } block in your build.gradle file.
This block is central to managing what your project needs to compile, test, and run.
/* build.gradle */
plugins {
id 'java'
}
repositories {
mavenCentral()
}
dependencies {
// Your dependencies go here!
}Where Gradle Finds Dependencies
Before Gradle can fetch dependencies, it needs to know where to look. This is defined in the repositories { ... } block.
The most common repository is Maven Central, a vast public repository for Java libraries. By declaring mavenCentral(), you tell Gradle to search there.
/* build.gradle */
repositories {
// Tells Gradle to look for dependencies in Maven Central
mavenCentral()
}`implementation`: The Standard
The implementation configuration is your go-to for most production dependencies. It adds the dependency to the compilation classpath and also to the runtime classpath.
Crucially, implementation dependencies are not exposed to other modules that consume your project, which helps with faster compilation and better encapsulation.
/* build.gradle */
dependencies {
// Adds SLF4J API for logging, used during compile and runtime
implementation 'org.slf4j:slf4j-api:1.7.30'
}Example: Using `implementation`
Let's see a simple Java class that uses a library added with implementation. We'll add the Apache Commons Lang library for string utilities.
/* build.gradle */
plugins {
id 'java'
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.apache.commons:commons-lang3:3.12.0'
}
/* src/main/java/App.java */
import org.apache.commons.lang3.StringUtils;
public class App {
public static void main(String[] args) {
String text = " Hello CoddyKit ";
System.out.println(StringUtils.trim(text));
}
}`testImplementation`: For Testing Only
Dependencies needed only for running tests (like JUnit 5 or Mockito) should be declared with testImplementation.
These dependencies are added to the test compilation and runtime classpaths but are not included in your final application artifact or exposed to production code.
/* build.gradle */
dependencies {
// Adds JUnit 5 for unit testing
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
}`runtimeOnly`: Just for Running
Sometimes, a dependency is only needed when your application actually runs, not during compilation. This is where runtimeOnly comes in.
A common use case is a JDBC driver. Your code compiles against the JDBC API (which is often part of Java or a compileOnly dependency), but the specific driver (e.g., HSQLDB, PostgreSQL) is only needed at runtime.
/* build.gradle */
dependencies {
// JDBC driver for HSQLDB, only needed when the app runs
runtimeOnly 'org.hsqldb:hsqldb:2.5.1'
}`compileOnly`: Compile-Time Helpers
Use compileOnly for dependencies that are required during compilation but should not be packaged with your final application or available at runtime.
Examples include annotation processors like Lombok, or API specifications (e.g., Servlet API) when your application will run in an environment that already provides them.
/* build.gradle */
dependencies {
// Lombok for reducing boilerplate code, not needed at runtime
compileOnly 'org.projectlombok:lombok:1.18.20'
annotationProcessor 'org.projectlombok:lombok:1.18.20'
}Local Files as Dependencies
While generally discouraged, you might sometimes need to include a local JAR file that isn't available in any public repository (e.g., a proprietary library).
You can declare these using the files() method within the dependencies block. Make sure the path is correct relative to your project root.
/* build.gradle */
dependencies {
// Includes a JAR file located in the 'libs' folder
implementation files('libs/my-custom-lib.jar')
}Dependency Configuration Quiz
You are building a web application and need to include:
- A logging library for all code.
- JUnit 5 for unit tests.
- The Servlet API, which your application server already provides.
Which Gradle dependency configurations would you use for these three items?
Recap: Declaring Dependencies
Great job! You've learned the essentials of declaring dependencies in Gradle:
- The
dependencies { ... }block is where you list all external libraries. repositories { ... }tells Gradle where to find these libraries (e.g.,mavenCentral()).implementationfor most production code.testImplementationfor test-specific libraries.runtimeOnlyfor dependencies only needed at runtime.compileOnlyfor compile-time only dependencies not packaged.- You can also include local JARs using
files().
Understanding these configurations helps you build efficient and well-structured projects!
คำถามที่พบบ่อย
บทเรียน “การประกาศการพึ่งพาของโครงการ” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การประกาศการพึ่งพาของโครงการ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Groovy & Gradle: JVM Automation and Build Engineering ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Groovy & Gradle: JVM Automation and Build Engineering มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การประกาศการพึ่งพาของโครงการ”
เรียนรู้วิธีต่าง ๆ ในการประกาศการพึ่งพาใน `build.gradle` สำหรับขอบเขตและชนิดต่าง ๆ คุณปฏิบัติ Groovy & Gradle: JVM Automation and Build Engineering ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Groovy & Gradle: JVM Automation and Build Engineering หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Groovy & Gradle: JVM Automation and Build Engineering บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน
บทเรียน “การประกาศการพึ่งพาของโครงการ” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Groovy & Gradle: JVM Automation and Build Engineering นี้ได้ไหม
ได้ บทเรียน Groovy & Gradle: JVM Automation and Build Engineering ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การประกาศการพึ่งพาของโครงการ
- การแก้ไขการพึ่งพาและแคช
- คลังเก็บแบบกำหนดเองและ BOM
- การแก้ข้อขัดแย้งของรุ่นและข้อจำกัดของส่วนพึ่งพา