本文适合于不熟悉 Groovy,但想快速轻松地了解其基础知识的 Java开发人员。了解 Groovy 对 Java 语法的简化变形,学习 Groovy 的核心功能,例如本地集合、内置正则表达式和闭包。编写第一个 Groovy 类,然后学习如何使用 JUnit 轻松地进行测试。借助功能完善的 Groovy 开发环境和使用技能,您将轻松完成本教程的学习。最重要的是,您将学会如何在日常 Java 应用程序开发中联合使用 Groovy 和 Java 代码。
阅读本文的前提条件:为了从本教程得到最大收获,您应该熟悉 Java 语法和在 Java 平台上进行面向对象开发的基本概念。如果熟悉C#的话,基本上也能很快上手。
本文预计时长:30分钟
一、groovy简介和环境搭建
本机环境:
ubuntu 14.04 64bit
JDK 1.7.67
IDE : intellij idea 13.1
1、groovy简介
其官方介绍为,Groovy...
- is an agile and dynamic language for the Java Virtual Machine
- builds upon the strengths of Java but has additional power features inspired by languages like Python, Ruby and Smalltalk
- makes modern programming features available to Java developers with almost-zero learning curve
- provides the ability to statically type check and statically compile your code for robustness and performance
- supports Domain-Specific Languages and other compact syntax so your code becomes easy to read and maintain
- makes writing shell and build scripts easy with its powerful processing primitives, OO abilities and an Ant DSL
- increases developer productivity by reducing scaffolding code when developing web, GUI, database or console applications
- simplifies testing by supporting unit testing and mocking out-of-the-box
- seamlessly integrates with all existing Java classes and libraries
- compiles straight to Java bytecode so you can use it anywhere you can use Java
简单说来:Groovy是一种运行在JVM上的动态语言,它吸取了Python,Ruby和Smalltalk等语言的优点,在Java语言的基础之上增加了许多特色功能;对于Java开发人员来说掌握Groovy是没有什么太大障碍的;相比 Java 而言,语法更易懂,更易上手,更易调试;无缝的集成了Java 的类和库;编译后的.groovy也是以class的形式出现的。
2、groovy下载
网址:http://groovy.codehaus.org/Download
目前最新稳定版为Groovy 2.3 (2014-09-14)
这里下载 :
Download zip: Binary Release :groovy-binary-2.3.6.zip
Download documentation: JavaDoc and zipped online documentation :groovy-docs-2.3.6.zip
3、groovy环境配置和Hello World!
1)首先解压:
unzip groovy-binary-2.3.6.zip #解压groovy unzip groovy-docs-2.3.6.zip #解压docs
2) 进入到Groovy Shell命令界面:
amosli@amosli-ThinkPad:~/developsoft/language/groovy/groovy-2.3.6/bin$ ./groovysh Groovy Shell (2.3.6, JVM: 1.7.0_67) Type ':help' or ':h' for help. ------------------------------------------------------------------------------- groovy:000> println "hello world!" hello world! ===> null groovy:000> :h
在Groovy Shell里不必定义class可以直接写代码,如下面进行一个for循环:
groovy:000> for(i=0;i<10;i++){ groovy:001> println("i:"+i);} i:0 i:1 i:2 i:3 i:4 i:5 i:6 i:7 i:8 i:9 ===> null
注意这里,你可以发现i是没有指定int类型的,这里也是写法上也是比较随意的。
3)、将groovy加入到环境变量(可选)
将解压后的groovy拷到/usr/local/groovy 目录下:
root@amosli-ThinkPad:/usr/local/groovy# cp -r /home/amosli/developsoft/language/groovy/groovy-2.3.6 .
将groovy路径拷到/etc/profile里:
gedit /etc/profile #使用gedit打开profile,也可以使用vi等工具
将下面内容拷到profile里最后位置:
export GROOVY_HOME=/usr/local/groovy/groovy-2.3.6 export PATH=$GROOVY_HOME/bin:$PATH:. export GROOVY_HOME export PATH
全部的profile内容:
# /etc/profile: system-wide .profile file for the Bourne shell (sh(1)) # and Bourne compatible shells (bash(1), ksh(1), ash(1), ...). if [ "$PS1" ]; then if [ "$BASH" ] && [ "$BASH" != "/bin/sh" ]; then # The file bash.bashrc already sets the default PS1. # PS1='\h:\w\$ ' if [ -f /etc/bash.bashrc ]; then . /etc/bash.bashrc fi else if [ "`id -u`" -eq 0 ]; then PS1='# ' else PS1='$ ' fi fi fi # The default umask is now handled by pam_umask. # See pam_umask(8) and /etc/login.defs. if [ -d /etc/profile.d ]; then for i in /etc/profile.d/*.sh; do if [ -r $i ]; then . $i fi done unset i fi JAVA_HOME=/usr/local/java/jdk1.7.0_67 PATH=$PATH:$HOME/bin:$JAVA_HOME/bin export JAVA_HOME export PATH export GROOVY_HOME=/usr/local/groovy/groovy-2.3.6 export PATH=$GROOVY_HOME/bin:$PATH:. export GROOVY_HOME export PATH