How To Write Build.Xml Together With Operate Gear Upwards Inwards Apache Ant

This is the minute article on Apache ANT tutorials for beginners series As I bring e'er said that I similar the curt , clear in addition to concise tutorial which tells virtually few concept but inward a clear in addition to concise way in addition to position weight on fundamentals . I seek to adopt same theory spell writing my weblog ship service spell writing my sense coupled amongst the concept which is of import for a software developer betoken of view.

Here I am answering around of the basic questions related to installing emmet , running emmet , creating a build.XML file , debugging build.xml inward the instance of whatever consequence .

These questions bring been asked past times my students spell pedagogy them JAVA in addition to related technology  inward my early on career.

How practise I run ant?
To run yous demand to download emmet in addition to install on your machine , in addition to then create environs 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 yous tin opened upward ascendance prompt in addition to type ant.

If yous teach this output, agency emmet binaries is non inward your path

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

Otherwise, yous volition teach output which volition complain virtually 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 inward Apache ANThere is a sample build.xml yous only demand to know of import chemical factor e.g. projection ,target ,property in addition to business in addition to the guild inward which unlike 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 encounter what nosotros are doing hither :

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


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

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

Here nosotros are declaring in addition to specifying belongings ,you tin country variable every belongings has at to the lowest degree ii attributes “name” in addition to “value” , though yous tin define your all properties inward a split properties file in addition to charge from in that location 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 yous e'er role belongings inward your build.xml instead of using difficult coded values inward target for directory rear etc , this volition laissez passer yous flexibility to alter the value anytime without changing at many places (in instance yous 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 inward projection tag, hence if nosotros don’t specify this target our create volition neglect to country “target non found”.

”name” attribute specified rear of target. “depends ontarget” says that earlier executing this target executed commencement “clean” in addition to and then “compile”
<echo>Doing all</echo>

This volition impress message inward 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 , yous tin bring every bit many target yous desire based on your need.

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


delete business or tag is used to delete directory, file etc, verbose=true makes it to impress message spell deleting inward cosole, <fileset> is an of import tag in addition to I recommend yous to read inward item somewhere inward emmet manual or may move I volition explicate inward item sometime because it include “patternset” which supports pattern 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 rear 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 yous encounter employment on your create or yous are getting exception related to findiing files/directory or anything in addition to then yous would similar to know what’s going behind in that location are ii pick , run emmet on verbose pick , it volition impress lots of item (I don’t prefer this) because of hence much unwanted information but tin move usefule inward sure as shooting situation.

Second in addition to my preffered way is proficient quondam “echo” way . role echo business to impress values of properties, variables or printing uncomplicated message to depository fiscal establishment check the run flow.

hither are around 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 role file other than build.xml?

Normally when yous run emmet from whatever directory from ascendance prompt it volition hold back for file called build.xml inward electrical flow directory; if it doesn’t notice the file it volition laissez passer error. If yous bring file named “custom_build.xml” yous tin instruct emmet to role this file for edifice your application past times using pick “-f” e.g. emmet –f custom_build.xml

Hope this would move useful permit me know if yous bring whatever questions, dubiety etc volition move happy to answer.

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

Some other Tutorial yous may like

Komentar

Postingan populer dari blog ini

Common Multi-Threading Mistakes Inwards Coffee - Calling Run() Instead Of Start()

3 Examples Of Parsing Html File Inwards Coffee Using Jsoup

Why You Lot Should Command Visibility Of Shape Too Interface Inward Java