Posted By

jerryvig on 09/03/11


Tagged

java apache build ant buildxml


Versions (?)

Minimal Apache Ant build.xml


 / Published in: XML
 

This is a minimal apache ant build.xml file that will quickly do all the things that you will generally want to do, including compiling and building a simple Java project to class files and jars. It provides targets to compile the class files and jars and to run the main class. The fields marked "My Project Name Here" and "My Main Class Here" are the fields that need to be modified for your project. To start create a directory for your project with subdirectories "lib" and "src" and place the build.xml file in the same directory. Place the Java sources in the "src" directory. Type "ant build" to compile and "ant run" to test and run the main class.

build build.xml lib src

  1. <project name="My Project Name Here" basedir="." default="main">
  2.  
  3. <property name="lib.dir" value="lib"/>
  4.  
  5. <path id="classpath">
  6. <fileset dir="${lib.dir}" includes="**/*.jar"/>
  7. </path>
  8.  
  9. <property name="src.dir" value="src"/>
  10.  
  11. <property name="build.dir" value="build"/>
  12. <property name="classes.dir" value="${build.dir}/classes"/>
  13. <property name="jar.dir" value="${build.dir}/jar"/>
  14.  
  15. <property name="main-class" value="My Main Class Name Here"/>
  16.  
  17. <target name="clean">
  18. <delete dir="${build.dir}"/>
  19. </target>
  20.  
  21. <target name="compile">
  22. <mkdir dir="${classes.dir}"/>
  23. <javac srcdir="${src.dir}" destdir="${classes.dir}" classpathref="classpath"/>
  24. </target>
  25.  
  26. <target name="jar" depends="compile">
  27. <mkdir dir="${jar.dir}"/>
  28. <jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
  29. <manifest>
  30. <attribute name="Main-Class" value="${main-class}"/>
  31. </manifest>
  32. </jar>
  33. </target>
  34.  
  35. <target name="run" depends="jar">
  36. <java fork="true" classname="${main-class}">
  37. <classpath>
  38. <path refid="classpath"/>
  39. <path location="${jar.dir}/${ant.project.name}.jar"/>
  40. </classpath>
  41. <arg value="RIMM" />
  42. </java>
  43. </target>
  44.  
  45. <target name="clean-build" depends="clean,jar"/>
  46.  
  47. <target name="main" depends="clean,run"/>
  48.  
  49. </project>

Report this snippet  

You need to login to post a comment.