Geertjan is a DZone Zone Leader and has posted 457 posts at DZone. You can read more from them at their website. View Full User Profile

How to Create Modular Groovy Applications in 5 Steps!

02.14.2009
| 16868 views |
  • submit to reddit

Let's create a modular Groovy application! Why?! Modularity is an enabler of scaleability. As your Groovy application increases in size, you increasingly need to manage code dependencies, structure your code in units that are larger than packages, and distribute pieces of your application to developers located in different locations and conflicting time zones. Welcome to modularity.

The NetBeans Platform already provides it (plus, OSGi is coming to the NetBeans Platform). Beyond modularity, there are specific features that the NetBeans Platform provides that will remain unique, such as a shared filesystem for intermodular communication and the concept of "context" (i.e., NetBeans Lookup), which not only applications have (as with the JDK 6 ServiceLoader class), but NetBeans Platform objects such as windows themselves. Welcome to loosely coupled modularity.

Now, let's get started.

  1. Start up NetBeans IDE 6.5.

  2. Go to this page and download the Groovy Console Template and use the Plugin Manager (in the Tools menu) to install it into the IDE.

  3. Now, in the New Project dialog, you should see this new project template:

  4. Click Next, give your new application a name (such as "HelloWorld") and a location on disk, and then click Finish. Expand a few folders and you should now see this:

    Briefly, the template gives you a Groovy POJO, with this content:

    package org.my.app

    public class DemoPojo {

    def foo

    }

    The template also gives you a ModuleInstall class, which handles the lifecycle of the module:

    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())
    }

    }

    }

    So, we have some standard Groovy constructs here, simply to get you started in the ecosystem of the NetBeans Platform. The module also includes a properties file for internationalization purposes and a layer.xml file, for the module's contributions to the shared filesystem.

  5. Run the application (i.e., without doing anything at all, no tweaking, no post processing, nothing at all, just run it). Look in the Output window of NetBeans IDE and you will see this:

So, you can see that you only have the absolute minimum set of modules to start with. Also you're using the Groovy compiler (thanks to an additional target that's added to the demo module's build.xml file). That's how to get started with modular Groovy applications. Have fun with Groovy on the NetBeans Platform!

 

AttachmentSize
fig-1.png18 KB
fig-2.png12.93 KB
Published at DZone with permission of its author, Geertjan Wielenga.

Comments

Wai Ho replied on Sat, 2009/02/14 - 8:40am

When I would do this and when I would do Griffon?

Geertjan Wielenga replied on Sat, 2009/02/14 - 8:44am

Good question. Modularity is not everybody's cup of tea. Though it provides no extra overhead (or the advantages outweigh any overhead you might have), not all applications need/want this kind of architecture, for personal reasons of taste and style, etc. Also, for small applications, i.e., those for which there is no plan to ever make larger than 'small', Griffon is definitely preferable.

Matt Stine replied on Fri, 2009/02/20 - 10:38am

Just out of curiosity, what does NetBeans use under the covers for its module system. I notice you say that OSGi is coming, so what is it now?

Geertjan Wielenga replied on Thu, 2009/03/05 - 12:24pm

Hi Matt. Right now NetBeans uses the standard JDK approach, that is, META-INF/services for the communication between modules, which are similar to OSGi bundles, except that the Manifest has NetBeans-specific keys, instead of OSGi keys, which is probably not going to change. OSGi will be supported in the sense that you'll be able to create OSGi bundles in NetBeans IDE, not in the sense that NetBeans IDE will run on OSGi. For more on the NetBeans module system, watch this screencast: http://mediacast.sun.com/users/GWiel/media/top10-netbeans-apis-part1.flv/details

Pat Mik replied on Fri, 2009/03/13 - 7:22pm

Can I develop a Netbeans plugin with Groovy Console Template (for example, the Google bar demo plugin)? If the answer is no, how could I code a Netbeans plugin in Groovy?

Great work! Thank you.

Mateo Gomez replied on Tue, 2012/04/17 - 12:26am

i am new to modulars and this has helped me tweak what i have right now

 mexican dessert recipes

Matt Coleman replied on Tue, 2012/04/17 - 12:46am

your the best Netbeans..this is terrific

graphic design buffalo

Carla Brian replied on Sun, 2012/05/06 - 7:25am

One Groovy script would provide the application above, the same as you get via the NetBeans Platform Application wizard in the New Project dialog. It could include a few basic modules that provide a more fleshed out layout. - James P Stuckey

Cata Nic replied on Fri, 2013/09/06 - 2:28am

 This solution looks modular and stable. Did you discovered any problematic point at this script?

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.