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

Advertisement

Masukkan script iklan 970x90px

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

Jumat, 14 Agustus 2020

This is the minute article on Apache ANT tutorials for beginners series As I accept ever said that I similar the curt , clear in addition to concise tutorial which tells almost few concept but inwards a clear in addition to concise trend in addition to set weight on fundamentals . I elbow grease to adopt same theory acre writing my weblog post service acre 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 inwards the instance of whatever effect .

These questions accept been asked yesteryear my students acre didactics them JAVA in addition to related technology  inwards my early on career.

How exercise I run ant?
To run you lot demand to download emmet in addition to install on your machine , in addition to then 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 upwards ascendence 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 equally an internal or external command,operable programme or batch file.

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



How exercise 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 merely demand to know of import chemical factor e.g. projection ,target ,property in addition to chore in addition to the social club 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 run into what nosotros are doing hither :

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


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

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

Here nosotros are declaring in addition to specifying holding ,you tin nation variable every holding has at to the lowest degree ii attributes “name” in addition to “value” , though you lot tin define your all properties inwards a divide properties file in addition to charge from at that spot equally good .<property> denotes ant’s holding task, which exercise accept another attribute e.g. location to specify location of whatever properties file. I recommend that you lot ever role holding inwards your build.xml instead of using difficult coded values inwards target for directory advert etc , this volition give you lot flexibility to modify the value anytime without changing at many places (in instance you lot accept difficult coded it).

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

Here nosotros are defining a target since nosotros accept already called target “all” equally default inwards projection tag, thus if nosotros don’t specify this target our create volition neglect to nation “target non found”.

”name” attribute specified advert of target. “depends ontarget” says that earlier executing this target executed starting fourth dimension “clean” in addition to and then “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 erstwhile create earlier edifice novel 1 , you lot tin accept equally 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 acre deleting inwards cosole, <fileset> is an of import tag in addition to I recommend you lot to read inwards item somewhere inwards emmet manual or may locomote I volition explicate inwards item 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 yesteryear 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 in addition to likewise copies resources e.g. images, nosotros tin likewise create jolt file using ant, which nosotros are non doing hither merely for simplicity.

Imporant matter hither is holding ${ant.project.name} this is builtin propety provided yesteryear emmet in addition to its’s value is advert of projection defined yesteryear 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 exercise I debug build.xml?
if you lot run into occupation on your create or you lot are getting exception related to findiing files/directory or anything in addition to then you lot would similar to know what’s going behind at that spot are ii alternative , run emmet on verbose alternative , it volition impress lots of item (I don’t prefer this) because of thus much unwanted information but tin locomote usefule inwards sure enough situation.

Second in addition to my preffered way is expert erstwhile “echo” way . role echo chore to impress values of properties, variables or printing elementary message to cheque the locomote flow.

hither are around illustration of using echo

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



How exercise I enforce emmet to role file other than build.xml?

Normally when you lot run emmet from whatever directory from ascendence prompt it volition expect for file called build.xml inwards electrical flow directory; if it doesn’t uncovering the file it volition give error. If you lot accept file named “custom_build.xml” you lot tin learn emmet to role this file for edifice your application yesteryear using alternative “-f” e.g. emmet –f custom_build.xml

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

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

Some other Tutorial you lot may like