第十二章. 使用Gradle 图形用户界面

Chapter 12. Using the Gradle Graphical User Interface

除了支持传统的命令行界面,Gradle也提供了一个图形用户界面(GUI)。这是一个独立的用户界面,可以通过加上 --gui 参数来启动。
In addition to supporting a traditional command line interface, Gradle offers a graphical user interface. This is a stand alone user interface that can be launched with the --gui option.

Example 12.1. Launching the GUI

gradle --gui

注意:此命令行窗口被将锁定,直到Gradle GUI被关闭。如果是在linux/unix系统下,则可以通过 ( gradle --gui& )让它作为后台任务运行。
Note that this command blocks until the Gradle GUI is closed. Under *nix it is probably preferable to run this as a background task ( gradle --gui& )

如果你在你的Gradle项目目录下运行Gradle GUI,你应该会看到一个任务树。
If you run this from your Gradle project working directory, you should see a tree of tasks.

Figure 12.1. GUI Task Tree

GUI Task Tree

最好是从Gradle项目目录运行此命令,这样对UI的设置就可以存储在你的项目目录中。当然,你也可以先运行它,然后通过在UI中的设置(Setup)选项卡,改变工作目录。
It is preferable to run this command from your Gradle project directory so that the settings of the UI will be stored in your project directory. However, you can run it then change the working directory via the Setup tab in the UI.

在Gradle的用户界面(UI)中,上面是4个选项卡,下面则是输出窗口。
The UI displays 4 tabs along the top and an output window along the bottom.

12.1. 任务树

12.1. Task Tree

任务树显示了所有项目和它们的任务的层次结构。双击一个任务可以执行它。
The Task Tree shows a hierarchical display of all projects and their tasks. Double clicking a task executes it.

这里还提供了一个过滤器,可以把比较少用的任务隐藏。你可以通过过滤器(Filter)按钮切换是否进行过滤。通过编辑过滤器,你可以对哪些任务和项目要显示进行配置。隐藏的任务显示为红色。注意:新创建的任务默认情况下是显示状态(而不是隐藏状态)
There is also a filter so that uncommon tasks can be hidden. You can toggle the filter via the Filter button. Editing the filter allows you to configure which tasks and projects are shown. Hidden tasks show up in red. Note: newly created tasks will show up by default (versus being hidden by default).

任务树的上下文菜单会提供以下选项:
The Task Tree context menu provides the following options:

  • 执行忽略依赖关系。这使得重新构建时不去依赖项目(与-a选项一样)
    Execute ignoring dependencies. This does not require dependent projects to be rebuilt (same as the -a option).

  • 将任务添加到收藏夹(见收藏夹(Favourites)选项卡)
    Add tasks to the favorites (see Favorites tab)

  • 隐藏选择的任务。这将会把它们添加到过滤器中。
    Hide the selected tasks. This adds them to the filter.

  • 编辑 build.gradle 文件。注意:该操作需要 Java 1.6 或更高的版本,并且要求在你的操作系统中关联 gradle 文件。
    Edit the build.gradle file. Note: this requires Java 1.6 or higher and requires that you have .gradle files associated in your OS.

12.2. 收藏夹

12.2. Favorites

收藏夹选项卡用来储存经常执行的命令。这些命令可以是复杂的命令(只要它们符合 Gradle 的语法),你可以给它们设置一个显示名称。它用于创建一个自定义的命令,来显示地跳过测试,文档,例子。你可以称之为“快速构建”。
The Favorites tab is place to store commonly-executed commands. These can be complex commands (anything that's legal to Gradle) and you can provide them with a display name. This is useful for creating, say, a custom build command that explicitly skips tests, documentation, and samples that you could call "fast build".

你可以根据自己的喜好,对收藏夹进行排序,甚至可以把它们导出到磁盘,并在其他地方导入。如果你在编辑它们的时候,选上“始终显示实时输出”,它只有在你选上“当发生错误时才显示输出”时有效。它会始终强制显示输出。
You can reorder favorites to your liking and even export them to disk so they can imported by others. If you edit them, you are given options to "Always Show Live Output." This only applies if you have 'Only Show Output When Errors Occur'. This override always forces the output to be shown.

12.3. 命令行

12.3. Command Line

命令行选项卡是直接执行单个的 Gradle 命令的地方。你只需要输入命令行中你经常在“Gradle”后面输入的命令即可。它也对要添加到收藏夹的命令提供了先去尝试的地方。
The Command Line tab is place to execute a single Gradle command directly. Just enter whatever you would normally enter after 'gradle' on the command line. This also provides a place to try out commands before adding them to favorites.

12.4. 设置

12.4. Setup

设置(Setup)选项卡允许你配置一些常规的设置。
The Setup tab allows configuration of some general settings.

Figure 12.2. GUI Setup

GUI Setup

  • 当前目录
    Current Directory

    定义了你的 Gradle 项目(通常是 build.gradle 所在的位置)的根目录。
    Defines the root directory of your Gradle project (typically where build.gradle is located).

  • 堆栈跟踪输出
    Stack Trace Output

    这决定了当出现错误时,有多少信息定到堆栈跟踪。注意:如果你在命令行或收藏夹选项卡上指定了堆栈跟踪级别,将会覆盖这里的设置。
    This determines how much information to write out stack traces when errors occur. Note: if you specify a stack trace level on either the Command Line or Favorites tab, it will override this stack trace level.

  • 只在出现错误时显示输出
    Only Show Output When Errors Occur

    启用此选项将在任务执行时隐藏任何输出,除非构建失败。
    Enabling this option hides any output when a task is executed unless the build fails.

  • 使用自定义的 Gradle 执行器 - 高级功能
    Use Custom Gradle Executor - Advanced feature

    这为你提供了启动 Gradle 命令行的替代方法。这是很有用的。如果你的项目需要在另一个批处理文件或 shell 脚本中做一些额外的配置(比如指定一个初始化脚本)。
    This provides you with an alternate way to launch Gradle commands. This is useful if your project requires some extra setup that is done inside another batch file or shell script (such as specifying an init script).