How To Write Build.Xml As Well As Piece Of Job Construct Inwards Apache Ant

This is the 2nd article on Apache ANT tutorials for beginners series As I convey ever said that I similar the brusk , clear together with concise tutorial which tells nearly few concept but inwards a clear together with concise fashion together with set weight on fundamentals . I endeavour to adopt same theory piece writing my spider web log postal service piece writing my sense coupled amongst the concept which is of import for a software developer signal of view.

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

These questions convey been asked past times my students piece didactics them JAVA together with related technology  inwards my early on career.

How do I run ant?
To run you lot demand to download emmet together with install on your machine , together with hence create surround variable ANT_HOME together with 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 plough over the axe opened upward dominance prompt together with type ant.

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

C:\Documents together with Settings>ant
'ant' is non recognized equally an internal or external command,operable programme or batch file.

Otherwise, you lot volition larn output which volition complain nearly construct file if it doesn’t exist.
  
    C:\Documents together with Settings>ant
    Buildfile: build.xml does non exist!
    Build failed



How do I write build.xml file?
Apache ANT tutorials for beginners serial How to write build.xml together with run construct inwards Apache ANThere is a sample build.xml you lot merely demand to know of import chemical component subdivision e.g. projection ,target ,property together with business together with the lodge inwards which dissimilar target gets executed to start amongst basic construct 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 run across what nosotros are doing hither :

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


This describe defines our project; every construct file must convey this line. The projection advert is “test” defined past times attribute “name”; default target is “all” piece running “ant” dominance from dominance prompt if nosotros don’t specify whatever target than emmet executed this default target.
basedir tells which is the move past times score directory for creating the construct 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 together with specifying holding ,you tin plough over the axe country variable every holding has at to the lowest degree 2 attributes “name” together with “value” , though you lot tin plough over the axe define your all properties inwards a dissever properties file together with charge from in that location equally good .<property> denotes ant’s holding task, which do convey another attribute e.g. location to specify location of whatever properties file. I recommend that you lot ever purpose holding inwards your build.xml instead of using difficult coded values inwards target for directory advert etc , this volition plough over you lot flexibility to alter the value anytime without changing at many places (in instance you lot convey difficult coded it).

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

Here nosotros are defining a target since nosotros convey already called target “all” equally default inwards projection tag, hence if nosotros don’t specify this target our construct volition neglect to country “target non found”.

”name” attribute specified advert of target. “depends ontarget” says that earlier executing this target executed rootage “clean” together with and hence “compile”
<echo>Doing all</echo>

This volition impress message inwards console equally “doing all”


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

This is target “Clean” which volition delete former construct earlier edifice novel 1 , you lot tin plough over the axe convey equally many target you lot 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 piece deleting inwards cosole, <fileset> is an of import tag together with I recommend you lot to read inwards item somewhere inwards emmet manual or may last I volition explicate inwards item sometime because it include “patternset” which supports blueprint matching of directory/files via its includes together with excludes attribute which is extremely useful to filter unwanted files (generally meta information files shape CVS, SVN etc).

Here nosotros are deleting construct directory past times using value of holding “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 together with too copies resources e.g. images, nosotros tin plough over the axe too create jounce file using ant, which nosotros are non doing hither merely for simplicity.

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

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

How do I debug build.xml?
if you lot run across occupation on your construct or you lot are getting exception related to findiing files/directory or anything together with hence you lot would similar to know what’s going behind in that location are 2 selection , run emmet on verbose selection , it volition impress lots of item (I don’t prefer this) because of hence much unwanted information but tin plough over the axe last usefule inwards surely situation.

Second together with my preffered way is practiced former “echo” way . purpose echo business to impress values of properties, variables or printing uncomplicated message to depository fiscal establishment check the move flow.

hither are roughly illustration of using echo

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



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

Normally when you lot run emmet from whatever directory from dominance prompt it volition await for file called build.xml inwards electrical flow directory; if it doesn’t honor the file it volition plough over error. If you lot convey file named “custom_build.xml” you lot tin plough over the axe learn emmet to purpose this file for edifice your application past times using selection “-f” e.g. emmet –f custom_build.xml

Hope this would last useful permit me know if you lot convey whatever questions, doubtfulness etc volition last 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


Sumber https://javarevisited.blogspot.com/

0 Response to "How To Write Build.Xml As Well As Piece Of Job Construct Inwards Apache Ant"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel