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

翻译项目请关注Github上的地址:
https://github.com/msdx/gradledoc
本文翻译所在分支:
https://github.com/msdx/gradledoc/tree/2.0 。
在线浏览地址:
http://gradledoc.qiniudn.com/2.0/userguide/userguide.html 。
另外,Android 手机用户可通过我写的一个程序浏览文档,带缓存功能的,目前0.6开发中版本兼容 Android 2.3以上系统,项目地址如下:
https://github.com/msdx/gradle-doc-apk
翻译不易,转载请注明本文在CSDN博客上的出处:
http://blog.csdn.net/maosidiaoxian/article/details/78402799

关于我对Gradle的翻译,以Github上的项目及http://gradledoc.qiniudn.com 上的文档为准。如发现翻译有误的地方,将首先在以上两个地方更新。因时间精力问题,博客中发表的译文基本不会同步修改。


第九章. 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.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值