Gradle应用程序插件扩展了语言插件的一些常见应用程序相关的任务。它允许为jvm运行和捆绑应用程序。
The Gradle application plugin extends the language plugins with common application related tasks. It allows running and bundling applications for the jvm.
要使用这个应用程序插件,请在构建脚本中包含:
To use the application plugin, include in your build script:
示例 45.1. 使用应用程序插件 - Example 45.1. Using the application plugin
build.gradle
apply plugin:'application'
要定义应用程序的主类,你必须设置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
中的文件都将添加到分发的根目录中。你可以运行 gradle installApp
以在 build/install/
中创建应用程序的镜像。你可以运行 projectName
gradle distZip
来创建一个包含分发的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/
. You can run projectName
gradle distZip
to create a ZIP containing the distribution.
如果你的 Java 应用程序需要一组特定的 JVM 设置或系统属性,你可以配置applicationDefaultJvmArgs
属性。这些 JVM 参数会被应用于 run
任务,并且在你的分发生成的启动脚本也会考虑到它。
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"]
应用程序插件向项目添加了以下任务。
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. |
应用程序插件向项目添加了一些属性,以用于配置其行为。请参阅Project
。
The application plugin adds some properties to the project, which you can use to configure its behaviour. See Project
.
applicationDistribution
是插件添加的约定属性之一,它是一个 CopySpec
。这个副本规范在 installApp
和 distZip
任务中会用到,作为要包含在分发中的内容的描述。在分发中,以上将启动脚本复制到 bin
目录和 lib
的必要jar中,也会复制 src/dist
目录中的所有文件。如果要在分发中包含任何的静态文件,只需要把它们放在 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.
如果你的项目生成要在分发中的文件,比如文档,你可以通过把它们添加到applicationDistribution
副本规范来将这些文件添加到分发中。
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 将知道在组装分发前必须调用哪些生产文件的任务,并且会为你处理好。
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