Groovy & Gradle: JVM Automation and Build Engineering · บทเรียน

โครงสร้างโครงการ Gradle

สำรวจโครงสร้างมาตรฐานของโครงการ Gradle และบทบาทของ `build.gradle` กับ `settings.gradle`

บทเรียน 2 จาก 412 ขั้นตอน

โครงสร้างโครงการ Gradle เป็นบทเรียน Groovy & Gradle: JVM Automation and Build Engineering ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Groovy & Gradle: JVM Automation and Build Engineering และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Groovy & Gradle: JVM Automation and Build Engineering มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Why Structure Matters

A well-organized project is key to successful development! Just like a recipe needs clear steps, a software project needs a clear structure.

Gradle projects follow a standard layout. This makes it easier for developers to understand, maintain, and collaborate on code.

The Gradle Root Project

Every Gradle build has a root project. This is usually the top-level directory where you run Gradle commands.

  • It's the starting point for Gradle's build process.
  • It can be a single project or contain multiple subprojects.

Think of it as the main folder for your entire application.

`settings.gradle`: Build Setup

The settings.gradle file is crucial for telling Gradle about your project structure.

  • It defines the project's name.
  • For multi-project builds, it lists all subprojects.

Even for a single project, it's good practice to have this file.

rootProject.name = 'my-first-gradle-app'

`build.gradle`: The Core Script

The build.gradle file is where you define all the rules for building your project. It contains:

  • Plugins to add specific functionalities (like Java compilation).
  • Project dependencies (libraries your code needs).
  • Custom tasks and configurations.

This is your project's main instruction manual for Gradle!

Key Project Files

When you initialize a Gradle project, you'll see a few important files:

  • settings.gradle: Configures the build structure.
  • build.gradle: Defines build logic (tasks, dependencies).
  • gradlew (and gradlew.bat): The Gradle Wrapper scripts.
  • .gradle/: Directory for Gradle's internal files (cache, daemon logs).

These files work together to make your build happen.

Common Directories

Gradle projects typically follow a convention for directories:

  • src/: Contains all your source code and resources.
  • build/: Where Gradle puts all generated files (compiled code, JARs, reports).
  • lib/ (optional): Sometimes used for unmanaged, local dependencies.

Sticking to this layout helps Gradle find things automatically.

The `src` Directory Explained

The src directory is further organized:

  • src/main/java: Your main application's Java source files.
  • src/main/resources: Non-code assets for your main app (e.g., config files).
  • src/test/java: Your test source files.
  • src/test/resources: Non-code assets for your tests.

This separation helps keep your code clean and manageable.

First Java `build.gradle`

Here's a simple build.gradle file for a Java project. It tells Gradle to apply the Java plugin and define basic project info.

plugins {
    id 'java' // Apply the Java plugin
}

group = 'com.coddykit'
version = '1.0-SNAPSHOT'

repositories {
    mavenCentral() // Where to find dependencies
}

// No dependencies for this basic example
dependencies {
}

Hello Gradle Java App

Let's create a simple Java program that fits into our src/main/java structure. Try running it!

// File: src/main/java/com/coddykit/Main.java
package com.coddykit;

public class Main {
  public static void main(String[] args) {
    System.out.println("Hello from Gradle Project!");
  }
}

Gradle's Build Flow

When you run a Gradle command (like gradle build), here's a simplified flow:

  • Gradle finds and executes settings.gradle.
  • It then processes the build.gradle file(s).
  • Plugins are applied, tasks are configured.
  • Finally, the requested tasks are executed (e.g., compile code, run tests).

This process transforms your source code into a usable output.

Quick Check: Core Files

Understanding the role of each file is fundamental to Gradle.

Recap: Gradle Structure

In this lesson, we explored the standard structure of a Gradle project:

  • The root project as the entry point.
  • settings.gradle for project configuration.
  • build.gradle for core build logic.
  • The conventional src/ and build/ directories.

This structure ensures clarity and efficiency in your build automation. Next, we'll dive deeper into tasks!

เริ่มต้นได้ฟรี

เรียนรู้ Groovy ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
12
บทเรียน
48

คำถามที่พบบ่อย

บทเรียน “โครงสร้างโครงการ Gradle” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “โครงสร้างโครงการ Gradle” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Groovy & Gradle: JVM Automation and Build Engineering ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Groovy & Gradle: JVM Automation and Build Engineering มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “โครงสร้างโครงการ Gradle”

สำรวจโครงสร้างมาตรฐานของโครงการ Gradle และบทบาทของ `build.gradle` กับ `settings.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 ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน

บทเรียน “โครงสร้างโครงการ Gradle” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Groovy & Gradle: JVM Automation and Build Engineering นี้ได้ไหม

ได้ บทเรียน Groovy & Gradle: JVM Automation and Build Engineering ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การติดตั้ง Gradle และ CLI
  2. โครงสร้างโครงการ Gradle
  3. งานและวงจรชีวิตการสร้าง
  4. การสร้างหลายโครงการและไฟล์การตั้งค่า
← กลับไปที่ Groovy & Gradle: JVM Automation and Build Engineering