Gradle教程和指南 - 创建Gradle构建

原文地址:Creating New Gradle Builds



遵循本指南,你将创建一个Gradle项目,调用一些基本的Gradle命令,并了解Gradle如何管理项目。

需要什么

Shell命令将基于Unix的系统使用。在Windows中,每个命令都有类似的命令。

初始化项目

首先创建一个目录,即项目的所在目录

❯ mkdir basic-demo
❯ cd basic-demo

然后,我们可以使用Gradle init命令来生成一个简单的项目。我们将探索所有产生的项目文件,以确切知道发生了什么。

❯ gradle init
Starting a Gradle Daemon (subsequent builds will be faster)

BUILD SUCCESSFUL in 3s
2 actionable tasks: 2 executed

该命令应显示BUILD SUCCESSFUL并生成下面的一个空项目。如果没有,请确保Gradle安装正确,并且设置正确的名为JAVA_HOME的环境变量。

下面就是Gradle生成的文件目录:

image

  1. 项目配置脚本,用于配置当前项目中的任务
  2. Gradle Wrappe可执行JAR
  3. Gradle Wrapper配置属性
  4. 用于基于Unix系统的Gradle Wrapper脚本
  5. 用于基于Windows的Gradle Wrapper脚本
  6. 设置配置脚本,用于配置哪些项目参与构建

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

我们可以在这里结束指南,但是如果你想知道如何在这个项目中使用Gradle。可以这么做。

创建任务

Gradle提供了通过Groovy或Kotlin的DSL来创建和配置任务的的API。每个Project有一系列执行基本操作的Task

Gradle附带一个用于配置项目的任务库。例如,有个叫做Copy的核心类,它将文件从一个位置复制到另一个位置。Copy任务非常的有用(详情请参阅文档),但是,在这里,我们再一次只是简单的使用它。

执行以下步骤:

  1. 创建名为src的文件夹
  2. 在文件夹src中添加myfile.txt。内容是任意的(甚至可以为空),但为了方便起见,添加一行内容Hello, World!
  3. 在主构建文件build.gradle中定一个名为copyCopy类型任务。它将src目录复制到一个名为dest的新目录中。(你不必创建dest文件夹,任务将替你创建)
task copy(type: Copy, group: "Custom", description: "Copies sources to the dest directory") {
    from "src"
    into "dest"
}

在这里,group和description是可以任意设置的。你甚至可以忽略它们,但是,如果这么做,tasks报告中,也会忽略它们,过会我们会用到它们。

现在执行新创建的copy任务:

❯ ./gradlew copy
:copy

BUILD SUCCESSFUL in 0s
1 actionable task: 1 executed

通过检查在dest文件夹中有名为myfile.txt的文件,并且里面的内容与src中的myfile.txt内容一致来检查该任务是否按照预期执行。

应用插件

Gradle包含一系列插件, the Gradle plugin portal中提供了非常多的插件。这个发行版中包含的一个名为base的插件。与核心类Zip一起使用,可以使用配置的名称和位置创建项目的zip压缩文件。

使用plugins脚本将base插件添加到build.gradle中。确保在文件顶部添加plugins {}代码块。

plugins {
    id "base"
}

... rest of the build file ...

现在添加一个创建src文件夹的zip压缩文件的任务。

task zip(type: Zip, group: "Archive", description: "Archives sources in a zip file") {
    from "src"
}

base插件与设置一起完成任务,在build/distributions文件夹下创建名为basic-demo-1.0.zip的压缩文件。

在这种情况下,执行任务zip并且查看生成的压缩文件是您所期望的。

❯ ./gradlew zip
:zip

BUILD SUCCESSFUL in 0s
1 actionable task: 1 executed

探索和调试构建

让我们来看看在新项目中Gradle还能做些什么。还提供了对命令行界面的完整引用。

查看可用的tasks

tasks命令列出你可调用的Gradle任务,包括base插件添加的任务以及刚刚添加的自定义任务。

❯ ./gradlew tasks

> Task :tasks

