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 .
让我们来看一个例子。要使用Groovy插件,你需要在构建脚本文件当中添加以下内容:
Let's look at an example. To use the Groovy plugin, add the following to your build file:
Example 9.1. Groovy plugin
注意: 此例子的代码可以在Gradle的二进制文件或源码中的
Note: The
code for this example can be found at
is in both the binary and source distributions of Gradle.
这段代码同时会将Java插件应用到project中,如果Java插件还没被应用的话。Groovy插件继承了
,在
This will also apply the Java plugin to the project, if it has not already been applied. The Groovy plugin extends the
to look for source files in directory
and the
to look for test source files in directory
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库。你可以通过在
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
The
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
这里是我们写好的构建文件:
Here is our complete build file:
Example 9.3. Groovy example - complete build file
运行
Running
这一章描述了一个很简单的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发行包的
项目示例。
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
in the Gradle distribution.
你的项目可以包含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/quickstartwhich
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
compiletask
to look for source files in directory
src/main/groovy,
and the
compileTesttask
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
groovyconfiguration.
The
compileconfiguration
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 buildwill 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/groovydirectory
in the Gradle distribution.
相关文章推荐
- Gradle2.0用户指南翻译——第九章. Groovy 快速入门
- Gradle 1.12 翻译——第九章 Groovy快速入门
- 【转载】Gradle学习 第九章:Groovy快速入门
- Gradle 1.12 翻译——第九章 Groovy快速入门
- Gradle用户指南(章9:Groovy快速入门)
- Gradle 教程说明 用户指南 第9章 Groovy----快速入门
- Gradle详解(一)——Groovy语法快速入门
- Gradle 教程说明 用户指南 第9章 Groovy----快速入门
- gradle--第九章 快速开始Groovy
- 基于 Groovy 的自动化构建工具 Gradle 入门(转)
- Java Gradle入门指南之内建与定制任务类(buildSrc、Groovy等)
- Gradle快速入门
- Gradle 快速入门
- Gradle 快速入门
- Gradle学习系列之一——Gradle快速入门
- Gradle学习系列之一——Gradle快速入门
- Gradle 教程说明 用户指南 第10章 Web应用程序----快速入门
- Gradle 教程说明 用户指南 第7章 构建Java工程----快速入门
- Gradle从入门到实战 - Groovy基础
- Gradle从入门到实战 - Groovy基础