Gradle¶
build.gradle¶
Overview
build.gradle
是 Gradle 项目的核心配置文件,定义了项目的构建逻辑和依赖关系。
build.gradle
is the core configuration file of a Gradle project, defining the build logic and dependencies.
Main Components
-
插件(Plugins)
- 定义:
plugins { id 'java' }
- 作用:添加预定义的任务和功能
- Definition:
plugins { id 'java' }
- Purpose: Adds predefined tasks and functionalities
- 定义:
-
项目属性(Project Properties)
- 定义:
group = 'com.example'
- 作用:设置项目的基本信息
- Definition:
group = 'com.example'
- Purpose: Sets basic information about the project
- 定义:
-
仓库(Repositories)
- 定义:
repositories { mavenCentral() }
- 作用:指定依赖下载的来源
- Definition:
repositories { mavenCentral() }
- Purpose: Specifies the source for dependency downloads
- 定义:
-
依赖(Dependencies)
- 定义:
dependencies { implementation 'org.springframework:spring-core:5.3.10' }
- 作用:声明项目需要的外部库
- Definition:
dependencies { implementation 'org.springframework:spring-core:5.3.10' }
- Purpose: Declares the external libraries required by the project
- 定义:
-
任务(Tasks)
- 定义:
task hello { doLast { println 'Hello, Gradle!' } }
- 作用:定义自定义构建任务
- Definition:
task hello { doLast { println 'Hello, Gradle!' } }
- Purpose: Defines custom build tasks
- 定义:
-
源集(Source Sets)
- 定义:
sourceSets { main { java { srcDirs = ['src/main/java'] } } }
- 作用:配置源代码和资源文件的位置
- Definition:
sourceSets { main { java { srcDirs = ['src/main/java'] } } }
- Purpose: Configures the location of source code and resource files
- 定义:
-
构建配置(Build Configurations)
- 定义:
compileJava { options.encoding = 'UTF-8' }
- 作用:配置编译和其他构建过程的选项
- Definition:
compileJava { options.encoding = 'UTF-8' }
- Purpose: Configures options for compiling and other build processes
- 定义:
Most Used Command¶
Command | Chinese Explanation | English Explanation |
---|---|---|
gradle build |
构建项目 | Build the project |
gradle clean |
清理构建目录 | Clean the build directory |
gradle test |
运行测试 | Run tests |
gradle run |
运行应用程序 | Run the application |
gradle tasks |
列出可用任务 | List available tasks |
gradle dependencies |
显示项目依赖 | Show project dependencies |
gradle wrapper |
生成 Gradle Wrapper 文件 | Generate Gradle Wrapper files |
./gradlew <task> |
使用 Gradle Wrapper 执行任务 | Execute a task using Gradle Wrapper |
gradle init |
初始化新的 Gradle 项目 | Initialize a new Gradle project |
gradle assemble |
编译并打包项目,但不运行测试 | Compile and package the project without running tests |
gradle check |
运行所有检查 | Run all checks |
gradle javadoc |
生成 Javadoc 文档 | Generate Javadoc documentation |
gradle --console plain |
以纯文本模式运行 Gradle | Run Gradle in plain console mode |
gradle <task> --info |
以信息模式运行任务,提供更多输出 | Run a task in info mode, providing more output |
gradle <task> --debug |
以调试模式运行任务 | Run a task in debug mode |