第九章. Groovy 快速入门

Chapter 9. Groovy Quickstart

要构建一个Groovy项目,你可以使用Groovy插件。此插件继承了Java插件,以便将Groovy的编译功能添加到项目中。你的Groovy项目可以包含Groovy源代码,Java源代码,或者两者都有。在其他各方面,Groovy项目与我们之前在《第七章,Java快速入门》所看到的都是相同的。
To build a Groovy project, you use the Groovy plugin. This plugin extends the Java plugin to add Groovy compilation capabilities to your project. Your project can contain Groovy source code, Java source code, or a mix of the two. In every other respect, a Groovy project is identical to a Java project, which we have already seen in Chapter 7, Java Quickstart.

9.1. 一个基本的Groovy 项目

9.1. A basic Groovy project

我们来看一个例子。要使用Groovy插件,请将以下内容添加到构建文件中:
Let's look at an example. To use the Groovy plugin, add the following to your build file:

示例9.1. Groovy插件 - Example 9.1. Groovy plugin

build.gradle

apply plugin: 'groovy'

注:本示例的代码可以在Gradle的二进制或源码发行包中的 samples/groovy/quickstart里找到。
Note: The code for this example can be found at samples/groovy/quickstart which is in both the binary and source distributions of Gradle.


如果还没有应用Java插件,那么Java插件也会被应用到项目中。 Groovy插件分别扩展了compile任务,以在src/main/groovy目录中查找源文件;以及compileTest任务,以在src/test/groovy目录中查找测试源文件。编译任务会把这些目录加到编译中,因此也意味着可以在项目里混合编译java和groovy。
This will also apply the Java plugin to the project, if it has not already been applied. The Groovy plugin extends the compile task to look for source files in directory src/main/groovy, and the compileTest task to look for test source files in directory src/test/groovy. The compile tasks use joint compilation for these directories, which means they can contain a mixture of java and groovy source files.

要使用groovy编译任务,还必须声明使用的Groovy版本以及可以在哪里找到Groovy库。你可以通过在groovy配置中添加依赖来实现。compile配置会继承于它,因此当编译Groovy和Java源码时,groovy库会被包含在类路径中。以下例子,我们将使用公共Maven仓库中的Groovy 2.2.0版本:
To use the groovy compilation tasks, you must also declare the Groovy version to use and where to find the Groovy libraries. You do this by adding a dependency to the groovy configuration. The compile configuration inherits this dependency, so the groovy libraries will be included in classpath when compiling Groovy and Java source. For our sample, we will use Groovy 2.2.0 from the public Maven repository:

示例 9.2. 依赖 Groovy 2.2.0 - Example 9.2. Dependency on Groovy 2.2.0

build.gradle

repositories {
    mavenCentral()
}

dependencies {
    compile 'org.codehaus.groovy:groovy-all:2.3.3'
}

这里是我们的完整构建文件:
Here is our complete build file:

示例9.3. Groovy示例 - 完整的构建文件 - Example 9.3. Groovy example - complete build file

build.gradle

apply plugin: 'eclipse'
apply plugin: 'groovy'

repositories {
    mavenCentral()
}

dependencies {
    compile 'org.codehaus.groovy:groovy-all:2.3.3'
    testCompile 'junit:junit:4.11'
}

运行 gradle build将会对你的项目进行编译,测试并打包成jar。
Running gradle build will compile, test and JAR your project.

9.2. 总结

9.2. Summary

本章介绍了一个非常简单的Groovy项目。通常情况下,一个真实的项目所需要的不止于此。因为Groovy项目也是一个Java项目,无论你如何使用Java项目,Groovy项目也可以被这样使用。
This chapter describes a very simple Groovy project. Usually, a real project will require more than this. Because a Groovy project is a Java project, whatever you can do with a Java project, you can also do with a Groovy project.

你可以在《第二十四章, Groovy插件》中了解更多关于Groovy 插件的内容,或在Gradle的 samples/groovy目录中找到更多Groovy 项目示例。
You can find out more about the Groovy plugin in Chapter 24, The Groovy Plugin, and you can find more sample Groovy projects in the samples/groovy directory in the Gradle distribution.