Maven vs Gradle: Build Tools สำหรับ Java

#java13 เม.ย. 2569

Build Tools คืออะไร

Build tools จัดการ dependencies, compile, test และ package Java projects Maven และ Gradle เป็นสองตัวเลือกหลัก

Maven

<!-- pom.xml -->
<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>my-app</artifactId>
    <version>1.0.0</version>
    <packaging>jar</packaging>

    <properties>
        <java.version>21</java.version>
        <spring-boot.version>3.2.0</spring-boot.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>${spring-boot.version}</version>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

Maven Commands

mvn clean          # ลบ target/
mvn compile        # compile
mvn test           # รัน tests
mvn package        # สร้าง JAR
mvn install        # ติดตั้งใน local repo
mvn spring-boot:run  # รัน Spring Boot

Gradle

// build.gradle.kts
plugins {
    id("org.springframework.boot") version "3.2.0"
    id("io.spring.dependency-management") version "1.1.4"
    kotlin("jvm") version "1.9.20"
}

group = "com.example"
version = "1.0.0"

dependencies {
    implementation("org.springframework.boot:spring-boot-starter-web")
    implementation("org.springframework.boot:spring-boot-starter-data-jpa")
    runtimeOnly("com.h2database:h2")
    testImplementation("org.springframework.boot:spring-boot-starter-test")
}

tasks.withType<Test> {
    useJUnitPlatform()
}

Gradle Commands

./gradlew clean        # ลบ build/
./gradlew build        # build ทั้งหมด
./gradlew test         # รัน tests
./gradlew bootRun      # รัน Spring Boot
./gradlew bootJar      # สร้าง executable JAR

เปรียบเทียบ

หัวข้อ Maven Gradle
Config format XML Groovy/Kotlin DSL
Build speed ช้ากว่า เร็วกว่า (incremental)
Learning curve ง่ายกว่า ยากกว่าเล็กน้อย
Flexibility น้อยกว่า มากกว่า
Enterprise นิยมมาก นิยมมากขึ้น

Dependency Scopes

<!-- Maven -->
<dependency>
    <scope>compile</scope>   <!-- default -->
    <scope>test</scope>      <!-- test only -->
    <scope>provided</scope>  <!-- provided by container -->
    <scope>runtime</scope>   <!-- runtime only -->
</dependency>
// Gradle
implementation("...")  // compile + runtime
testImplementation("...")  // test only
compileOnly("...")  // compile only
runtimeOnly("...")  // runtime only

สรุป

ทั้ง Maven และ Gradle ใช้งานได้ดี Maven เหมาะกับ enterprise ที่ต้องการ standardization ส่วน Gradle เหมาะกับโปรเจ็คที่ต้องการ flexibility และ build speed ครับ