------------------------------------------------------------
All tasks runnable from root project
------------------------------------------------------------

Archive tasks
-------------
zip - Archives sources in a zip file

Build tasks
-----------
assemble - Assembles the outputs of this project.
build - Assembles and tests this project.
clean - Deletes the build directory.

Build Setup tasks
-----------------
init - Initializes a new Gradle build.
wrapper - Generates Gradle wrapper files.

Custom tasks
------------
copy - Simply copies sources to a the build directory

Help tasks
----------
buildEnvironment - Displays all buildscript dependencies declared in root project 'basic-demo'.
components - Displays the components produced by root project 'basic-demo'. [incubating]
dependencies - Displays all dependencies declared in root project 'basic-demo'.
dependencyInsight - Displays the insight into a specific dependency in root project 'basic-demo'.
dependentComponents - Displays the dependent components of components in root project 'basic-demo'. [incubating]
help - Displays a help message.
model - Displays the configuration model of root project 'basic-demo'. [incubating]
projects - Displays the sub-projects of root project 'basic-demo'.
properties - Displays the properties of root project 'basic-demo'.
tasks - Displays the tasks runnable from root project 'basic-demo'.

Verification tasks
------------------
check - Runs all checks.

Rules
-----
Pattern: clean<TaskName>: Cleans the output files of a task.
Pattern: build<ConfigurationName>: Assembles the artifacts of a configuration.
Pattern: upload<ConfigurationName>: Assembles and uploads the artifacts belonging to a configuration.

To see all tasks and more detail, run gradlew tasks --all

To see more detail about a task, run gradlew help --task <task>

BUILD SUCCESSFUL in 0s
1 actionable task: 1 executed

分析和调试你的构建

Gradle还为您的构建提供了一个丰富的,基于Web的视图,称为构建审视。

image

通过使用--scan命令选项或通过显示声明将构建审视插件应用到项目中,您可以免费在链接scans.gradle.com上创建构建审视。构建审视发布到scans.gradle.com 并将这些数据上传到Gradle的服务器。要将数据保存在您自己的服务器上,请查看Gradle Enterprise.

在执行任务时,通过添加 --scan命令选项生成构建审视。

❯ ./gradlew zip --scan

BUILD SUCCESSFUL in 0s
1 actionable task: 1 up-to-date

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]
Gradle Cloud Services license agreement accepted.

Publishing build scan...
https://gradle.com/s/repnge6srr5qs

如果您浏览构建审视,则应该能够轻松找出执行哪些任务以及执行多长时间,应用了哪些插件等等。下次您在StackOverflow上调试某些内容时,请考虑共享构建审视。

Build Scan Plugin用户手册中详细了解如何配置和使用构建审视。

查看可用的properties

properties命令可以查看项目的构建属性。

❯ ./gradlew properties

数据量是很大的。这里只是一些可用的属性:

> Task :properties

------------------------------------------------------------
Root project
------------------------------------------------------------

buildDir: /Users/.../basic-demo/build
buildFile: /Users/.../basic-demo/build.gradle
description: null
group:
name: basic-demo
projectDir: /Users/.../basic-demo
version: unspecified

BUILD SUCCESSFUL

项目的name属性默认与该文件夹名称一致。您也可以指定group和version属性,但是,当前获得的是它们的默认值,就像description属性。

buildFile属性是build.gradle的全限定路径名, 位于buildDir文件夹中 - 默认情况下,是projectDir的build子目录,该目录包含build.gradle文件。

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

description = "A trivial Gradle build"
version = "1.0"

下一步

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

您可能希望为特定平台创建Library或应用程序,因此以下是一些指南,可以帮助您更多地了解如何在所选平台中创建版本:

  • 构建Android应用程序
  • 构建C ++可执行文件
  • 构建Groovy库
  • 构建Java库
  • 构建Kotlin JVM库
  • 构建Scala库

你也可以在Github clone Gradle构建示例

猜你喜欢

转载自blog.csdn.net/IO_Field/article/details/79925898
今日推荐