ProjectBasicsDemo

Project Basics Demo

This demo script is part of the NetBeans World Tour 2008 session #2, Using NetBeans For Your Existing Projects.

Main Points to Hit

  • NetBeans projects are Ant based
  • It is very easy to extend the build process
  • It is not rocket-science, you can explore and modify the projects yourself

Prerequisities

  • JDK5 or JDK6
  • NetBeans 5.5 (or higher)

Setup

  • Make sure ant is on your PATH (you can execute ant command from command line).

Demo Steps

This demo will introduce the audience to the Ant build script generated by NetBeans and the steps to customize it.

Introduce the NetBeans Generated Scripts

  1. Create a new Anagram Game sample project.
  2. Run the application so that people see what it does. Ask them to guess the first word :)
  3. Close the application and show the output window. Explain that this is output of ant. We can see which tasks are being executed during build and running of the application.
  4. Switch to the Files view and open the build.xml. Explain it's contents - it includes the build-impl.xml script which is the default IDE script, for the rest this script is empty except for the comments. The comments explain how to extend the ant script. You add your custom tasks here, not into the impl script.
  5. Open the nbproject/build-impl.xml script. Explain that this is what NetBeans is using on background, so whenever you run, test, compile, debug, etc. your application the tasks from this script are executed. Obviously this script is quite complex but that shouldn't bother you, you can override the tasks in build.xml or define new tasks.

Customizing the Build

Here we'll show how to extend the build process - say we want to provide a zip of the application on demand. Similarly we could create a task to copy the file to remote server, obfuscate code, commit something into CVS, etc. The possibilities are limitless :)

  1. We will create a zip task which will create a zip with current sources next to this jar.
  2. Create a new target in the build.xml file (note, providing a description causes the task to appear in the top level Run Target list, rather than the "Other Targets" sub-menu):
<target name="create-zip" description="Zips the source" depends="init"> <mkdir dir="${dist.dir}"/> <zip basedir="${src.dir}" zipfile="${dist.dir}/anagram-src.zip" /> </target>
  1. Run the task by right-clicking on the build.xml file and choosing Run Target | create-zip.
  2. A zip file is create in the dist directory. Expand the zip file node in the IDE to show it's contents.
  3. Delete the zip file. Now we will show how to make this step a part of the build process so that you don't have to run the target manually.
  4. Show the section of the build.xml file which explains individual -pre and -post targets - it's in the comments near the beginning. Explain the hooks for extending the build process.
  5. Change the code of the zip target to:
<target name="-post-compile" description="Zips the source"> <mkdir dir="${dist.dir}"/> <zip basedir="src" zipfile="${dist.dir}/anagram-src.zip" /> </target>
  1. Explain that the zip will be created after compilation. We could hook it elsewhere into the build process, e.g. after building the jar file.
  2. Mention that there is currently no zip file in the dist directory. Run build of the application.
  3. The zip file was created again, but this time as a part of the regular build process.
  4. We are still not happy because the zip file gets overwritten everytime we run the task. It might be useful to save one zip per day to archive the sources. So for that we will change the name of the zip to contain current date:
<tstamp> <format property="timestamp" pattern="yy-MM-dd-hh:mm:ss" /> </tstamp> <target name="-post-compile" description="Zips the source"> <mkdir dir="${dist.dir}"/> <zip basedir="src" zipfile="${dist.dir}/anagram-src-${timestamp}.zip" /> </target>
  1. The tstamp task generates the timestamp property. We can choose the pattern then the name is set in the zip task. Run the build.
  2. The zip was created and current date is part of the filename. We are happy :)

Running Outside of NetBeans

Show that you can run the ant script outside of the IDE as well

  1. Switch to console.
  2. Go to project directory - the one where you can find build.xml.
  3. Run "ant clean", "ant jar" and "ant run". Explain what happens - that you are executing the targets like the IDE would. That's the advantage of Ant - you are not dependent on the IDE to compile/run/deploy/etc. your application.

Demo Cleanup

  • Delete the Anagram Game project
Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo