第四十五章. 应用程序插件

Chapter 45. The Application Plugin

Gradle 应用程序插件扩展了语言插件的一些常见应用程序相关的任务。它允许为 jvm 运行和捆绑应用程序。
The Gradle application plugin extends the language plugins with common application related tasks. It allows running and bundling applications for the jvm.

45.1. 用法

45.1. Usage

要使用这个应用程序插件,请在构建脚本中包含以下语句:
To use the application plugin, include in your build script:

示例 45.1. 使用应用程序插件 - Example 45.1. Using the application plugin

build.gradle

apply plugin:'application'

若要为应用程序定义 main-class,你必须如下所示设置mainClassName属性
To define the main-class for the application you have to set the mainClassName property as shown below

示例 45.2. 配置应用程序的主类 - Example 45.2. Configure the application main class

build.gradle

mainClassName = "org.gradle.sample.Main"

然后,你可以通过执行 gradle run 来运行这个应用程序。Gradle 将会处理好构建的应用程序的类,以及它们的运行时依赖,还有使用正确的类路径启动应用程序。你可以通过gradle run --debug-jvm 在调试模式下启动该应用程序(见JavaExec.setDebug())。
Then, you can run the application by running gradle run. Gradle will take care of building the application classes, along with their runtime dependencies, and starting the application with the correct classpath. You can launch the application in debug mode with gradle run --debug-jvm (see JavaExec.setDebug()).

该插件还可以生成你的应用程序的分发文件。分发的内容将会把这个程序的运行时依赖和一些操作系统特定的启动脚本打包在一起。所有存储在 src/dist 的文件都将添加到 distribution 的根目录中您可以运行gradle installApp,在 build/install/projectName 中创建一张应用程序的图像。你可以运行gradle distZip 把 distrubution 打包成一个 ZIP 文件。
The plugin can also build a distribution for your application. The distribution will package up the runtime dependencies of the application along with some OS specific start scripts. All files stored in src/dist will be added to the root of the distribution. You can run gradle installApp to create an image of the application in build/install/projectName. You can run gradle distZip to create a ZIP containing the distribution.

如果你的 Java 应用程序需要一组特定的 JVM 设置或系统属性,你可以配置applicationDefaultJvmArgs属性。这些 JVM 参数会被应用于run 任务,以及生成的 distrubution 的启动脚本。
If your Java application requires a specific set of JVM settings or system properties, you can configure the applicationDefaultJvmArgs property. These JVM arguments are applied to the run task and also considered in the generated start scripts of your distribution.

示例 45.3. 配置默认 JVM 设置 - Example 45.3. Configure default JVM settings

build.gradle

applicationDefaultJvmArgs = ["-Dgreeting.language=en"]

45.2. 任务

45.2. Tasks

应用程序插件向 project 中添加了以下任务。
The Application plugin adds the following tasks to the project.

表 45.1. 应用程序插件 - 任务 - Table 45.1. Application plugin - tasks

任务名称
Task name
依赖于
Depends on
类型
Type
描述
Description
run classes JavaExec 启动应用程序。
Starts the application.
startScripts jar CreateStartScripts 创建操作系统特定的脚本来把该项目作为一个 JVM 应用程序运行。
Creates OS specific scripts to run the project as a JVM application.
installApp jar, startScripts Sync 将应用程序安装到指定的目录。
Installs the application into a specified directory.
distZip jar, startScripts Zip 创建包含了运行时库和操作系统特定的脚本的完整分发 ZIP 文件。
Creates a full distribution ZIP archive including runtime libraries and OS specific scripts.
distTar jar, startScripts Tar 创建包含了运行时库和操作系统特定的脚本的完整分发 TAR 文件。
Creates a full distribution TAR archive including runtime libraries and OS specific scripts.

45.3. 约定属性

45.3. Convention properties

该应用程序插件将一些属性添加到 project 中,以用于配置其行为。请参见 Project
The application plugin adds some properties to the project, which you can use to configure its behaviour. See Project.

45.4. 在 distribution 中包含其他资源

45.4. Including other resources in the distribution

applicationDistribution是由插件添加的公约属性之一,它是一个CopySpec。这个描述在 installAppdistZip 任务中会用到,作为这个distribution 都包含了什么内容的描述。以上在 distribution 中将启动脚本复制到bin目录,并且将必要的 jar 文件复制到lib目录,所有在src/dist目录的文件也会被复制。如果要在 distribution 中包括任何的静态文件,只需要把它们放在src/dist目录。
One of the convention properties added by the plugin is applicationDistribution which is a CopySpec. This specification is used by the installApp and distZip tasks as the specification of what is to be include in the distribution. Above copying the starting scripts to the bin dir and necessary jars to lib in the distribution, all of the files from the src/dist directory are also copied. To include any static files in the distribution, simply arrange them in the src/dist directory.

如果你的项目生成的文件中要在 distrubution 中,例如文档,你可以通过把它们添加到applicationDistribution复制规范,来将这些文件添加到 distribution 中。
If your project generates files to be included in the distribution, e.g. documentation, you can add these files to the distribution by adding to the applicationDistribution copy spec.

示例 45.4. 在应用程序的分发中包含其他任务的输出文件 - Example 45.4. Include output from other tasks in the application distribution

build.gradle

task createDocs {
    def docs = file("$buildDir/docs")
    outputs.dir docs
    doLast {
        docs.mkdirs()
        new File(docs, "readme.txt").write("Read me!")
    }
}

applicationDistribution.from(createDocs) {
    into "docs"
}

通过指定分布包应包括的任务的输出文件 (见15.9.1章,“声明任务的输入和输出”),Gradle 将知道在组装 distribution 前哪些生产文件的任务必须被调用,并且会为你处理好。
By specifying that the distribution should include the task's output files (see Section 15.9.1, “Declaring a task's inputs and outputs”), Gradle knows that the task that produces the files must be invoked before the distribution can be assembled and will take care of this for you.

示例 45.5. 自动创建用于分发的文件 - Example 45.5. Automatically creating files for distribution

gradle distZip的输出
Output of gradle distZip

> gradle distZip
:createDocs
:compileJava
:processResources UP-TO-DATE
:classes
:jar
:startScripts
:distZip

BUILD SUCCESSFUL

Total time: 1 secs