https://github.com/msdx/gradledoc
本文翻译所在分支:
https://github.com/msdx/gradledoc/tree/2.0 。
如你所见,Gradle是一个通用的构建工具。你在构建脚本中实现的几乎任何内容它都能够构建。它开箱即用,但是需要你在构建脚本中先写好代码。
As we have seen, Gradle is a general-purpose build tool. It can build pretty much anything you care to implement in your build script. Out-of-the-box, however, it doesn't build anything unless you add code to your build script to do so.
大部分的Java项目的基本流程都是类似的:编译Java源文件,运行单元测试,创建一个Jar包。 如果可以不用为每个项目都去编写这些流程那就好了。在实际上,我们确实不用。Gradle通过 插件来解决这一问题。插件就是Gradle的扩展,可以让你以某种方式来配置你的项目,通常通过添加一些预先配置的任务来共同做一些有用的事情。Gradle内置了许多插件,并且你也可以轻松地编写你自己的插件和分享自己的插件。Java plugin是其中之一,这一插件向项目添加一些编译和测试Java源代码,并打包成JAR包的任务。
Most Java projects are pretty similar as far as the basics go: you need to compile your Java source files, run some unit tests, and create a JAR file containing your classes. It would be nice if you didn't have to code all this up for every project. Luckily, you don't have to. Gradle solves this problem through the use of plugins. A plugin is an extension to Gradle which configures your project in some way, typically by adding some pre-configured tasks which together do something useful. Gradle ships with a number of plugins, and you can easily write your own and share them with others. One such plugin is the Java plugin. This plugin adds some tasks to your project which will compile and unit test your Java source code, and bundle it into a JAR file.
这个Java插件是基于约定的。这也意味着它为项目的很多个方面都定义了一些默认值,比如Java源文件的位置。如果你遵循这些约定,那么你就不用在构建脚本中再去做太多的事情。如果你不想或无法遵循约定,Gradle也允许你进行自定义。实际上,由于对Java项目的支持是以插件的形式来实现的,你甚至可以完全不用这个构建来构建Java项目。
The Java plugin is convention based. This means that the plugin defines default values for many aspects of the project, such as where the Java source files are located. If you follow the convention in your project, you generally don't need to do much in your build script to get a useful build. Gradle allows you to customize your project if you don't want to or cannot follow the convention in some way. In fact, because support for Java projects is implemented as a plugin, you don't have to use the plugin at all to build a Java project, if you don't want to.
在后续章节中,我们有深入介绍与Java插件,依赖管理以及多项目构建的相关的许多例子。在本章里,我们还是先了解一下使用Java插件来构建Java项目的基本用法。
We have in-depth coverage with many examples about the Java plugin, dependency management and multi-project builds in later chapters. In this chapter we want to give you an initial idea of how to use the Java plugin to build a Java project.
让我们来看一个简单的例子。要使用Java插件,只需在构建脚本中添加如下代码:
Let's look at a simple example. To use the Java plugin, add the following to your build file:
示例 7.1. 使用 Java 插件 - Example 7.1. Using the Java plugin
build.gradle
apply plugin: 'java'
注:本示例的代码可以在Gradle的二进制或源代码发行包中的 samples/java/quickstart
里找到。
Note: The code for this example can be found at samples/java/quickstart
which is in both the binary and source distributions of Gradle.
定义一个Java项目只需要这些就够了。它将会向你的项目应用Java插件,从而添加一些任务。
This is all you need to define a Java project. This will apply the Java plugin to your project, which adds a number of tasks to your project.
有哪些任务可用?
你可以使用 gradle tasks
来列出一个项目的任务,这样就会看到Java插件向项目添加了哪些项目。
You can use gradle tasks
to list the tasks of a project. This will let you see the tasks that the Java plugin has added to your project.
What tasks are available?
Gradle会在 src/main/java
and your test source code under和 src/test/java
能分别找你的生产 源码和测试源码。并且,在 src/main/resources
的所有文件都会作为资源被打包进JAR包,任何在 src/test/resources
下的资源文件都会被包含到类路径中用于执行测试。所有输出的文件都会在 build
目录里创建,而JAR包会被输出在build/libs
目录里。
Gradle expects to find your production source code under src/main/java
and your test source code under src/test/java
. In addition, any files under src/main/resources
will be included in the JAR file as resources, and any files under src/test/resources
will be included in the classpath used to run the tests. All output files are created under the build
directory, with the JAR file ending up in the build/libs
directory.
Java插件向你的项目中添加了不少的任务。但是,在构建项目时只有其中的一部分任务才使用到。最常用的是这个 build
任务,它会对项目做一次完整的构建。当运行 gradle build
时,Gradle会编译并执行测试,然后将你的主要类文件和资源文件打成JAR包。
The Java plugin adds quite a few tasks to your project. However, there are only a handful of tasks that you will need to use to build the project. The most commonly used task is the build
task, which does a full build of the project. When you run gradle build
, Gradle will compile and test your code, and create a JAR file containing your main classes and resources:
示例7.2. 构建Java项目 - Example 7.2. Building a Java project
gradle build
的输出结果
Output of gradle build
> gradle build :compileJava :processResources :classes :jar :assemble :compileTestJava :processTestResources :testClasses :test :check :build BUILD SUCCESSFUL Total time: 1 secs
其他一些有用的任务是:
Some other useful tasks are:
- clean
-
删除
build
目录,移除所有构建的文件。
Deletes thebuild
directory, removing all built files. - assemble
-
编译代码并打成jar包,但不会运行单元测试。其他的插件会向这个任务添加更多的工件。例如,如果你使用 War 插件,这个任务也会对你的项目构建出 WAR 文件。
Compiles and jars your code, but does not run the unit tests. Other plugins add more artifacts to this task. For example, if you use the War plugin, this task will also build the WAR file for your project. - check
-
编译和测试代码。其他的插件会向这一任务添加更多的检查。例如,如果你使用
checkstyle
插件,这一任务就会针对你的源代码运行 Checkstyle 。
Compiles and tests your code. Other plugins add more checks to this task. For example, if you use thecheckstyle
plugin, this task will also run Checkstyle against your source code.
通常一个Java项目都会依赖一些外部JAR文件。如果想在项目里引用这些JAR文件,你需要告诉Gradle如何找到这们。在Gradle中,像JAR文件这样的工件都存在于 仓库。仓库可以用于获取项目中的依赖,或是发布项目的工件。在下面的例子中,我们将使用公共的Maven仓库:
Usually, a Java project will have some dependencies on external JAR files. To reference these JAR files in the project, you need to tell Gradle where to find them. In Gradle, artifacts such as JAR files, are located in a repository. A repository can be used for fetching the dependencies of a project, or for publishing the artifacts of a project, or both. For this example, we will use the public Maven repository:
示例 7.3. 添加 Maven 仓库 - Example 7.3. Adding Maven repository
build.gradle
repositories { mavenCentral() }
让我们来添加一些依赖。在这里,我们将声明我们的生产类对commons collection具有编译时依赖,我们的测试类对junit具有需要编译时依赖:
Let's add some dependencies. Here, we will declare that our production classes have a compile-time dependency on commons collections, and that our test classes have a compile-time dependency on junit: