javablogspot

Just another WordPress.com weblog

3, 2009 Running Groovy on the NetBeans Platform

Posted by damuchinni on February 14, 2009

Here’s a Groovy POJO:

package org.my.app

public class DemoPojo {

    def foo

}

And here’s my ModuleInstall class (which is a NetBeans module’s lifecycle manager), which uses its restored() method to instantiate the POJO, set the variable, and then print out the variable. Oh, by the way, the class below is, just like the above, a Groovy class. The thing to notice is that I defined no getter/setter in the POJO above, because Groovy simply assumes I have them, and that there are no semi-colons at the end of the statements at all (thanks again Groovy).

package org.my.app

import org.openide.modules.ModuleInstall as MI

public class Installer extends MI {

    @Override
    public void restored() {

        for(int i = 0; i < 10; i++) {
            DemoPojo dp = new DemoPojo()
            dp.setFoo(i)
            println("Number: " + dp.getFoo())
        }

    }

}

Also, the “println” statement comes from Groovy, without my needing to import the class. Look also at the abbreviation for my imported class, which I can then refer to in that way in my class signature (and anywhere else). In fact, I could use any Groovy constructs at all now.

So the above two are in a module. The ModuleInstall class is registered in the Manifest, just like any other ModuleInstall class, because after compilation the Groovy class becomes a Java class. At that point, there’s no difference anymore.

To make all of the above possible, add the following to your module’s build.xml file:

<taskdef name="groovyc"
     classpath="ext/groovy-all.jar"
     classname="org.codehaus.groovy.ant.Groovyc"/>

<target name="netbeans-extra">
    <groovyc srcdir="${src.dir}" destdir="${build.classes.dir}">
        <classpath refid="cp"/>
    </groovyc>
    <jarwithmoduleattributes jarfile="${cluster}/${module.jar}"
           compress="${build.package.compress}" index="${build.package.index}"
           manifest="${manifest.mf}" stamp="${cluster}/.lastModified">
        <fileset dir="${build.classes.dir}"/>
    </jarwithmoduleattributes>
</target>

So, after the Groovy compiler does its thing, the module’s JAR is recreated, using the output from the Groovy compiler. Note that, as indicated above, you need a folder in your module named “ext”, containing “groovy-all.jar”. You also need to put your module in a suite and set a dependency on a library wrapper module that contains “groovy-all.jar”.

Here’s the Projects view on my module, isn’t it cool to see only Groovy classes there?

Then you’ve got everything you need to run Groovy on the NetBeans Platform. Now excuse me while I go and create some file templates for Groovy versions of TopComponents, Actions, and so on…

Advertisements

2 Responses to “3, 2009 Running Groovy on the NetBeans Platform”

  1. Mike said

    Just passing by.Btw, your website have great content!

    _________________________________
    Making Money $150 An Hour

    • damuchinni said

      Thanks Mike

      its my pleasure, if u want any iformatiom more please feel free to post, so that i can provide.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: