Hacking NetBeans #2 - Executing Ant Tasks from the IDE



In my new plug-in I need to call an an task from the IDE. Thanks to Zajo for telling me the secret how to do that via NetBeans API. It's quite simple, the hardest thing is (as usual) to find the right classes and methods. Here is the source code:

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import org.apache.tools.ant.module.api.support.ActionUtils;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
...

       String script = "<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?>" +
                "<project name=\\"Packager\\" default=\\"copy\\" basedir=\\".\\">" +
                "<target name=\\"copy\\">" +
                "<copy todir=\\"e:\\\\test2\\">" +
                "<fileset dir=\\"e:\\\\test\\" includes=\\"\*\*/\*.txt\\"/>" +
                "</copy>" +
                "</target>" +
                "</project>";
        try {
           File zf = File.createTempFile("ant-copy", "xml");
           BufferedWriter out = new BufferedWriter(new FileWriter(zf.getAbsoluteFile()));
           out.write(script);
           out.close();
           FileObject zfo = FileUtil.toFileObject(FileUtil.normalizeFile(zf));
           ActionUtils.runTarget(zfo, new String[] {"copy"}, null);
           zf.deleteOnExit();
        } catch (IOException e) {
           System.out.println("IO error: "+e);
        }

What do I do in the code? At first I create a xml file which will serve as the ant script. I create it as a temporary file in default temp dir and write the xml into it. To execute the ant task I call the ActionUtils.runTarget method with parameters - a fileobject (created from a normalized File), 1 target to run and no properties. At the end I mark the file to be deleted when VM exits. Easy, isn't it?

To be able to compile this code you need to add to your plug-in two dependencies - Ant and Execution API. When you run this code, the output window opens and shows the results:



Now I can do with my plug-in anything what ant is capable of. Time to generate some powerful ant scripts.
Comments:

Roumen, Two comments: 1. Please indicate fully qualified class names, otherwise it would be hard to see where you use NetBeans API (there's a java.io.File also, but I have a feeling your code does not use that; there's also an ActionUtils in Struts, I think, but I bet you're not using that one, as well). 2. As a matter of style, don't have the Ant build file hard-coded into your program, rather access it from a file specified in command line arguments. Having such things encoded in your class file is just bad.

Posted by Lucian Pintilie on červenec 27, 2005 at 12:08 dop. CEST #

Hi Lucian,

ad 1. Sure, I'll fix that.

ad 2. Well my plug-in needs a generated ant script depending on the inputs - the build script will be different every time. Maybe I can do it by passing parameters to the ant script. Thanks for your comments.

Posted by Roman Strobl on červenec 27, 2005 at 03:55 dop. CEST #

I've added the imports, using fq names would make the code less readable.

Posted by Roman Strobl on červenec 27, 2005 at 04:00 dop. CEST #

The imports are just fine (and more elegant, more readable as well). Thank you!

Posted by Lucian Pintilie on červenec 27, 2005 at 08:09 dop. CEST #

Post a Comment:
Comments are closed for this entry.
About

Roman Strobl

Search

Archives
« duben 2014
PoÚtStČtSoNe
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
    
       
Today