Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How to Create Modular Groovy Applications in 5 Steps!

DZone's Guide to

How to Create Modular Groovy Applications in 5 Steps!

· Java Zone
Free Resource

Learn how our document data model can map directly to how you program your app, and native database features like secondary indexes, geospatial and text search give you full access to your data. Brought to you in partnership with MongoDB.

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!

 

Discover when your data grows or your application performance demands increase, MongoDB Atlas allows you to scale out your deployment with an automated sharding process that ensures zero application downtime. Brought to you in partnership with MongoDB.

Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}