X

Geertjan's Blog

  • July 30, 2008

World's Simplest GroovyBuilder

Geertjan Wielenga
Product Manager
Here's a GroovyBuilder for generating NetBeans projects such as the ones I showed in the past days:
package generators
import groovy.util.BuilderSupport
class NBProjectGenerator extends BuilderSupport {
protected void setParent(Object parent, Object child) {
}
protected Object createNode(Object name) {
return null
}//Here we receive the path to a folder that should be created:
protected Object createNode(Object name, Object value) {
def folder = new File(value)
folder.mkdirs()
}//Here we create a new file in a specified location
//and with a specified content:

protected Object createNode(Object name, Map attrs) {
new File(attrs.get("loc") + File.separator + attrs.get("name")).write(attrs.get("file"))
}
protected Object createNode(Object name, Map attrs, Object value) {
return null
}
}

It could be used for generating any kind of source structure, not only NetBeans projects at all, with folders and files being the two only items of concern. This is how it would be used to create a new NetBeans module:

NBProjectGenerator nbProjectGenerator = new NBProjectGenerator()
nbProjectGenerator.generate() {//Root folder:
folderProp(folder)//Source folder:
srcProp(src)//NetBeans project folder:
nbprojectProp(nbproject)//Main package:
pkgsProp(pkgs)//Ant build.xml script:
buildXmlProp(loc:folder, file:build_xml, name:"build.xml")//Build-impl.xml script:
buildImplXmlProp(loc:nbproject, file:build_impl_xml, name:"build-impl.xml")//Bundle.properties:
bundleProp(loc:pkgs, file:bundle_properties, name:"Bundle.properties")//Layer file:
layerXmlProp(loc:pkgs, file:layer_xml, name:"layer.xml")//Manifest file:
manifestMfProp(loc:folder, file:manifest_mf, name:"manifest.mf")//Platform properties:
platformProp(loc:nbproject, file:platform_properties, name:"platform.properties")//Project properties:
projectProp(loc:nbproject, file:project_properties, name:"project.properties")//Project XML file:
projectXmlProp(loc:nbproject, file:project_xml, name:"project.xml")
}

Using the very same GroovyBuilder, you can generate a NetBeans Platform application. Only remember to exclude several of the statements above, since a NetBeans Platform application doesn't have its own build.xml, for example:

NBProjectGenerator nbProjectGenerator = new NBProjectGenerator()
nbProjectGenerator.generate() {//Root folder:
folderProp(folder)//NetBeans project folder:
nbprojectProp(nbproject)//Platform properties:
platformProp(loc:nbproject, file:platform_properties, name:"platform.properties")//Project properties:
projectProp(loc:nbproject, file:project_properties, name:"project.properties")//Project XML file:
projectXmlProp(loc:nbproject, file:project_xml, name:"project.xml")
}

Something similar could be done for NetBeans web applications too, for example. The only thing that needs to be done is for the values to be retrieved somehow. Right now I have them hardcoded in my Groovy script (as shown in the blog entries of the last two days). However, I could access a file on disk via Groovy and set that as the value that is passed to the GroovyBuilder. And I could create another GroovyBuilder to simplify that in the same way as the above. I'd then be accessing two GroovyBuilders from within the same Groovy script and then generating NetBeans projects very quickly, efficiently, and transparently. The Groovy script itself would end up being pretty short and with no superfluous, repetitive boilerplate code, because that's moved to the GroovyBuilder class. All that's pretty neat!

Join the discussion

Comments ( 1 )
  • JS Wednesday, July 30, 2008

    One could create a Grails cmd line script so we could generate a full fledged Netbeans 6.5 Grails project from a naked grails app.

    prompt> grails netbeans

    And voilà!


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.