要構建一個Groovy項目,你需要使用Groovy插件。該插件擴展了Java插件,對你的項目增加了Groovy的編譯功能.
你的項目可以包含Groovy源碼,Java源碼,或者兩者都包含。在其他各方面,Groovy項目與我們在第七章 Java快速入門 中所看到的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 .
讓我們來看一個例子。要使用Groovy插件,你需要在構建腳本文件當中添加以下內容:
Let's look at an example. To use the Groovy plugin, add the following to your build file:
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插件應用到project中,如果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:
Example 9.2. Dependency on Groovy 2.2.0
build.gradle
repositories {
mavenCentral()
}
dependencies {
compile 'org.codehaus.groovy:groovy-all:2.2.0'
}
這裏是我們寫好的構建文件:
Here is our complete build file:
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.2.0' testCompile 'junit:junit:4.11' }
運行 gradle
build
將會對你的項目進行編譯,測試和打成jar包。
Running gradle
build
will compile, test and JAR your project.
這一章描述了一個很簡單的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.
你可以參閱 第24章 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.