How To Write Build.Xml Together With Operate Cook Inward Apache Ant

This is the minute article on Apache ANT tutorials for beginners series As I bring ever said that I similar the brusk , clear in addition to concise tutorial which tells close few concept but inwards a clear in addition to concise mode in addition to lay weight on fundamentals . I elbow grease to adopt same theory piece writing my weblog postal service piece writing my sense coupled amongst the concept which is of import for a software developer betoken of view.

Here I am answering to a greater extent than or less of the basic questions related to installing emmet , running emmet , creating a build.XML file , debugging build.xml inwards the instance of whatever upshot .

These questions bring been asked past times my students piece didactics them JAVA in addition to related technology  inwards my early on career.

How practise I run ant?
To run you lot demand to download emmet in addition to install on your machine , thence create surroundings variable ANT_HOME in addition to include ANT_HOME/bin into your PATH similar below.

In Windows path=%path%;%ANT_HOME%/bin
In Linux    PATH =${PATH}:${ANT_Home}/bin


Now you lot tin opened upwardly ascendancy prompt in addition to type ant.

If you lot larn this output, agency emmet binaries is non inwards your path

C:\Documents in addition to Settings>ant
'ant' is non recognized every bit an internal or external command,operable plan or batch file.

Otherwise, you lot volition larn output which volition complain close create file if it doesn’t exist.
  
    C:\Documents in addition to Settings>ant
    Buildfile: build.xml does non exist!
    Build failed



How practise I write build.xml file?
Apache ANT tutorials for beginners serial How to write build.xml in addition to run create inwards Apache ANThere is a sample build.xml you lot only demand to know of import chemical factor e.g. projection ,target ,property in addition to chore in addition to the lodge inwards which dissimilar target gets executed to start amongst basic create procedure.

<?xml version="1.0"?>
<project name="test" default="all" basedir=".">
  <property name="src"   value="src"/>
  <property name="build" value="build"/>
  <property name="lib"   value="lib"/>

<target name="all" depends="clean, compile" description="Builds the whole project">
    <echo>Doing all</echo>
  </target>

<target name="Clean" description="Removes previous build">
    <delete verbose="true">
      <fileset dir="${build}"/>
    </delete>
  </target>

<target name="compile" depends="clean" description="compile whole project">
    <echo>compile ${ant.project.name} </echo>
    <copy file="${src}/splashscreen.jpeg" tofile="${build}/splashscreen.jpeg"/>
    <javac srcdir="${src}" destdir="${build}" includes="Test.java"/>
  </target>
</project>


lets come across what nosotros are doing hither :

<project name="test" default="all" basedir=".">


This business defines our project; every create file must bring this line. The projection advert is “test” defined past times attribute “name”; default target is “all” piece running “ant” ascendancy from ascendancy prompt if nosotros don’t specify whatever target than emmet executed this default target.
basedir tells which is the overstep marking directory for creating the create inwards this instance its electrical flow directory (from where you lot run emmet command) , denoted past times point “.” .

<property name="src"   value="src"/>

Here nosotros are declaring in addition to specifying belongings ,you tin tell variable every belongings has at to the lowest degree 2 attributes “name” in addition to “value” , though you lot tin define your all properties inwards a split upwardly properties file in addition to charge from at that topographic point every bit good .<property> denotes ant’s belongings task, which practise bring another attribute e.g. location to specify location of whatever properties file. I recommend that you lot ever purpose belongings inwards your build.xml instead of using difficult coded values inwards target for directory advert etc , this volition order you lot flexibility to alter the value anytime without changing at many places (in instance you lot bring difficult coded it).

<target name="all" depends="clean, compile" description="Builds the whole project">

Here nosotros are defining a target since nosotros bring already called target “all” every bit default inwards projection tag, thence if nosotros don’t specify this target our create volition neglect to tell “target non found”.

”name” attribute specified advert of target. “depends ontarget” says that earlier executing this target executed kickoff “clean” in addition to thence “compile”
<echo>Doing all</echo>

This volition impress message inwards console every bit “doing all”


<target name="Clean" description="Removes previous build">

This is target “Clean” which volition delete quondam create earlier edifice novel i , you lot tin bring every bit many target you lot desire based on your need.

<delete verbose="true">
      <fileset dir="${build}"/>
</delete>


delete chore or tag is used to delete directory, file etc, verbose=true makes it to impress message piece deleting inwards cosole, <fileset> is an of import tag in addition to I recommend you lot to read inwards special somewhere inwards emmet manual or may locomote I volition explicate inwards special sometime because it include “patternset” which supports designing matching of directory/files via its includes in addition to excludes attribute which is extremely useful to filter unwanted files (generally meta information files shape CVS, SVN etc).

Here nosotros are deleting create directory past times using value of belongings “build”, ${build} denotes value of whatever property.

<target name="compile" depends="clean" description="compile whole project">
    <echo>compile ${ant.project.name} </echo>
    <copy file="${src}/splashscreen.jpeg" tofile="${build}/splashscreen.jpeg"/>
    <javac srcdir="${src}" destdir="${build}" includes="Test.java"/>
  </target>
</project>


This is our compile target ,which compiles our code in addition to too copies resources e.g. images, nosotros tin too create jounce file using ant, which nosotros are non doing hither only for simplicity.

Imporant affair hither is belongings ${ant.project.name} this is builtin propety provided past times emmet in addition to its’s value is advert of projection defined past times attribute “name” of proejct tag.

<copy> tag is used to re-create files in addition to <javac> tag is used to compile coffee code .

How practise I debug build.xml?
if you lot come across work on your create or you lot are getting exception related to findiing files/directory or anything thence you lot would similar to know what’s going behind at that topographic point are 2 selection , run emmet on verbose selection , it volition impress lots of special (I don’t prefer this) because of thence much unwanted information but tin locomote usefule inwards for sure situation.

Second in addition to my preffered way is skillful quondam “echo” way . purpose echo chore to impress values of properties, variables or printing elementary message to banking concern check the run flow.

hither are to a greater extent than or less instance of using echo

<echo>Doing all</echo>
<echo message="Now creating directory origin "/>
<echo level="warning" message ="Active configuration (config.active property) is non laid - using default." />



How practise I enforce emmet to purpose file other than build.xml?

Normally when you lot run emmet from whatever directory from ascendancy prompt it volition await for file called build.xml inwards electrical flow directory; if it doesn’t abide by the file it volition order error. If you lot bring file named “custom_build.xml” you lot tin teach emmet to purpose this file for edifice your application past times using selection “-f” e.g. emmet –f custom_build.xml

Hope this would locomote useful allow me know if you lot bring whatever questions, incertitude etc volition locomote happy to answer.

Further Learning
Maven Fundamentals past times Bryan Hansen
Java Web Fundamentals
Apache Ant Fundamentals By Rusty Lowrey

Some other Tutorial you lot may like

Belum ada Komentar untuk "How To Write Build.Xml Together With Operate Cook Inward Apache Ant"

Posting Komentar

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel