Using Ant to build J2EE Applications www.scmGalaxy.com Rajesh Kumar [email_address]
Contents Introduction How does ANT work ? Sample Build file Built-in properties ANT – Different flows Writing your own task Command-line options IDE Integration References
Introduction What Is Ant? A build tool like ‘make’ Open source –  from the Apache Jakarta project –  http://ant.apache.org/ Implemented in Java Used to build many open source products
Introduction ..contd Ease of use –  Ant is extended using Java classes –  The configuration files are XML-based, calling out a target tree where various tasks get executed –  Same config file (build.xml) can be across multiple platorms
How does ANT Work ? Ant commands (or tasks) are implemented by Java classes –  many are built-in –  others come in optional JAR files –  custom commands can be created Each project using Ant will have a build file –  typically called build.xml since Ant looks for this by default Each build file is composed of targets –  these correspond to common activities like compiling and running code Each target is composed of tasks –  Each task is run by an object that implements a particular Task interface –  executed in sequence when the target is executed –  like make, Ant targets can have dependencies –  for example, modified source files must be compiled before the application can be run
How does ANT Work ? .. contd Targets to be executed –  can be specified on the command line when invoking Ant –  if none are specified then the default target is executed –  execution stops if an error is encountered –  so all requested targets may not be executed Each target is only executed once –  regardless of the number of other targets that depend on it –  for example •  the “test” and “deploy” targets both depend on “compile” •  the “all” target depends on “test” and “deploy” but “compile” is only executed once when “all” is executed Some tasks are only executed when they need to be –  for example, files that have not changed since the last time they were compiled are not recompiled
Sample Build file < project  name=“test&quot; default=“hello&quot;> < target  name=“hello&quot; depends=“setup, pre-hello1, pre-hello2“> < echo > Hello World</ echo > </ target > < target  name=“setup”> < property  name=“company.name” value=“MindTree”/> < condition  property=&quot;os.is.solaris&quot;> <os name=&quot;SunOS&quot; /> </ condition > </ target > < target  name=“pre-hello1” if=&quot;os.is.solaris“> < echo > You are running this script in Solaris </ echo > </ target > < target  name=“pre-hello2” unless=&quot;os.is.solaris“> < echo > You are NOT running this script in Solaris </ echo > </ target > </ project >
Sample Build file Save the file as test.xml in some temporary folder ( Say C:\Temp) Set ANT_HOME= C:\Jakarta-Ant-1.5 Set PATH=%PATH%;%ANT_HOME%\bin Cd C:\Temp ant –buildfile test.xml Buildfile: test.xml setup: pre-hello1: pre-hello2: [echo]  You are NOT running this script in Solaris hello: [echo]  Hello World BUILD SUCCESSFUL Total time: 1 second
Built-in Properties Ant provides access to all system properties and also has some additional properties. the JVM version Ant detected;  ant.java.version  The name of the project that is currently executing; it is set in the name attribute of <project>.   ant.project.name  The version of Ant ant.version  The absolute path of the buildfile.   ant.file  The absolute path of the project's basedir (as set with the basedir attribute of <project>).   basedir
Ant – Different flows Using “depends” Using “antcall” Using “ant”
Ant – Different flows Using “depends” – Last task to first task Eg : <target name=&quot;compile&quot; depends=&quot;init, setup&quot; description=&quot;compile the source &quot; >  <!-- Compile the java code from ${src} into ${build} -->  <javac srcdir=&quot;${src}&quot; destdir=&quot;${build}&quot;/>  </target>
Ant – Different flows Using “antcall” – Sequential & Functional oriented Calling different targets in the same build.xml (very similar to calling functions in regular programming language) Eg : <antcall target=&quot;copymodule&quot;> <param name=&quot;module.name&quot; value=&quot;user&quot;/> </antcall> <target name=&quot;copymodule&quot; if=&quot;gws.prepared&quot;> <echo>Module : ${module.name} </echo> <copy todir=&quot;${gws.app}/j2ee-apps/gws/${module.name}&quot;  includeEmptyDirs=&quot;no&quot;> <fileset dir=&quot;${gws.class.folder}&quot;> <patternset> <include name=&quot;**/${module.name}/**&quot;/> </patternset> </fileset> </copy> </target>
Ant – Different flows Using “ant” This is used for running scripts for sub-projects  Eg : <target name=&quot;ROOT&quot;> <ant dir=&quot;${basedir}/ROOT&quot; target=&quot;dist&quot;/> </target> <target name=&quot;examples&quot;> <ant dir=&quot;${basedir}/examples&quot; target=&quot;dist&quot;/>   <ant antfile=&quot;subproject/subbuild.xml&quot; dir=“${basedir}/subproject&quot; target=&quot;compile&quot;/>  </target>
Core & Optional tasks  http://ant.apache.org/manual/index.html
Writing your own task Create a Java class that extends  org.apache.tools.ant.Task   For each attribute, write a  setter  method. Implement the interface  org.apache.tools.ant.TaskContainer   if your task contains other tasks as nested elements   Write a  public void execute  method, with no arguments, that throws a  BuildException Adding your task to the system Make sure the class that implements your task is in the classpath when starting Ant.  Add a  <taskdef>  element to your project. This actually adds your task to the system.  Use your task in the rest of the buildfile Eg: <?xml version=&quot;1.0&quot;?>  <project name=&quot;OwnTaskExample&quot; default=&quot;main&quot; basedir=&quot;.&quot;>    <taskdef name=&quot;mytask“ classname=&quot;com.mydomain.MyVeryOwnTask&quot;/>  <target name=&quot;main&quot;>  <mytask message=&quot;Hello World! MyVeryOwnTask works!&quot;/>  </target>  </project>
Command line options ant [options] [target [target2 [target3] ...]]  Options:  -help    print this message  -projecthelp  print project help information  -version  print the version information and exit  -diagnostics  print information that might be helpful to diagnose  or report problems.  -quiet, -q  be extra quiet  -verbose, -v  be extra verbose  -debug  print debugging information  -emacs  produce logging information without adornments  -logfile <file>   use given file for log    -l <file>  ''  -logger <classname>  the class which is to perform logging  -listener <classname>  add an instance of class as a project listener  -buildfile <file>  use given buildfile  -file <file>  ''    -f <file>  ''  -D<property>=<value>  use value for given property  -propertyfile  taking precedence  -inputhandler <class>  the class which will handle input requests  -find <file> <name>  load all properties from file with -D properties  search for buildfile towards the root of the  filesystem and use it
IDE Integration Ant can be integrated with the following Java IDEs –  Jbuilder –  IntelliJ Idea –  Eclipse See the Ant User Manual for more details –  in  http://ant.apache.org/manual/index.html
References Home –  http://ant.apache.org/ FAQ –  http://ant.apache.org/faq.html Mailing Lists http://marc.theaimsgroup.com/?l=ant-user&r=1&w=2 http://archives.apache.org/eyebrowse/SummarizeList?listId=5 Books  Java Development with Ant -  http://www.manning.com/hatcher/ Ant: The Definitive Guide  -  http://www.oreilly.com/catalog/anttdg/ Related Projects : Maven -  http://jakarta.apache.org/turbine/maven/ Centipede -  http://www.krysalis.org/centipede/ Tools built over Ant :  AntHill -  http:// www.urbancode.com/projects/anthill/default.jsp CruiseControl -  http://cruisecontrol.sourceforge.net/
Thank You ! www.scmGalaxy.com

Using Ant To Build J2 Ee Applications

  • 1.
    Using Ant tobuild J2EE Applications www.scmGalaxy.com Rajesh Kumar [email_address]
  • 2.
    Contents Introduction Howdoes ANT work ? Sample Build file Built-in properties ANT – Different flows Writing your own task Command-line options IDE Integration References
  • 3.
    Introduction What IsAnt? A build tool like ‘make’ Open source – from the Apache Jakarta project – http://ant.apache.org/ Implemented in Java Used to build many open source products
  • 4.
    Introduction ..contd Easeof use – Ant is extended using Java classes – The configuration files are XML-based, calling out a target tree where various tasks get executed – Same config file (build.xml) can be across multiple platorms
  • 5.
    How does ANTWork ? Ant commands (or tasks) are implemented by Java classes – many are built-in – others come in optional JAR files – custom commands can be created Each project using Ant will have a build file – typically called build.xml since Ant looks for this by default Each build file is composed of targets – these correspond to common activities like compiling and running code Each target is composed of tasks – Each task is run by an object that implements a particular Task interface – executed in sequence when the target is executed – like make, Ant targets can have dependencies – for example, modified source files must be compiled before the application can be run
  • 6.
    How does ANTWork ? .. contd Targets to be executed – can be specified on the command line when invoking Ant – if none are specified then the default target is executed – execution stops if an error is encountered – so all requested targets may not be executed Each target is only executed once – regardless of the number of other targets that depend on it – for example • the “test” and “deploy” targets both depend on “compile” • the “all” target depends on “test” and “deploy” but “compile” is only executed once when “all” is executed Some tasks are only executed when they need to be – for example, files that have not changed since the last time they were compiled are not recompiled
  • 7.
    Sample Build file< project name=“test&quot; default=“hello&quot;> < target name=“hello&quot; depends=“setup, pre-hello1, pre-hello2“> < echo > Hello World</ echo > </ target > < target name=“setup”> < property name=“company.name” value=“MindTree”/> < condition property=&quot;os.is.solaris&quot;> <os name=&quot;SunOS&quot; /> </ condition > </ target > < target name=“pre-hello1” if=&quot;os.is.solaris“> < echo > You are running this script in Solaris </ echo > </ target > < target name=“pre-hello2” unless=&quot;os.is.solaris“> < echo > You are NOT running this script in Solaris </ echo > </ target > </ project >
  • 8.
    Sample Build fileSave the file as test.xml in some temporary folder ( Say C:\Temp) Set ANT_HOME= C:\Jakarta-Ant-1.5 Set PATH=%PATH%;%ANT_HOME%\bin Cd C:\Temp ant –buildfile test.xml Buildfile: test.xml setup: pre-hello1: pre-hello2: [echo] You are NOT running this script in Solaris hello: [echo] Hello World BUILD SUCCESSFUL Total time: 1 second
  • 9.
    Built-in Properties Antprovides access to all system properties and also has some additional properties. the JVM version Ant detected; ant.java.version The name of the project that is currently executing; it is set in the name attribute of <project>. ant.project.name The version of Ant ant.version The absolute path of the buildfile. ant.file The absolute path of the project's basedir (as set with the basedir attribute of <project>). basedir
  • 10.
    Ant – Differentflows Using “depends” Using “antcall” Using “ant”
  • 11.
    Ant – Differentflows Using “depends” – Last task to first task Eg : <target name=&quot;compile&quot; depends=&quot;init, setup&quot; description=&quot;compile the source &quot; > <!-- Compile the java code from ${src} into ${build} --> <javac srcdir=&quot;${src}&quot; destdir=&quot;${build}&quot;/> </target>
  • 12.
    Ant – Differentflows Using “antcall” – Sequential & Functional oriented Calling different targets in the same build.xml (very similar to calling functions in regular programming language) Eg : <antcall target=&quot;copymodule&quot;> <param name=&quot;module.name&quot; value=&quot;user&quot;/> </antcall> <target name=&quot;copymodule&quot; if=&quot;gws.prepared&quot;> <echo>Module : ${module.name} </echo> <copy todir=&quot;${gws.app}/j2ee-apps/gws/${module.name}&quot; includeEmptyDirs=&quot;no&quot;> <fileset dir=&quot;${gws.class.folder}&quot;> <patternset> <include name=&quot;**/${module.name}/**&quot;/> </patternset> </fileset> </copy> </target>
  • 13.
    Ant – Differentflows Using “ant” This is used for running scripts for sub-projects Eg : <target name=&quot;ROOT&quot;> <ant dir=&quot;${basedir}/ROOT&quot; target=&quot;dist&quot;/> </target> <target name=&quot;examples&quot;> <ant dir=&quot;${basedir}/examples&quot; target=&quot;dist&quot;/> <ant antfile=&quot;subproject/subbuild.xml&quot; dir=“${basedir}/subproject&quot; target=&quot;compile&quot;/> </target>
  • 14.
    Core & Optionaltasks http://ant.apache.org/manual/index.html
  • 15.
    Writing your owntask Create a Java class that extends org.apache.tools.ant.Task For each attribute, write a setter method. Implement the interface org.apache.tools.ant.TaskContainer if your task contains other tasks as nested elements Write a public void execute method, with no arguments, that throws a BuildException Adding your task to the system Make sure the class that implements your task is in the classpath when starting Ant. Add a <taskdef> element to your project. This actually adds your task to the system. Use your task in the rest of the buildfile Eg: <?xml version=&quot;1.0&quot;?> <project name=&quot;OwnTaskExample&quot; default=&quot;main&quot; basedir=&quot;.&quot;> <taskdef name=&quot;mytask“ classname=&quot;com.mydomain.MyVeryOwnTask&quot;/> <target name=&quot;main&quot;> <mytask message=&quot;Hello World! MyVeryOwnTask works!&quot;/> </target> </project>
  • 16.
    Command line optionsant [options] [target [target2 [target3] ...]] Options: -help print this message -projecthelp print project help information -version print the version information and exit -diagnostics print information that might be helpful to diagnose or report problems. -quiet, -q be extra quiet -verbose, -v be extra verbose -debug print debugging information -emacs produce logging information without adornments -logfile <file> use given file for log -l <file> '' -logger <classname> the class which is to perform logging -listener <classname> add an instance of class as a project listener -buildfile <file> use given buildfile -file <file> '' -f <file> '' -D<property>=<value> use value for given property -propertyfile taking precedence -inputhandler <class> the class which will handle input requests -find <file> <name> load all properties from file with -D properties search for buildfile towards the root of the filesystem and use it
  • 17.
    IDE Integration Antcan be integrated with the following Java IDEs – Jbuilder – IntelliJ Idea – Eclipse See the Ant User Manual for more details – in http://ant.apache.org/manual/index.html
  • 18.
    References Home – http://ant.apache.org/ FAQ – http://ant.apache.org/faq.html Mailing Lists http://marc.theaimsgroup.com/?l=ant-user&r=1&w=2 http://archives.apache.org/eyebrowse/SummarizeList?listId=5 Books Java Development with Ant - http://www.manning.com/hatcher/ Ant: The Definitive Guide - http://www.oreilly.com/catalog/anttdg/ Related Projects : Maven - http://jakarta.apache.org/turbine/maven/ Centipede - http://www.krysalis.org/centipede/ Tools built over Ant : AntHill - http:// www.urbancode.com/projects/anthill/default.jsp CruiseControl - http://cruisecontrol.sourceforge.net/
  • 19.
    Thank You !www.scmGalaxy.com