HOWTO: Starting Groovy development
Posted : May 6, 2004 at 10:11 am [America/Los_Angeles]
Well, as I try to take baby steps towards learning Groovy, I decided that the first step towards being successful with Groovy (or any language for that matter) is to get a development environment setup so that I can focus more on learning the syntax and less on the environment gotchas. If Groovy is “really” a good dynamic language (and I think it is), I should be able to use it without necessarily relying on IDEs. I understand there are plugins for Eclipse (and IntelliJ as well), but I decided to stay away from those while I try to learn the language.
Here are the details:
My folder structure (D:development-rootgroovy):
--------------------------------------------------------------
src/
lib/
build.properties
build.xml
My src/ folder:
------------------
Main.groovy
HelloProvider.groovy
GroovyProvider.groovy
My lib/ folder:
----------------
asm-1.4.1.jar (core groovy lib)
asm-attrs-1.4.1.jar (core groovy lib)
asm-util-1.4.1.jar (core groovy lib)
groovy-1.0-beta-4.jar (core groovy lib)
mysql-connector-3.0.11.jar (java apis you want to use in your groovy code)
...
My build.properties file:
----------------------------
# core folders
src.dir=${basedir}/src
lib.dir=${basedir}/lib
# folders to be used during build
build.dir=${basedir}/build
build.classes.dir=${build.dir}/classes
My build.xml file:
-------------------
<?xml version="1.0"?>
<project name="helloworld" basedir="." default="all">
<!-- Load user overrides -->
<property file="${user.home}/.build.properties"/>
<property file="${basedir}/build.properties"/>
<path id="compile.classpath">
<pathelement location="${build.classes.dir}"/>
<fileset dir="${lib.dir}">
<include name="**/*.jar" />
</fileset>
</path>
<path id="runtime.classpath">
<pathelement location="${build.classes.dir}"/>
<fileset dir="${lib.dir}">
<include name="**/*.jar" />
</fileset>
</path>
<!-- Task definition for groovy compiler -->
<taskdef name="groovyc" classname="org.codehaus.groovy.ant.Groovyc"
classpathref="compile.classpath"/>
<!-- TARGETS -->
<target name="init" description="Prepare build and dist directories">
<mkdir dir="${build.dir}" />
<mkdir dir="${build.classes.dir}" />
</target>
<target name="compile" depends="init" description="Compile source">
<groovyc destdir="${build.classes.dir}" srcdir="${src.dir}" listfiles="true">
<classpath refid="compile.classpath"/>
</groovyc>
</target>
<target name="run" depends="compile" description="Execute the app">
<java classname="Main" fork="true">
<classpath>
<path refid="runtime.classpath" />
</classpath>
</java>
</target>
<target name="clean" description="cleans the output directories">
<delete dir="${build.dir}"/>
</target>
<target name="all" depends="run" description="Clean and compile all components"/>
</project>
To write Groovy code, I just do the following:
1. Add groovy code into the src/ folder
2. Run (prompt)>ant clean
3. Run (prompt)>ant
4. Make changes/fixes and run (prompt)>ant again
You can download (size: 510 KB) the code (along with libs, so you really would not need any groovy libs either) and get started right away.
Hope this helps.
Links that will help you get started with Groovy:
- Anand
Category: Application Development