第46章. Java 库发布插件

Chapter 46. The Java Library Distribution Plugin

Java 库发布插件目前还处于孵化中。请务必注意,在以后的 Gradle 版本中,相关的 DSL 和其他配置可能会有所改变。
The Java library distribution plugin is currently incubating. Please be aware that the DSL and other configuration may change in later Gradle versions.

Java 库分布插件为一个 Java library 添加了构建一个分发 ZIP 的支持。该分发包含了 library 和它的依赖的 JAR 文件。
The Java library distribution plugin adds support for building a distribution ZIP for a Java library. The distribution contains the JAR file for the library and its dependencies.

46.1. 用法

46.1. Usage

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

示例 46.1. 使用 Java 库分发插件 - Example 46.1. Using the java library distribution plugin

build.gradle

apply plugin: 'java-library-distribution'

若要定义 distribution 的名称,必须如下所示设置baseName属性:
To define the name for the distribution you have to set the baseName property as shown below:

示例 46.2. 配置分发的名称 - Example 46.2. Configure the distribution name

build.gradle

distributions {
    main{
        baseName = 'my-name'
    }
}

该插件还可以生成你的 library 的分发文件。distribution 将打包所有的运行时依赖。所有在src/main/dist中存储的文件都将被添加到 archive distribution 根目录中。你可以运行 gradle distZip 把 distrubution 打包成一个 ZIP 文件。
The plugin build a distribution for your library. The distribution will package up the runtime dependencies of the library All files stored in src/main/dist will be added to the root of the archive distribution. You can run gradle distZip to create a ZIP containing the distribution.

46.2. 任务

46.2. Tasks

Java 库分发插件向 project 对象添加以下任务。
The Java library distribution plugin adds the following tasks to the project.

表 46.1. Java 库分发插件 - 任务 - Table 46.1. Java library distribution plugin - tasks

任务名称
Task name
依赖于
Depends on
类型
Type
描述
Description
distZip jar Zip 创建包含运行时库的完整分发 ZIP 文件。
Creates a full distribution ZIP archive including runtime libraries.

46.3. 在 distribution 中包含其他资源

46.3. Including other resources in the distribution

所有在src/dist目录中的文件都会被复制。如果要在 distribution 中包括任何的静态文件,只需要把它们放在 src/dist 目录。
All of the files from the src/dist directory are copied. To include any static files in the distribution, simply arrange them in the src/dist directory, or add it to the content of the distribution.

示例 46.3. 在分发包中包含文件 - Example 46.3. Include files in the distribution

build.gradle

distributions {
    main {
        baseName = 'my-name'
        contents {
            from { 'src/dist' }
        }
    }
}