站内搜索: 请输入搜索关键词

当前页面: 开发资料首页Netbeans 专题Creating Shortcuts to Your Custom ANT Tasks

Creating Shortcuts to Your Custom ANT Tasks

摘要: Creating Shortcuts to Your Custom ANT Tasks

I've been anticipating the release of Netbeans 4.0 for a long time, and this release is full of some very powerful and dramatic changes including refactoring, ANT project tasks, and windowing system improvements. However, sometimes it's the small things that can make a big difference. For me that small thing is the ability to create shortcuts to run your customized ANT scripts. I haphazardly stumbled upon this feature as I was modifying an ANT script that I was creating. I realized that this is a simple, yet powerful way of extending the IDE, without digging into the APIs. I don't have the most spectacular laptop for development, and trying to run an IDE, a database and a web server can really hinder development productivity. I do have access to a fairly nice test server, however, and using this new feature in Netbeans, I was able to create a shortcut to my customized ANT script that compiles my application, packages it into a war file, and deploys it to my test server in seconds. Now I just use my laptop to run my IDE and a Web Browser. One of the other nice features of Netbeans 4.0 is that project system is now based on ANT. A nice side effect of having the ANT project system based on ANT is that you can take advantage of the pre-built project ANT scripts to create your own custom scripts. Many of the basic tasks are already created for you, such as compiling and packaging the application into a war or a jar file.

Here is the ANT script that I wrote to deploy my application to my test server.

<?xml version="1.0" encoding="UTF-8"?>
<project name="deploy-to-test-server" default="all" basedir=".">
    <property name="remote.output.dir"
value=":/server/deploydir"/>
    <property name="password" value="testserverpassword"/>
    <!--include the build-impl.xml file to take advantage
    of the pre-built tasks -->
    <import file="nbproject/build-impl.xml"/>

    <!-- my custom task depends on the pre-built tasks
    in the project ANT script-->
    <target name="deploy-to-test" depends="compile,dist">
        <scp
            file="${dist.war}"
            todir="${remote.output.dir}"
            trust="yes"
            password="${password}"
        />
    </target>
</project>

I was able to take advantage of the pre-built tasks in the project ANT script by including

<import file="nbproject/build-impl.xml">

I used the secure copy task to upload my application war file to my deployment directory on my test server. I also made the task depend on the compilation, and distribution tasks in the prebuilt ANT project script.

Once your custom task has been created, it is really easy to create a shortcut to run your ANT script. Just expand the tree node on your ANT script to display all of the ANT targets in your script. Right click on the target that you want to create a shortcut for.

You can create a keyboard shortcut, a menu item shortcut, or a toolbar shortcut. Just check the type of shortcut that you want to create. Depending on the type of shortcut you've created, you will either enter a key sequence for a keyboard shortcut or a label for your menu shortcut or toolbar shortcut. Now running your ant task is as simple as clicking a button on your toolbar, selecting a menu item or typing a key sequence. This is just one simple example of what you can do with ANT script shortcuts. I could have easily written the same ANT script in the previous version of Netbeans and accomplished the same task, but the ability to launch the task from a shortcut is really convenient, and being able to take advantage of the prebuilt ANT script can really simplify the creation of custom ANT scripts. You could easily create shortcuts to run your unit tests, or copy resource files into your classpath. The possibilities are limitless.


↑返回目录
前一篇: Consuming Web Services in NetBeans IDE 4.1(2)
后一篇: Customize Your NetBeans Java Template to Fit Your Need