« červenec 2005 »
PoÚtStČtSoNe
    
23
30
31
       
Today

Navigation

Speaker Profile
Roumen's Weblog
Login
Sun Bloggers
Technorati Profile

Am I popular?

Today's Page Hits: 1386

Contacts

Name: Roman Strobl
E-mail: roman dot strobl
at sun dot com

NetBeans

Java Sites

Javalobby
The Server Side
Java Tips
Java Blogs
java.net
java.sun.com
java.cz

Blogs

NetBeans:
Geertjan
Brian Leonard
Gregg Sporar
Lukas Hasik
Ludovic Champenois
Vincent Brabant
Alexis Moussine-Pouchkine
Jullion-Ceccarelli
Tom Ball
Tim Boudreau
Jesse Glick
Petr Blaha
Ruth Kusterer
Jara Uhrik
xzajo
Jan Lahoda
James Branam
nbextras.org

Sun:
Kazem - bug cartoons ;-)
Tor Norbye
Romain Guy
James Gosling
Chief Gaming Officer
Bill Vass
Jim Grisanzio
Jonathan Schwartz

Planets:
Planet Netbeans
Planet Sun
Planet Eclipse

Other:
netbeans-blog.org
Joel Spolsky
Bruce Eckel

License info

Creative Commons License
This work is licensed under a Creative Commons License.

Recent Entries

Map of visits

Locations of visitors to this page
« Previous day (Jul 25, 2005) | Main | Next day (Jul 26, 2005) »
20050726 Úterý červenec 26, 2005
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.


    Disclaimer: The contents of my blog represent my personal opinions which may differ from official views of my employer, Sun Microsystems.