第四十四章. 分发插件

Chapter 44. The Distribution Plugin

分发插件目前是一个试验性插件。注意,在以后的 Gradle 版本中,其 DSL 和其他配置可能会有变化。
The distribution plugin is currently incubating. Please be aware that the DSL and other configuration may change in later Gradle versions.

分发插件有助于构建用作项目分发的档案。分发档案通常包含可执行程序和其他支持文件,比如文档。
The distribution plugin facilitates building archives that serve as distributions of the project. Distribution archives typically contain then executable application and other supporting files, such as documentation.

44.1. 用法

44.1. Usage

要使用分发插件,请在构建脚本中包含:
To use the distribution plugin, include in your build script:

示例 44.1. 使用分发插件 - Example 44.1. Using the distribution plugin

build.gradle

apply plugin: 'distribution'

本插件向项目添加了一个名为“distributions”的DistributionContainer类型的扩展。它还在分发容器扩展中创建了一个名为“main”的单个distribution。如果您的构建只生成一个分发,则你只需要配置这一个distribution(或使用默认值)。
The plugin adds an extension named "distributions" of type DistributionContainer to the project. It also creates a single distribution in the distributions container extension named "main". If your build only produces one distribution you only need to configure this distribution (or use the defaults).

你可以运行“gradle distZip”把 main distribution 打包为 ZIP,或者是运行“gradle distTar”来创建一个 GZip 压缩的 TAR 文件。这些文件将会创建在“$buildDir/distributions/$project.name-$project.version.«ext»”。
You can run "gradle distZip" to package the main distribution as a ZIP, or "gradle distTar" to create a GZip compressed TAR file. The files will be created at "$buildDir/distributions/$project.name-$project.version.«ext»".

你可以运行“gradle installDist”将未经压缩的分发内容组装到“$buildDir/install/main”。
You can run "gradle installDist" to assembles the distribution content, uncompressed, into "$buildDir/install/main".

44.2. 任务

44.2. Tasks

分发插件将以下以下任务添加到项目中:
The Distribution plugin adds the following tasks to the project:

表 44.1. 分发插件——任务 - Table 44.1. Distribution plugin - tasks

任务名称
Task name
依赖于
Depends on
类型
Type
描述
Description
distZip - Zip 创建分发内容的 ZIP 存档
Creates a ZIP archive of the distribution contents
distTar - Tar 创建分发内容的 ZIP 存档
Creates a ZIP archive of the distribution contents
installDist - Sync 组装分发内容,并将其安装在当前计算机上
Assembles the distribution content and installs it on the current machine

对于你向项目中添加的每一个额外的分发集,分发插件都会添加以下任务:
For each extra distribution set you add to the project, the distribution plugin adds the following tasks:

表 44.2. 多分发——任务 - Table 44.2. Multiple distributions - tasks

任务名称
Task name
依赖于
Depends on
类型
Type
描述
Description
${distribution.name}DistZip - Zip 创建分发内容的 ZIP 存档
Creates a ZIP archive of the distribution contents
${distribution.name}DistTar - Tar 创建分发内容的 TAR 文件
Creates a TAR archive of the distribution contents
install${distribution.name.capitalize()}Dist - Sync 组装分发内容,并将其安装在当前计算机上
Assembles the distribution content and installs it on the current machine

示例 44.2. 添加额外的分发 - Example 44.2. Adding extra distributions

build.gradle

apply plugin: 'distribution'

version = '1.2'
distributions {
    custom {}
}

这将向项目添加以下任务:
This will add following tasks to the project:

  • customDistZip
  • customDistTar
  • installCustomDist

鉴于项目的名称是“myproject”,版本为“1.2”,运行“gradle customDistZip”将会产生一个名为“myproject-custom-1.2.zip”的 ZIP 文件。
Given that the project name is "myproject" and version "1.2", running "gradle customDistZip" will produce a ZIP file named "myproject-custom-1.2.zip".

运行“gradle installCustomDist”将会把分发内容安装到“$buildDir/install/custom”。
Running "gradle installCustomDist" will install the distribution contents into "$buildDir/install/custom".

44.3. 分发内容

44.3. Distribution contents

src/$distribution.name/dist”目录中的所有文件都会被自动包含在分发中。你可以通过配置作为容器中的一部分的Distribution对象来添加其他文件。
All of the files in the "src/$distribution.name/dist" directory will automatically be included in the distribution. You can add additional files by configuring the Distribution object that is part of the container.

示例 44.3. 配置 main distribution - Example 44.3. Configuring the main distribution

build.gradle

apply plugin: 'distribution'

distributions {
    main {
        baseName = 'someName'
        contents {
            from { 'src/readme' }
        }
    }
}

在上面的示例中,“src/readme”目录的内容将被包含进 distribution(与默认添加的“src/dist/main”目录中的文件一起)。
In the example above, the content of the "src/readme" directory will be included in the distribution (along with the files in the "src/dist/main" directory which are added by default).

baseName”属性也被修改了。这将导致分发的存档以不同的名字创建。
The "baseName" property has also been changed. This will cause the distribution archives to be created with a different name.