NetBeans Quick Tip #4 - Extending the Build Process



In today's tip I will show you how to extend the NetBeans build process on a simple example.

Some background first - NetBeans utilizes Ant as it's primary project engine. So when you build any project the IDE executes Ant. Other Java IDEs use as primary project systems different engines, developed specially for these IDEs. That was the case with NetBeans 3.x as well but with NetBeans 4.0 it was changed to Ant which is de facto a standard for building Java applications.

Ant is used similarly to GNU make to automate the build process. It can also handle build dependencies but unlike make it is completely plaftorm and shell independent (it's java-based), it's configuration is XML-based and can be extended easily. You can read more about Ant in it's user manual.

Back to the tip - let's say I want to copy the jar file which is built with my project to a network share - for instance to publish it as a bleeding edge development build of the application. I want to create a backup copy of it's sources as well. Such tasks can be automated with NetBeans and Ant very easily.

To do this I need to override the build.xml file which is stored in the main directory of my project. The build.xml file by default just includes a file called build-impl.xml, located in nbproject subdirectory. Build-impl should not be touched (it is generated automatically), instead the targets in build.xml need to be overriden.

So I override the "-post-compile" target to create an archive with my sources using tar and gzip tasks. I want Ant to create a subdirectory called by current date and name the archive according to the date as well. To do that I at first initialize the DSTAMP variable using a task called <tstamp/>. I also override the "-post-jar" target to copy the jar which is created automatically in the dist directory:

   <target name="-post-compile">
        <tstamp/>
        <tar tarfile="${dist.dir}/sources.tar" basedir="${src.dir}"/>
        <gzip zipfile="${dist.dir}/sources.tar.gz" src="${dist.dir}/sources.tar"/>
        <copy file="${dist.dir}/sources.tar.gz"            
                 tofile="h:\\shared\\myapplication\\${DSTAMP}\\myapplication-sources-${DSTAMP}.tar.gz"/>
        <delete file="${dist.dir}/sources.tar"/>     
        <delete file="${dist.dir}/sources.tar.gz"/>
    </target>
    <target name="-post-jar">
        <copy file="${dist.jar}/" tofile="h:\\shared\\myapplication\\${DSTAMP}\\myapplication-${DSTAMP}.jar"/>
    </target>

Once I build the main project I get the following output:


From now on everytime I build the project a copy of the jar and an archive with soures are created in a directory with current date on a network share. To achieve that I only needed to add few lines into the build.xml file (yeah, I know, you need to know which lines to add... ;-) If you didn't play with Ant yet, I suggest to try it, because it can help you automate a lot of tasks you may be doing manually. It's sometimes funny how much time we can give away to avoid doing a manual task - but with Ant this mostly pays off. Really.
Comments:

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