Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Compiling with Ant

Options
  • 05-04-2002 10:41am
    #1
    Closed Accounts Posts: 1,651 ✭✭✭


    Just posting this cause I use it a lot and I thought it might be helpful. Someone may find some use for it at least.

    This is just a basic Ant buildfile that I use, it can be used for pretty much
    any project. All it really does is compile and package your sources but it does
    it all in one neat process.

    You can get Ant at http://jakarta.apache.org/ant

    All you need to do is put ant.jar and optional.jar into your classpath, and the ant scripts into your path. From the directory where the buildfile is, just type 'ant' at the command line and it should take care of everything for you, if you've got your properties set up.

    build.xml
    <?xml version='1.0'?>
    
    <project name="Project Name" default="clean" basedir=".">
    
       <!-- ======================================================== -->
       <!-- SET PROPERTIES                                           -->
       <!-- Reads the properties from file called build.prop         -->
       <!-- Properties in the format 'propName=propValue'            -->
       <!-- ======================================================== -->
       <property file="build.prop"/>
       
       <!-- ======================================================== -->
       <!-- PERFORM INITIALISATION                                   -->
       <!-- Creates a tmp directory and copies .java files to it     -->
       <!-- ======================================================== -->
       <target name="prepare">
          <mkdir dir="${build}"/>
          <copy todir="${build}">
             <fileset dir="${javasrc}">
    	    <include name="**/*.java"/>
             </fileset>
          </copy>
       </target>
    
       <!-- ======================================================== -->
       <!-- COMPILE JAVA SOURCES                                     -->
       <!-- Compiles Java source files in tmp directory              -->
       <!-- ======================================================== -->
       <target name="compile" depends="prepare">
          <javac srcdir="${build}" destdir="${build}"/>        
       </target>
    
       <!-- ======================================================== -->
       <!-- CREATE JAR                                               -->
       <!-- Jars the compiled classes                                -->
       <!-- ======================================================== -->
       <target name="jar" depends="compile">
          <jar jarfile="${jarfile}" basedir="${build}" excludes="**/*.java"/>
       </target>
    
       <!-- ======================================================== -->
       <!-- CLEANUP                                                  -->
       <!-- Deletes the tmp directory                                -->
       <!-- ======================================================== -->
       <target name="clean" depends="jar">
          <delete dir="${build}"/>
       </target>
    </project>
    


    build.prop
    build=tmp
    javasrc=c:/project/src
    jarfile=g:/jakarta-tomcat-4.0.1/webapps/project/WEB-INF/lib/library.jar
    


    For more info on Ant tasks:
    http://jakarta.apache.org/ant/manual/index.html

    Hope someone finds this useful.


Comments

  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    nice. Actually I think a few people here use Ant (I've actually written a few ANT tasks as well).


    A couple of things I'd change on that...

    1. change build.prop to build.properties

    2. I'd also add the following property.
    <property environment="env"/>

    This will give you acccess to all the system environment vars. Eg ${env.CLASSPATH}

    3. I normally use the following target process as my base line.

    [init]
    Clean up temp work directories and create output directories.

    [javac]
    Compile java code

    [unit test]
    Run unit tests on the java code.

    [jar]
    Create the jars of all the code.

    [javadoc]
    Create the javadoc for the java code and pull in any documentation files (eg. html, txt, licenses)

    [zip]
    Zip the lot to a seperate directory so I have a loose release and zipped release.

    [functional test]
    Run functional tests on the release to ensure it's ok.


  • Closed Accounts Posts: 1,651 ✭✭✭Enygma


    Thanks!


    <property environment="env"/>

    Didn't know about that, will come in handy I'm sure.


  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    np.

    Oh yea.

    I would also add a [clean] target which has no depends or targets dependant on it. This would clean up the build environment so I have the absolute minimum of stuff (eg. what should be in the source tree).

    Also I tend not to put source control information into the ANT task (if I had to it would go between [init] and [javac]). The reason for this is I try to build my source tree as the fully working build environment. That way all a person has to do is pull down the whole source tree from CVS and type ANT in the root.


  • Registered Users Posts: 4,676 ✭✭✭Gavin


    I use NetBeans for most of my work.. and am going to start onto using CVS and ANT.

    How does ANT handle sorting out web application archives ?

    Cheers,
    Gav


  • Closed Accounts Posts: 1,651 ✭✭✭Enygma


    There is a <war> task, or you could just use the <jar> task.


  • Advertisement
Advertisement