Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Apache Ant


Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Apache Ant

  1. 1. Ali Bahu12/24/2012APACHE ANT
  2. 2. INTRODUCTION A build tool can be used to automate certain repetitive tasks forexample compiling source code, running software tests andcreating files and documentation for the software deployment. Build tools typically run without a graphical user interface. Popular build tools used by Java developers are Apache Ant andMaven.
  3. 3. APACHE ANT OVERVIEW Apache Ant (Ant) is a general purpose build tool. Ant is an abbreviation for“Another Neat Tool”. Ant is primarily used for building and deploying Java projects. A Java build process typically includes: the compilation of the Java source code into Java bytecode creation of the .jar file for the distribution of the code creation of the Javadoc documentation Ant uses an xml file for its configuration. This file is usually called build.xml.Ant builds are based on three blocks: tasks, targets and extension points. A task is a unit of work which should be performed and are small, atomicsteps, for example compile source code or create Javadoc. Tasks can begrouped into targets. A target can be directly invoked via Ant. Targets can specify theirdependencies. Ant will automatically execute dependent targets. For exampleif target A depends on B, than Ant will first perform B and then A. You canspecify the default target in your build.xml file. Ant will execute this target, ifno explicit target is specified.
  4. 4. HOW TO USE APACHE ANT Create a Java Project called in Eclipses.Create a package called test.package test;public class MyTest{public int multi(int number1, int number2){return number1 * number2;}}package test;import test.MyTest;public class Main{public static void main(String[] args) {MyTest test = new MyTest();System.out.println("Result is: " + test.multi(5,10));}}
  5. 5. CREATE BUILD.XML FILE & RUN FROMECLIPSE Click on the build.xml file to review and understand it. Running from Eclipse i.e. Running From Command Line:Open a command line and switchto your project directory. Type in"ant" or "ant -f build.xml" to startthe build process.
  6. 6. APACHE ANT CLASSPATH Ant also allows to create classpath containers and use them intasks. The following build_classpath.xml from a project demonstrates this concept: To print the classpath for debugging:<!-- Write the classpath to the console. Helpful for debugging --><!-- Create one line per classpath element--><pathconvert pathsep="${line.separator}" property="echo.classpath" refid="junit.class.path"></pathconvert><!-- Write the result to the console --><echo message="The following classpath is associated with junit.class.path" /><echo message="${echo.classpath}" />
  7. 7. RUNNING JUNITS VIA ANT Ant allows to run JUnit tests. Ant defines JUnit task. You only need toinclude the junit.jar and the compiled classes into the classpath forAnt and then you can run JUnit tests. Below is an example: You can run this JUnit unit test via the following build_junit.xml. Thisexample assumes that the JUnit jar "junit.jar" is located in “lib” folder.package test;import math.MyMath;import org.junit.Test;import staticorg.junit.Assert.assertEquals;public class MyMathTest {@Testpublic void testMulti() {MyMath math = new MyMath();assertEquals(50, math.multi(5, 10));}}
  8. 8. ANT TIPS Eclipse has an ant editor which make the editing of ant file very easy byproviding syntax checking of the build file. Eclipse has also a ant view. In thisview you execute ant files via double-clicking on the target. Apache Ant allows to convert relative paths to absolute paths i.e.<property name="my.config" value="../my-config.xml"/><makeurl file="${my.config}" property="my.config.url"/> Ant can be used to replace text based on regular expressions i.e.<target name="regular-expressions"><!-- Replace tabs with two spaces --><replaceregexp flags="gs"><regexp pattern="(t)" /><substitution expression=" " /><fileset dir="${outputtmp.dir}/"><include name="**/*" /> </fileset></replaceregexp></target>