Gradle2.0用户指南翻译——第七章. Java 快速入门

简介: 翻译项目请关注Github上的地址:https://github.com/msdx/gradledoc本文翻译所在分支:https://github.com/msdx/gradledoc/tree/2.0 。
翻译项目请关注Github上的地址:
https://github.com/msdx/gradledoc
本文翻译所在分支:
https://github.com/msdx/gradledoc/tree/2.0 。


第七章. Java 快速入门

Chapter 7. Java Quickstart

7.1. Java 插件

7.1. The Java plugin

如你所见,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.

7.2. 一个基本的 Java 项目

7.2. A basic 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.

7.2.1. 构建项目

7.2.1. Building the project

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 the build 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 the checkstyle plugin, this task will also run Checkstyle against your source code.

7.2.2. 外部依赖

7.2.2. External dependencies

通常一个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:

示例 7.4. 添加 依赖 - Example 7.4. Adding dependencies

build.gradle

dependencies {
    compile group: 'commons-collections', name: 'commons-collections', version: '3.2'
    testCompile group: 'junit', name: 'junit', version: '4.+'
}

目录
相关文章
|
19天前
|
Java
Java快速入门之判断与循环
本文介绍了编程中的流程控制语句,主要包括顺序结构、判断结构(if语句和switch语句)以及循环结构(for、while和do...while)。通过这些语句可以精确控制程序的执行流程。if语句有三种格式,分别用于简单条件判断、二选一判断和多条件判断。switch语句适用于有限个离散值的选择判断,而循环结构则用于重复执行某段代码,其中for循环适合已知次数的情况,while循环适合未知次数但有明确结束条件的情况,do...while则是先执行后判断。文中还提供了多个示例和练习,帮助读者理解并掌握这些重要的编程概念。
|
9月前
|
XML Java 项目管理
java maven 和gradle哪种好
java maven 和gradle哪种好
218 0
|
14天前
|
存储 Java 索引
Java快速入门之数组、方法
### Java快速入门之数组与方法简介 #### 一、数组 数组是一种容器,用于存储同种数据类型的多个值。定义数组时需指定数据类型,如`int[]`只能存储整数。数组的初始化分为静态和动态两种: - **静态初始化**:直接指定元素,系统自动计算长度,如`int[] arr = {1, 2, 3};` - **动态初始化**:手动指定长度,系统给定默认值,如`int[] arr = new int[3];` 数组访问通过索引完成,索引从0开始,最大索引为`数组.length - 1`。遍历数组常用`for`循环。常见操作包括求和、找最值、统计特定条件元素等。
|
10天前
|
Java
Java快速入门之类、对象、方法
本文简要介绍了Java快速入门中的类、对象和方法。首先,解释了类和对象的概念,类是对象的抽象,对象是类的具体实例。接着,阐述了类的定义和组成,包括属性和行为,并展示了如何创建和使用对象。然后,讨论了成员变量与局部变量的区别,强调了封装的重要性,通过`private`关键字隐藏数据并提供`get/set`方法访问。最后,介绍了构造方法的定义和重载,以及标准类的制作规范,帮助初学者理解如何构建完整的Java类。
|
7月前
|
存储 Java 编译器
Java基础教程(五千字带你快速入门!)(二)
Java基础教程(五千字带你快速入门!)(二)
|
7月前
|
存储 安全 Java
Java基础教程(五千字带你快速入门!)(一)
Java基础教程(五千字带你快速入门!)(一)
|
8月前
|
JavaScript 前端开发 Java
【vue快速入门】很适合JAVA后端看
【vue快速入门】很适合JAVA后端看
84 0
|
6月前
|
SQL Java 关系型数据库
【前端学java】JDBC快速入门
【8月更文挑战第12天】JDBC快速入门
45 2
【前端学java】JDBC快速入门
|
6月前
|
存储 前端开发 JavaScript
【前端学JAVA】有手就会!10min快速入门java的基础语法(2)
【8月更文挑战第8天】10min快速入门java的基础语法
51 2
【前端学JAVA】有手就会!10min快速入门java的基础语法(2)
|
9月前
|
Java
安装JAVA_JDK快速入门
安装JAVA_JDK快速入门