Tuesday, July 29, 2008

Adding Groovy File Compilation To Any Netbeans Project

Many of you may already be aware that adding support for groovy file
compilation in any Netbeans project is easy. You can easily adjust the
build file to use the groovyc compiler. For those of you who do not know
how to do this, simply perform the following:

1) Adjust build.xml by adding the following target:

<target name="-pre-compile">
  classpath="${your.netbeans.groovy.library.classpath}" />
  <groovyc destdir="${classes.dir}"
  classpath="${ your.netbeans.groovy.library.classpath}"
  jointcompilationoptions="-j -Jsource=1.5 -Jtarget=1.5">

2) Compile your project as normal!

All .groovy files will be compiled into Java...seamless integration. If you
are already using the "-pre-compile" target for another task, then name it
something else and adjust the build-impl.xml accordingly to include the new


  1. Is this to keep from having it compiled during run time? Or so that the other Java classes can call into the groovy files. Just curious. I am just learning groovy.

  2. Using this method allows blatant mixing of Java and Groovy files within a project. If you wish to use Java source for implementing some things, but then use Groovy source to implement another, then you can do do using this feature. Without doing so, the build process on a regular Java project will fail when it tries to compile a .groovy file...you need to use the groovyc task to compile such code.

    As a note: If you use Netbeans 6.5, you have the ability to create Groovy files (and Grails projects). The editor treats files with the .groovy extension special just for Groovy.


Please leave a comment...