Twitter

Sponsors

NetBeans and Ant, a great combination

16 Nov
2009
We love NetBeans! It neatly fills our needs for developing php applications. It does seem to have some minor annoyances, but at least one of them can easily be overcome by using Ant. NetBeans with php allows you to designate a local web server for testing. The IDE automatically will copy files to the web server… well, most of the time. Sometimes, for whatever reason, NetBeans, doesn’t know if it needs to copy files. This often happens with our image files. It always will copy a file if you modify it, but this doesn’t work very well for files like images that aren’t normally modified in the IDE. Furthermore, it is annoying as mentioned earlier. You can update your files quickly by building a simple Ant script to copy your local files to your web server or another location.

For this tutorial, we will assume that you know about NetBeans, and how to create a new project. We are planning a couple of tutorials on using NetBeans for WordPress development in the future. These will cover more basics of using NetBeans.

If you would like to try NetBeans, go to http://netbeans.org/ to download the newest version. They have many different configurations depending on what your needs are. We use the php version.

tools On top of what comes in the php version of NetBeans, you will need to add the Ant plugin. The plugins dialog is located under the tools menu.
plugins Next, click on the available plugins tab. If you click on the name column head, the plugins will sort alphabetically putting Ant at the top. Simply check the checkbox next to Ant and click the Install button at the bottom of the dialog. That’s it.
So, how do you use this? NetBeans will automatically recognize any Ant build file. So all we have to do, is create an Ant build file. The default build file is usually named build.xml.

Simply, create a new empty file named build.xml and add the following code.

<project name="Netbeans push" default="copyLocal" basedir=".">
    <description>
        pushes files to local web server
    </description>

    <property name="local.web" location="C:\xampplite\htdocs" />

    <target name="copyLocal" description="Copy site locally for testing">
        <copy todir="${local.web}\testing">
            <fileset dir="${basedir}">
                <exclude name="**\.svn\**"/>
                <exclude name="**\nbproject\**"/>
            </fileset>
        </copy>
    </target>
</project>
 

    A quick review of the file.

  • It simply starts with project, everything except for the name is optional. The default attribute names the default target. Basedir simply defines a property for a basedir that can be used later.
  • The description should be self-explanatory.
  • The property statement allows us to create a variable that we can use repeatedly in our code. Obviously, in this example, it actually adds typing, but you can imagine the time savings if you have a large build file with multiple targets.
  • The target is the meat of the file. This is what gets called when the build file runs. Targets can even depend on other targets.
    The name must be unique within the file.
    The copy line inside of the target is called a task. There are many ways to write almost all tasks. In this case, we are simply saying that we want to copy everything in our base directory to our local web server. As you can see, we can put any property inside of ${ } to use it within attributes. Additionally in this example, we have added a couple of exclude statements that are common in our work. The first ignores any Subversion files. Since we use Subversion as our Versioning system, it isn’t necessary to copy these to our local web server. The second ignores all of the Netbeans project files. You may or may not have your project files with the projects.
runtarget All that’s left is to use the file. In Netbeans, you can access the targets in multiple ways. The most common access points for us, is to right click on the file itself in the project window and click run target.
navigator You can also view targets inside of the navigator window. Right clicking on the target you want to run, and selecting run is all there is to it.
output You should see the results in your output window. This will also display any errors in your build file if you run into problems.
For more information on Ant, visit http://ant.apache.org/

As a final note, it is likely that Netbeans is caching information regarding the modification times of the files to accomplish its copy to server functionality. In our experience, we have not seen any problems, but you should proceed with caution.

Comment Form