【Gradle教程】第九章:Groovy快速入门

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项目,你需要用到Groovy插件。这个插件继承自Java插件以便添加Groovy编译能力到你的项目中。你的项目可以包含Groovy源码,Java源码,或者是二者混合使用。在其他所以方面,一个Groovy项目与Java项目相同,这一点我们在第七章,Java快速入门里就已经看到了。

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:
<翻译>接下来让我们看一个例子。想要使用Groovy插件就添加如下代码到你的构建文件中:

Example 9.1. Groovy plugin Groovy插件

build.gradle

apply plugin: 'groovy'

Note: The code for this example can be found at samples/groovy/quickstart which is in both the binary and source distributions of Gradle.
注意:这个例子中代码可以在samples/groovy/quickstart目录下找到。
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.
<翻译>如果Java插件还没有被加入,那么这段代码同样也会将Java插件加入到项目中。Groovy插件继承了compile task以便从src/main/groovy目录中搜索源码文件,继承了compileTest task以便从src/test/groovy目录中搜索测试源码文件。compile tasks对这些目录使用了联合编译,这意味着他们可以混合Java和Groovy文件。

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:
<翻译>想要使用Groovy编译任务,你必须声明Groovy版本和在哪里能够找到Groovy库文件,你可以通过在Groovy配置项中添加依赖来完成这两项工作。编译配置项继承了该依赖声明,所以当编译Groovy和Java源码时Groovy库文件会被包含到classpath中。在我们的示例中,我们将会使用来自公共Maven仓库的Groovy 2.2.0(我觉得这里版本号写错了,因为根据下面的示例,版本应该是2.3.6):

Example 9.2. Dependency on Groovy Groovy依赖

build.gradle

repositories {
    mavenCentral()
}

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


Here is our complete build file:
<翻译>下面是完整的脚本构建文件:

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

build.gradle

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

repositories {
    mavenCentral()
}

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


Running gradle build will compile, test and JAR your project.
<翻译>运行命令gradle build将会编译、测试并打包你的项目。

9.2. Summary

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项目就是一个Java项目,任何你可以用Java项目做的,你也可以用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.
<翻译>24章,Groovy插件中你可以找到更多有关Groovy插件的信息,并且你可以在Gradle安装目录目录下的samples/groovy目录中找到更多的Groovy项目示例。

原文地址:http://www.gradle.org/docs/cur ... .html
翻译者:Jerry
邮箱:hjpdyxhjd@163.com

如对翻译内容有异议,请在评论区提出或联系作者
2 分享
studio chendeshen
studio

studio

楼主辛苦了!
0 赞 2014-12-16 15:49

要回复文章请先登录注册