创建新的Gradle项目

按照本文档,您将创建一个简单的Gradle项目,调用一些基本的Gradle命令,并了解Gradle如何管理这个项目

你需要什么

  • 大约11分钟
  • 终端应用程序(cmd或bash)
  • Java运行时环境(JRE)或Java Development Kit(JDK),版本1.8或更高版本(仅运行Gradle时需要)
  • Gradle分发版,版本4.10-rc-2或更高版本

Shell命令将显示基于Unix的系统。 Windows对每个命令都有类似的命令

初始化一个项目

首先,让我们为新项目创建一个新的目录。

  1. mkdir basic-demo
  2. cd basic-demo

现在我们可以使用Gradle的 gradle init 命令来生成一个简单的项目。然后查看生成的所有内容,以便知道发生了什么。

  1. gradle init
  2. Starting a Gradle Daemon (subsequent builds will be faster)
  3. BUILD SUCCESSFUL in 3s
  4. 2 actionable tasks: 2 executed

如果你想使用 Kotlin DSL 脚本,可以使用 gradle init --dsl kotlin 命令。更多信息可以阅读 详细文档.

该命令应显示 “BUILD SUCCESSFUL” 并生成以下 “空” 项目。 如果没有,请确保正确 安装Gradle,并确保正确设置了 JAVA_HOME 环境变量。

这就是Gradle为您生成的内容。

Groovy

  1. ├── build.gradle
  2. ├── gradle
  3. └── wrapper
  4. ├── gradle-wrapper.jar
  5. └── gradle-wrapper.properties
  6. ├── gradlew
  7. ├── gradlew.bat
  8. └── settings.gradle

Kotlin

  1. ├── build.gradle.kts
  2. ├── gradle
  3. └── wrapper
  4. ├── gradle-wrapper.jar
  5. └── gradle-wrapper.properties
  6. ├── gradlew
  7. ├── gradlew.bat
  8. └── settings.gradle.kts

build.gradle 用于配置当前项目的Gradle构建脚本
gradle-wrapper.jar Gradle Wrapper可执行JAR
gradle-wrapper.properties Gradle Wrapper配置属性
gradlew 基于Unix的系统的Gradle Wrapper脚本
gradlew.bat 适用于Windows的Gradle Wrapper脚本
settings.gradle 用于配置Gradle构建的Gradle设置脚本

gradle init 可以生成各种不同类型的项目,甚至知道如何将简单的pom.xml文件转换为Gradle。

搞定!我们的教程可以在这里结束,但是您可能想知道如何在这个项目中使用Gradle。那就让我们继续吧。

创建一个任务

Gradle提供了用于通过基于Groovy或Kotlin的DSL脚本创建和配置任务的API。 Project (项目)包括一组任务,每个任务执行一些基本操作。

Gradle提供了一个任务库,您可以在自己的项目中配置它。例如,有一个名为Copy的核心任务类型,它将文件从一个位置复制到另一个位置。复制任务非常有用(参阅文档了解详细信息),但是在这里,让我们再次保持简单。执行以下步骤:

  1. 创建一个名为 src 的目录。
  2. src 目录中添加一个名为 myfile.txt 的文件。内容可以是任意的(甚至可以是空的),但是为了方便起见,可以添加一行Hello, World!
  3. 在你的 build.gradle 文件中定义一个类型为 Copy (注意首字母大写)名称为 copy 的任务来复制 src 目录到一个名为 dest 的目录(你不必去手动创建 dest 目录,因为任务会为你去创建)。

build.gradle width Groovy

  1. task copy(type: Copy, group: "Custom", description: "Copies sources to the dest directory") {
  2. from "src"
  3. into "dest"
  4. }

build.gradle.kts width Kotlin

  1. tasks.create<Copy>("copy") {
  2. description = "Copies sources to the dest directory"
  3. group = "Custom"
  4. from("src")
  5. into("dest")
  6. }

在这里,groupdescription 您可以随便写,甚至可以忽略它们,但是这样做也会在稍后使用的tasks报告中忽略它们。

现在来执行您新建 copy 任务:

  1. ./gradlew copy
  2. > Task :copy
  3. BUILD SUCCESSFUL in 0s
  4. 1 actionable task: 1 executed

我们可以检查一下 dest 目录中是否存在名为 myfile.txt 的文件并且它的内容和 src 目录中的文件一样,来验证 copy 任务是否成功执行。

应用插件

Gradle包含一系列的插件,在 Gradle插件市场 上可以找到更多的插件。该发行版附带的插件之一是 base plugin(基本插件)。这个基础插件附带了一个名为 Zip 的任务类型,您可以通过配置名称和路径来为你的项目打包成一个 zip 压缩文件。

添加 base plugin(基本插件)到构建脚本可以使用名为 plugins 语法块。请确保 plugins{} 代码块添加在文件的前面。

build.gradle width Groovy

  1. plugins {
  2. id "base"
  3. }
  4. ... rest of the build file ...

build.gradle.kts width Kotlin

  1. plugins {
  2. id("base")
  3. }
  4. ... rest of the build file ...

现在可以添加把 src 目录打包成 zip 文件的任务了。

build.gradle width Groovy

  1. task zip(type: Zip, group: "Archive", description: "Archives sources in a zip file") {
  2. from "src"
  3. setArchiveName "basic-demo-1.0.zip"
  4. }

build.gradle.kts width Kotlin

  1. tasks.create<Zip>("zip") {
  2. description = "Archives sources in a zip file"
  3. group = "Archive"
  4. from("src")
  5. setArchiveName("basic-demo-1.0.zip")
  6. }

base plugin(基本插件)根据上面的设置在 build/distributions 目录创建一个名为 basic-demo-1.0.zip 的压缩文件。

现在,只需运行新的 zip 任务,并查看生成的zip文件是否符合您的期望。

  1. ./gradlew zip
  2. > Task :zip
  3. BUILD SUCCESSFUL in 0s
  4. 1 actionable task: 1 executed

探索和调试项目

让我们看看在我们的新项目中还可以用Gradle做些什么。我们可以在 命令行界面 中完成。

发现可用的任务

tasks 命令可以列出了可用的Gradle任务,包括 base plugin(基本插件)添加的任务,以及您刚刚添加的自定义任务。

  1. ./gradlew tasks
  2. > Task :tasks
  3. ------------------------------------------------------------
  4. All tasks runnable from root project
  5. ------------------------------------------------------------
  6. Archive tasks
  7. -------------
  8. zip - Archives sources in a zip file
  9. Build tasks
  10. -----------
  11. assemble - Assembles the outputs of this project.
  12. build - Assembles and tests this project.
  13. clean - Deletes the build directory.
  14. Build Setup tasks
  15. -----------------
  16. init - Initializes a new Gradle build.
  17. wrapper - Generates Gradle wrapper files.
  18. Custom tasks
  19. ------------
  20. copy - Simply copies sources to a the build directory
  21. Help tasks
  22. ----------
  23. buildEnvironment - Displays all buildscript dependencies declared in root project 'basic-demo'.
  24. components - Displays the components produced by root project 'basic-demo'. [incubating]
  25. dependencies - Displays all dependencies declared in root project 'basic-demo'.
  26. dependencyInsight - Displays the insight into a specific dependency in root project 'basic-demo'.
  27. dependentComponents - Displays the dependent components of components in root project 'basic-demo'. [incubating]
  28. help - Displays a help message.
  29. model - Displays the configuration model of root project 'basic-demo'. [incubating]
  30. projects - Displays the sub-projects of root project 'basic-demo'.
  31. properties - Displays the properties of root project 'basic-demo'.
  32. tasks - Displays the tasks runnable from root project 'basic-demo'.
  33. Verification tasks
  34. ------------------
  35. check - Runs all checks.
  36. Rules
  37. -----
  38. Pattern: clean<TaskName>: Cleans the output files of a task.
  39. Pattern: build<ConfigurationName>: Assembles the artifacts of a configuration.
  40. Pattern: upload<ConfigurationName>: Assembles and uploads the artifacts belonging to a configuration.
  41. To see all tasks and more detail, run gradlew tasks --all
  42. To see more detail about a task, run gradlew help --task <task>
  43. BUILD SUCCESSFUL in 0s
  44. 1 actionable task: 1 executed

分析和调试项目

Gradle还提供了一个丰富的、基于web的构建视图,叫做 build scan(构建扫描)。

创建新的Gradle项目 - 图1

通过使用 --scan 选项或显式地将 build scan 插件应用到项目中,您可以在 scans.gradle.com 中免费创建一个 build scan。将 构建扫描 发布到 scans.gradle.com 会将此数据传输到Gradle的服务器。要将数据保存在自己的服务器上,请查看 Gradle 企业版

尝试在执行任务时添加 --scan 来创建构建扫描。

  1. ./gradlew zip --scan
  2. BUILD SUCCESSFUL in 0s
  3. 1 actionable task: 1 up-to-date
  4. Publishing a build scan to scans.gradle.com requires accepting the Terms of Service defined at https://scans.gradle.com/terms-of-service. Do you accept these terms? [yes, no]
  5. Gradle Cloud Services license agreement accepted.
  6. Publishing build scan...
  7. https://gradle.com/s/repnge6srr5qs

如果您浏览您的 build scan ,您应该能够很容易地找到在哪里执行的任务,它们花费了多少时间,应用了哪些插件,等等。下次在StackOverflow上调试的某些东西时,可以考虑附上一个 build scan 信息。

您可以在 build scan 插件用户手册 中学习怎样配置和使用 build scan

发现可用的属性

properties 命令告诉您项目的属性。

  1. ./gradlew properties

输出结果很全,以下是一些可用的属性:

  1. > Task :properties
  2. ------------------------------------------------------------
  3. Root project
  4. ------------------------------------------------------------
  5. buildDir: /Users/.../basic-demo/build
  6. buildFile: /Users/.../basic-demo/build.gradle.kts
  7. description: null
  8. group:
  9. name: basic-demo
  10. projectDir: /Users/.../basic-demo
  11. version: unspecified
  12. BUILD SUCCESSFUL

默认情况下,项目的 name 与文件夹的名称一样。 您还可以指定 groupversion 属性,但目前它们采用默认值,如上描述的一样。

buildFile 属性是 build.gradle 脚本的路径名,默认情况下位于 projectDir 中。

您可以更改许多属性。 例如,您可以尝试将以下行添加到 build.gradle 脚本件中,然后重新执行gradle属性。

  1. version = "1.0"
  2. description = "A trivial Gradle build"

下一步

恭喜你!您已经学会了创建一个新的Gradle项目,并学习了如何检查Gradle构建!

您可能想要为特定的平台创建一个库或应用程序,因此这里有一些 使用指南,将教你如何在选择的平台中创建构建:

您也可以在 Github 上获取实例项目。