Skip to content

Hello World

通过创建第一个 Gradle 项目,快速体验 Gradle 的基本用法。

方式一:使用 gradle init 初始化(推荐)

gradle init 命令提供交互式向导,自动生成项目骨架。

bash
# 创建项目目录
mkdir hello-gradle
cd hello-gradle

# 运行初始化向导
gradle init

向导交互过程:

Select type of project to generate:
  1: basic
  2: application        ← 选择 2(Java 应用程序)
  3: library
  4: Gradle plugin
Enter selection (default: basic) [1..4] 2

Select implementation language:
  1: C++
  2: Groovy
  3: Java             ← 选择 3
  4: Kotlin
  5: Scala
  6: Swift
Enter selection (default: Java) [1..6] 3

Select build script DSL:
  1: Groovy
  2: Kotlin           ← 选择 2(推荐 Kotlin DSL)
Enter selection (default: Kotlin) [1..2] 2

Project name (default: hello-gradle):
Source package (default: hello.gradle): com.example

BUILD SUCCESSFUL in 3s

生成的项目结构

hello-gradle/
├── app/
│   ├── build.gradle.kts          ← 应用模块构建脚本
│   └── src/
│       ├── main/
│       │   ├── java/com/example/
│       │   │   └── App.java      ← 主类
│       │   └── resources/
│       └── test/
│           ├── java/com/example/
│           │   └── AppTest.java  ← 测试类
│           └── resources/
├── gradle/
│   └── wrapper/
│       ├── gradle-wrapper.jar
│       └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
└── settings.gradle.kts           ← 项目设置文件

查看生成的文件

settings.gradle.kts

kotlin
rootProject.name = "hello-gradle"
include("app")

app/build.gradle.kts

kotlin
plugins {
    application
}

repositories {
    mavenCentral()
}

dependencies {
    testImplementation(libs.junit.jupiter)
    testRuntimeOnly("org.junit.platform:junit-platform-launcher")
    implementation(libs.guava)
}

application {
    mainClass = "com.example.App"
}

tasks.named<Test>("test") {
    useJUnitPlatform()
}

App.java

java
package com.example;

public class App {
    public String getGreeting() {
        return "Hello World!";
    }

    public static void main(String[] args) {
        System.out.println(new App().getGreeting());
    }
}

方式二:手动创建最简单的项目

适合理解 Gradle 最基础的结构。

1. 创建目录和文件

bash
mkdir hello-gradle-manual
cd hello-gradle-manual

# 创建构建脚本
touch settings.gradle.kts
touch build.gradle.kts

settings.gradle.kts

kotlin
rootProject.name = "hello-gradle-manual"

build.gradle.kts

kotlin
tasks.register("hello") {
    group = "custom"
    description = "打印 Hello World"
    doLast {
        println("Hello, Gradle!")
    }
}

2. 运行任务

bash
gradle hello

# 输出:
# > Task :hello
# Hello, Gradle!
#
# BUILD SUCCESSFUL in 1s
# 1 actionable task: 1 executed

运行常用命令

hello-gradle 目录下(使用 Wrapper):

bash
# 构建项目(编译 + 测试 + 打包)
./gradlew build

# 运行应用程序
./gradlew run

# 仅运行测试
./gradlew test

# 清理构建输出
./gradlew clean

# 查看所有可用任务
./gradlew tasks

# 查看依赖树
./gradlew dependencies

# 构建并查看详细信息
./gradlew build --info

build 输出示例

$ ./gradlew build

> Task :app:compileJava
> Task :app:processResources NO-SOURCE
> Task :app:classes
> Task :app:jar
> Task :app:startScripts
> Task :app:distTar
> Task :app:distZip
> Task :app:assemble
> Task :app:compileTestJava
> Task :app:processTestResources NO-SOURCE
> Task :app:testClasses
> Task :app:test

BUILD SUCCESSFUL in 3s
8 actionable tasks: 8 executed

run 输出示例

$ ./gradlew run

> Task :app:run
Hello World!

BUILD SUCCESSFUL in 1s
2 actionable tasks: 1 executed, 1 up-to-date

查看构建产物

构建成功后,产物位于 app/build/ 目录:

app/build/
├── classes/           ← 编译后的 .class 文件
├── distributions/     ← 发行包(ZIP/TAR)
├── libs/
│   └── app.jar        ← JAR 文件
├── reports/
│   └── tests/         ← 测试报告(HTML)
└── tmp/

打开测试报告:

bash
# Linux/macOS
open app/build/reports/tests/test/index.html

# Windows
start app\build\reports\tests\test\index.html

增量构建体验

bash
# 第一次构建
$ ./gradlew build
8 actionable tasks: 8 executed

# 不做任何修改,再次构建
$ ./gradlew build
8 actionable tasks: 8 up-to-date 全部跳过,0 秒!

# 修改 App.java 后
$ ./gradlew build
8 actionable tasks: 6 executed, 2 up-to-date 只重新编译相关部分

下一步