Wizard-based "grails create-app"

The 'grails create-app' command calls a Groovy script, from the command line or from an Ant script, and then creates a pretty complete outline of a Grails application. If one wants an IDE to support Grails, a good thing to do is to somehow run that 'grails create-app' command. So, one approach is to create a WebFrameworkProvider class, which is a NetBeans API class that provides an extension point to the web application project type. In that class, the extend method must, somehow, call the Ant script that kicks off the related Groovy script. Calling Ant from within a NetBeans module is described in this blog entry. So here is a method that creates my Ant script, note though that the environment variables are still hard coded:

public String getScript() {
    String script = "<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?>" + 
        "<project name=\\"" + projName + "\\" basedir=\\"" + projPath + "\\">" +
        "<property name=\\"grails\\" value=\\"grails\\" />" +
        "   <target name=\\"grails create-app\\">" + 
        "     <exec executable=\\"${grails}\\" failonerror=\\"true\\">" + 
        "       <env key=\\"JAVA_HOME\\" value=\\"/usr/lib/jvm/java-1.5.0-sun-1.5.0.11\\"/>" +  
        "       <env key=\\"GRAILS_HOME\\" value=\\"/home/geertjan/grails-0.5.6\\"/>" +
        "       <arg value=\\"create-app " + projName + "\\" />" + 
        "     </exec>" +   
        "   </target>" + 
        "</project>";
    return script;
}

Now we need to call the above Ant script, which, via the 'grails' property, has access to the 'create-app' target, the latter which I have highlighted above. However, we need to somehow get hold of the project path (so that the target will generate folders and files in our web application) as well as the project name (so that the web application will be populated with our Grails extension).

So, here is my WebFrameworkProvider.extend method, which is called at the end of the Web Application wizard, if 'Grails' is selected in the Framework panel:

public Set extend(WebModule wm) {
    FileObject documentBase = wm.getDocumentBase();
    Project project = FileOwnerQuery.getOwner(documentBase);
    FileObject fo = project.getProjectDirectory();
    projPath = File.separator + fo.getParent().getPath();
    projName = fo.getName();
    try {
        File zf = File.createTempFile("grails-create-app", "xml");
        BufferedWriter out = new BufferedWriter(new FileWriter(zf.getAbsoluteFile()));
        out.write(getScript());
        out.close();
        FileObject zfo = FileUtil.toFileObject(FileUtil.normalizeFile(zf));
        ActionUtils.runTarget(zfo, new String[]{"grails create-app"}, null);
        zf.deleteOnExit();
    } catch (IOException ex) {
        Exceptions.printStackTrace(ex);
    }
    return Collections.EMPTY_SET;
}

And that's all. Now you have called 'grails create-app' at the end of the Web Application wizard, which adds all the folders and files to your web application, transforming it into a Grails application. There are now still several other problems to solve, too many to mention here, but this approach is a useful way forward.

Comments:

hi geertjan,

I know groovy is a fantastic scripting language. i would like to know your thoughts about the future of groovy.kindly share it and oblige.

Thank you

Posted by Ranganath.S on August 06, 2007 at 08:40 PM PDT #

kjşijkşlm
jkmkl
jkolmk kmk j no o oıoujhyu

Posted by guest on August 10, 2007 at 10:52 PM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed
About

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
12
13
14
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today