您的位置:首页 > 其它

Gradle第九章:Groovy快速入门

2015-05-25 17:38 155 查看
要构建一个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 .


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:

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.


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.

你可以参阅 第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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: