LOG4J
Log4j:-Logging for java
·
The messages that are generated by application to know
state and flow of execution are called “LOG
messages”.
·
The process of keeping track of applications flow of
execution through log messages is called as “logging operations”.
·
Using SOP statements we can generate log messages or
confirmation messages for logging operations.
·
Performing logging operations with SOP statements
having the following limitations.
1. SOP log
messages will be rendered to console monitor, so when more messages are
rendered the screen scrolls-up and will last the log messages.
2. SOP log
messages cannot be categorized will rendering and can’t be filtered while
retrieving.
3. Rendering
log messages with SOP statement is single threaded operation. So, even though
multiple log messages are generated at a time there will be time delay towards
generating log messages.
4. SOP log
messages cannot be written to files, database software’s and etc.
5. SOP log
messages cannot be rendered having layout [arranging and defining the format of
messages] control.
·
There are multiple API’s to perform logging operations
as alternate to SOP statement.
I.
Logging API.
-- given by sun micro system
as built in features of JDK
II.
Assertion API. --
but not popular because of the limited features.
III.
Log4j. à Given by
apache foundation and it is industries defacto [no officially announced but everyone used] standard to perform
logging operations.
Log4J:-
Type : API
for logging operations.
Vendor : Apache
foundation.
Version : 1.4.X
(compatible with any version of JDK).
Open source API
Jar file that represents log4j: log4j.jar, log4j-<version>.jar.
(add material)
·
We can install log4j software separately by extracting
its zip file or we can work with log4j API related jar file that comes with web
server and application server software installation.
·
In web logic 10.X software installation
C:\bea-home\samples
…….\log4j-1.2.8.jar represents log4j API.
·
In web logic 8.X software installation
D:\bea\web
logic8i\common\lib\log4j.jar represents log4j API.
·
Log4j log messages can be placed in application code
having the following five categories.
1. Debug.
2. Info.
3. Warn.
4. Error.
5. Fatal.
Ø The
priority levels of these log messages are
Debug < info < warn < error
< fatal.
·
The real time projects development replace SOP
statements with log4j API based log messages.
·
Log4j API will be used for logging operations during
development phase of the project. But the generated log messages will be used
by programmer to fix bugs and issues by knowing applications flow/state of
execution in maintenance phase or testing phase of the project.
·
Onsite engineers pass log messages offshore engineers
and these offshore engineers will use these messages to know state of
application execution towards bug fixing.
LOG4J Programming:-
Three
important objects of log4j programming.
1. Logger
object.
2. Appender
object.
3. Logout
object.
1.
Logger
object:-
Ø This object
can enable log4j based logging operations on certain java class and also
provides methods to generate log messages in our application code having
different categories.
Ø Using this
object we can set certain mark to filter log messages while retrieving.
// To
create logger object.
Logger logger=logger.getLogger
(SelectTest.class);
the class on which we want to apply logging operations.
//
to write log messages in application code
Statement st=con.createStatement();
Logger.debug(“proj1.mod1.app1.JDBC
statement object is ready”);
The actual
log message
Other methods:-
logger.info (“………”);
logger.warn (“………”);
logger.error (“………”);
logger.fatal (“………”);
Ø Use debug
level for generating confirmation messages.
Ø Use info
level when java application interacts with non-java resources like files,
database software’s and etc.
Eg: for JDBC connection
object creation.
Ø Uses warn
level if some un expected statements of application code are executed.
Ø Use error
level in known exception handling catch blocks.
Ø Use fatal
level in the catch block that handles unknown exceptions.
try
{
----------
}
catch
(ClassNotFoundException)
{
Logger.error (“proj1.mod1.app2.xxx
class is not found”);
}
catch(Exception e)
{
Logger.fatal(“(proj1.mod1.app2.
some unknown problem is raised”);
}
2.
Appender
Object:-
Ø Using this
object we can decide the destination repository like file, console, Database
s/w and etc to write the log messages.
Ø The LOG4J
API supplies certain built-in appender classes like console appender, jdbc
appender, SMTP appender and etc.
Layout Object:-
·
Using this object we can decide the format and pattern
of log messages.
·
Log4j API gives certain built-in layouts and also
allows the programmer to customize the layout.
·
Log4j API gives the following classes for layout
operations.
1) Simple
layout.
2) HTML
layout.
3) Pattern
layout.
Ø Procedure
to work with log4j based messages in Stand-alone-Application development
environment:-
Step:1 Add the Log4J API related jar file to
class path [collect from web logic software
Installation].
Step:2 create logger object to enable
logging operations on certain java class.
Step:3 create layout object.
Step:4 create
appender object pointing to layout object.
Step:5 Add appender object to logger object.
Step:6 Set logger level to filter log
messages while retrieving the log message.
Step:7 Add log messages of different categories in
your application code.
Step:8 compile and execute the
application.
Note:- If
the logger level is “warn” to retrieve the log messages then application
generates only those log messages whose
priority levels are greater than or equal to
warn.
For the related information on
logger object appender object , layout object refer page numbers 1-5 of the
material
(Material
1-5 pages)
Ø If no logger level is explicitly set the default
logger level to retrieve log messages is debugging.
Ø For log4j enabled JDBC Stand-alone-applications
[example application] refer the supplementary handout given in 12/10/2011.
1.
Code to work with simple layout and file appender:-
Helloworld.java
package
chp1;
import org.apache.log4j.Logger;
import
org.apache.log4j.BasicConfigurator;
public class HelloWorld1
{
static Logger logger=Logger.getLogger(“chp1.HelloWorld1”);
public static void main(String args[])
{
BasicConfigurator.configure();
//configuring log4j for your environment(simple way)
Logger.debug(“HelloWorld”);
}
}
2. Code to
work with HTMLLayout, FileAppender:[gives html file as log file]
Logger
logger=Logger.getLogger(SelectTest.class);
HTMLLayout layout=new
HTMLLayout();
FileAppender appender=new
FileAppender(layout,”log.html”,true);
logger.addAppender(appender);
3. Code to
work with HTMLLayout, FileAppender:
Logger logger=Logger.getLogger(SelectTest.class);
HTMLLayout layout=new HTMLLayout();
FileOutputStream fos=new
FileOutputStream(”log1.html”,true);
WriteAppender appender=new
WriteAppender(layout,fos);
logger.addAppender(appender);
Note: The
WriteAppender is very much similar to FileAppender.
Ø The
Standard slogan is the industry is don’t hard code any values in your
application that are changeable in the future. It is recommended to pass these
details to the application from outside the application either by using
properties files support or xml file support.
Ø To specify
lo4j configurations from outside the application we can use either properties
file or xml file.
Example application specifying the log4j configurations from properties
file:
Log.properties:
#for ConsoleAppender,
SimpleLayout (#indicates comment)
log4j.rootLogger=INFO,
S
log4j.appender.S=org.apache.log4j.ConsoleAppender
log4j.appender.S.layout=org.apache.log4j.SimpleLayout
Note: In this properties
file the property names are fixed but the values can be change
SelectTest.java:
Import
java.sql.*;
Import
org.apache.log4j.*;
Public class
SelectTest
{
Public static
void main(String args[]) throws Exception
{
Logger
logger=Logger.getLogger(SelectTest.class);
//locate the
properties file where log4j configs are placed
PropertyConfigurator.configure(“log.properties”);
//write jdbc
code here
try
{ ……..
………
}
catch(SQLException
Se)
{
………….
}
catch(Exception
e)
{
………….
………….
}
}
}
Note: Make
sure that SelectTest.java and log.properties file are there in same directory.
Ø To know
about various log4j properties & their utilization refer quick start to
using log4j document i.e downloaded to google search engine or www.apache.org website
Log.properties file for FileAppender HTMLLayout:
Log.properties:
#for FileAppender HTMLLayout
log4.rootlogger=DEBUG,S
log4j.appender.S=org.apache.log4j.FileAppender
log4j.appender.S.File=mymsgs.html
log4j.appender.S.Append=false
log4j.appender.S.layout=org.apache.log4j.HTMLLayout
Ø The Rolling File Appender creates backup files (rolling)
when specified main log file content reaches to max capacity.
Rolling
File Appender
abc.txt (log file)
abc.txt 1
abc.txt 2 …………………. abc.txt n
|
………………..
………………..
………………..
…………………..
………………….
|
|
………………..
………………..
…………………
………………...
|
|
…………………
…………………
………………...
…………………
|
|
………………….
………………….
………………….
…………………..
………………….
|
Log4j
properties for RollingFileAppender:
#for RollingFileAppender HTMLLayout
log4j.rootLogger=DEBUG,R
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=Mylog1.html
log4j.appender.R.MaxBackupIndex=10
log4j.appender.R.layout=org.apache.log4h.HTMLLayout
Daily Rolling
File Appender:-
Ø It is
capable of generating logfile on daily basis or hurly basis or Minute basis and
etc.
Ø In this
process the slog files also Maintaining data and time as part of file name.
abc.2011-10-13.txt abc.2011-10-12.txt abc.2011-10-11.txt abc.2011-10-10.txt
|
………………..
………………..
………………..
…………………..
………………….
|
|
………………..
………………..
…………………
………………...
|
|
…………………
…………………
………………...
…………………
|
|
………………….
………………….
………………….
…………………..
………………….
|
Ø The
popularly used Appender in realtime projects is DailyRollingFileAppender
Log4j configuration properties for DailyRollout FileAppender pattern
layout:
Log.properties:
#for DailyRollingFileAppender, PatternLayout
log4j.rootLogger=DEBUG, R
log4j.appender.R=org.apache.log4j.DailyRollingFileappender
log4j.appender.R.File=MyFile.txt
log4j.appender.R.DatePattern=’-‘YYYY-MM-DD-HH-MM
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %r {%t} %c %m %d
%n
other possible values for date pattern property
‘-‘yyyy-mm: Roll log file on
the first of each month
‘-‘yyyy-ww: Roll log file on
the first of each check
‘-‘ yyyy-mm-dd: Roll log file at midnight
of every day
‘-‘yyyy-mm-dd-a: Roll log file at midnight of every
day
‘-‘yyyy-mm-dd-hh: Roll log file every hour
‘-‘yyyy-mm-ddd-hh-mm: every
minute
Ø Pattern
layout allows the programmer to specify the content of log messages in
programmers choice manner with the support of format specifies of shown above
%p-à message priority
(like debug, info &etc).
%ràMilliseconds
since programme started running.
%nà new line.
%màour log
message.
%cà current
class name.
%t à name of
current thread.
Ø When java web
application uses log4j API in its web resource programs to generate log
messages then the log4j API related log4j.2.8.jar must be placed in class path
and must also be placed WEB-INF/lib folder of web application.
Ø In web
environment the log4j properties file should be placed in WEB-INF/CLASSES
folder, but the generated log file will come in the home directory of
underlying web server (for tomcat).
Ø Instead of
properties files the log4j also allows the programmer to use xml file while
specifying log4j configurations form outside the application (but not
recommended to use because of its complexity).
For Example application refer application 6 of page no 10
We can use log4j support in all kinds of java,j2ee s/w based
applications to log massages.
Java Debugging-tool
v The
debugging enabled on the application gives flow of execution and helps the
programmer to analyze the problem and to fix the problem. To perform debugging
we need debugger. This debugger is a tool that can be enabled to perform
application execution by exposing the flow of execution.
v In java
environment JDK software supplies JDB tool & every IDE software supplies
one built-in debugger to perform debugging operations.
v Debugging
can be used for the following operation.
1. To analyze
and fix bugs and issues.
2. To know the
internal execution of predefined method calls.
3. To know
server/container level internal executions in deployable applications
environment like web applications, EJB component and etc..
Note: The f7
& f8 options of TURBO C++, IDE lunches debugger to perform debugging.
TestDemo.java
public class TestDemo
{
public void
x()
{
System.out.println(“TestDemo:x()”);
int a=10;
int b=20;
if(a>=b)
System.out.println(“a>=b);
else
System.out.println(“b>a);
y();
}
public void
y()
{
System.out.println(“TestDemo:y()”);
for(int i=1;i<=5;++i)
System.out.println(i);
}
public
static void main(String args[]) throws Exception
{
thread.sleep(40000);
System.out.println(“Starting
of TestDemo:main(-)”);
TestDemo
td=new TestDemo();
td.x();
System.out.println(“end
of TestDemo:main(-)”);
}
}
Ø While
performing debugging we must set one break point in our application based on
either line number or method name.
Ø Break point
is a position set in application from where the debugger starts performing
debugging.
Procedure
to debug the above application by using JDB tool:-
Step:1 compile source
file c:\app1>javac TestDemo.java
Step:2 launch debugger
[JDB] for debugging
C:\>jdb
< ..i
Initializing
jdb………………
…………………………………….
>run TestDemo
………………
VM started <..I
>stop in
TestDemo.x
………………………………
Main[1] step
…………………[do this repeatedly until application completes its
execution]
………………..
Other break
points: for line
break point
>stop at
TestDemo:8
Ø To perform
step over operations [executing until the current method returns to its calling
routine]
>step up
Ø The IDE software
supplied debuggers having GUI environment to debug the applications.
Procedure to
debug java applications in my Eclipse IDE:
Step: 1 Create java project having
the above application
Fileànewàjava projectàmy projectànextàfinish
[paste TestDemo.java file in src folder]
Step: 2 Set break
point in your application code.
àDouble click in left side margin
pointing to a certain statement of application.
Step: 3 Run applications
in debug mode.
àright click on source code of
applicationàdebug asàjava application àyesàuse step
into, step over, step return options
of debug window to see the flow of execution.
Ø We can
perform “add watch” operation on the
variables and expressions of application to keep track of the value changes
that are happening in the variables and expressions during the execution of the
application.
Ø In My
Eclipse IDE the procedure is launch debug mode environmentàexpression window [windowàshow viewàothers] (if not available use of
window menu) àRight Click
in expression windowà Add watch
expressionà Right
expression like a, a+b, etc.
Procedure
to perform debugging on web application by using My Eclipse IDE:
Step: 1 create web
project.
File menuà new àweb project
àproject name [TestWebProj] àfinish.
Step :2 add one jsp program to the web root folder of above project.
Index.jsp
<b>
from index.jsp</b>
<% for
(int i=1;i<=5;++i)
{
out.println(i);
} %>
Step :3 Deploy the web application in server
a. Configure
tomcat server.
b.
For deployment go to deploy icon of
toolbar project [TestWebProj] àadd àserver
à Finish àok.
Step :4 Create break point in index.jsp.
Step:5 Debug the jsp program.
àapply àdebug.
Procedure to debug stand alone
application in Net beans 6.7.1:-
Step :1 create java project and adding one java application
File menu ànew project àjava àjava
application ànext àTestApp àfinish.
àadd
TestDemo.java to source packages.
Step :2 create break point
in the source code of TestDemo.java
Step :3 debug the
application.
Right click in the source code TestDemo.java àDebug file.
Ø Net beans
IDE automatically enables add watch operation on the variables of the
application.
Ø To add
explicitly add watch operation on certain or expressions
Right click
in variables window ànew watch àadd expression.
Procedure to debug the web resource
program of java web application in Net beans IDE:
Step :1 create web project in Net beans IDE
File menu ànew project
àjava web àweb-application ànext à
[TestWebProj1] ànext àserver [glassfish-2.1] ànext àfinish.
Step :2 add following code in the index.jsp of web
page folder of project .
Step
:3 create break point on the
statement of index.jsp.
Step :4 debug the above jsp program.
Right click In the project àdebug.
Ø The Http
server monitor, Http Client monitor options given in Net beans IDE allows the
programmer to monitor various details that are there in http request and http
response.
Enable those monitors use window menu àdebugging àHttp server monitor, HttpClient
monitor.
Ø The Http
monitor provision is working properly in Net Beans 5.0, 5.5 versions.
Code
Movement in the real world:-
Ø All
developers and tester machines will be there running in windows environment but
all these machines will be connected to a common machine of company called
integrated machine.
Ø This
integrated machine resides in Linux (or) salaries environment having high
configuration and also contains the common software that are required for
multiple projects of the company.
|
|
|
|
|
|
|
|
|
|||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Ø The
Developer/tester communicates with the integrates machine either FTP
application tool or TELNET application tool.
Ø Similarly
integrated machine interacts with developer/tester machine using “Samba Server Tool”.
Ø For each
project multiple domains will be created in the application server software
that is placed on Inte4grated machine.
Ø If project
contains three teams then three domains.
I.
three teams on one for each team.
II.
One domain for project leader for integration of the
project.
III.
one domain for testers to test the project.
Ø The
multiple projects of the company will use the multiple logical databases that
are created in the database software of integrated machine on one per project
basis.
Ø During
development mode of the project, the project will be maintained in CVS
repository or SVN repository. To make the resources of the project visible and
accessible for all the developer of the project more over it is also useful to
pass one developer resources to another developer in the team environment.
Ø The CVS repository keeps track of various
operations that are done on the files by developers by accessing the files from
CVS repository.
Ø CVS
repository keeps track of various modifications done in the file by different
developers by generating versions.
A.java
(original resource)
A.java.1.2
(after second modification)
A.java.1.2
(after third modification)
…………….
…………….
…………….
Some examples
for CVS repository Software’s:-
1. Clear case.
2. CVS NT
3. Win CVS
4. Tortoise
Ø The CVS
repository software will be installed on the installed on the integrated
machine and the IDE software’s of developer machine will be configured to
interact with CVS repository.
Ø The process
of collecting resource or project from CVS repository is called as Checkout
operation. Similarly the process of keeping resource back into CVS repository
after doing modifications is called as check-in operation.
o Note: for each check in operation of the
resource one version will be generated for the resource.
Ø We can use comparison
and merge tool like Araxis, Wdiff to compare multiple versions of each resource
and to merge them into single version or single file.
Ø Every CVS
repository software is version control system using which you can record the
history of source file by keeping track of the modifications.
CVS NT:
Type : CVS Repository software
: Open
source software
Version : 2.x
To download software : www.cvsnt.org
Procedure to
keep CVS NT repository ready for certain project/module :-
Step :1
create one directory in your computer file system
E:\SathyaProj
Step :2 start
the service control panel of CVS NT software pointing to above folder.
Startà programsàCVS NTà service control panel àCVS serviceà start.
CVS lock serviceàstart àRepository tabà add àlocation
browse and select [e:\SathyaProj] àname[/rep1{logical
name}] àok àyes àapply
Note: CVS
repository software’s stores all the versions of a file/resource in a single
file in a clever way that stores differences between versions.
Procedure to perform check-in and check-out operations on the above CVS
NT repository resources from My Eclipse IDE:-
Step :1 create two windows
users from control panel.
Start àsettings àcontrol panel àuser accounts àcreate new account ànew account
[subbu]
Nextàcreate
account àselect
subbu account àcreate
password àtype new
password[*****]
Create password.
Note: also create
another user raja having password raja
Step :2 launch My
Eclipse IDE having new work space for raja programmer having E:\SathyaProj
as the work space folder.
|
|||||
|
|||||
|
|||||
Step :3 configure the above created CVS NT repository
in currently launched MyEclipse IDE for raja programmer.
Window menuàshow view àothers àCVS àselect CVS
editors, repositories àok àgoto àCVS
repository window ànew àrepository location à Host[localhost]àrepository path[/rep1]à user [raja]àpassword [raja] à finish.
Step :4 create java project in My Eclipse
IDE having one.java source file
File menu ànew àjava
project àproject
name [MyTestProj] ànext àfinish.
Test.java
Public class Test
{
Public
static void main(String args[])
{
System.out.println(“hello1”);
}
}
Step :5 place the
above project in CVS NT repository[/rep1]
Right click
on project à team àshare project àselect rep1 repository ànext àfinish.
Step :6 keep
Test.java in CVS repository [/rep1 maintained MyTestProj]
Right click
on Test.java àteam àcommit [like check in operation] àfinish.
Step :7 do more
check in operations on Test.java having more modifications.
Step :8 see the
history of source file[Test.java] [all versions]
Right click
on Test.java àteam àshow history.
Step :9 to compare current file content with the files
of history
Right click
on Test.java àcompare
with àhistory.
Step :10 To update
the content of current file with CVS repository.
Right click
on file (Test.java) àteam àupdate.
Checking out project from CVS repository as an another
user (Remash):-
Step:1 launch My Eclipse IDE with different work space folder.
E:\Ramesh workàok.
Step:2 configure the CVS repository
with Ramesh MyEclipse IDE window.
Window menuà show viewà otherà CVSà CVS repositoryàRight click in CVS
repository windowànewà Repository locationà host [localhost]à Repository path [/rep1] àauthentication user [Rameh] àpassword [Ramesh]àfinish.
Step:3 import/check out project
from CVS repository.
File menuàimport àCVS àprojects from CVSànext àselect rep1 repositoryànext àuse an existing moduleàselect MyTestProjànext àcheck out as a project in
the work spaceànext àfinish
Perform few more check in
operations on Test.java from Ramesh My Eclipse IDE window.
Note: to
make raja programmer getting updating done by Ramesh programmer open raja’s programmers.
My Eclipse IDE windowàRight click on Test.javaàteam àupdate.
Note: to replace current file (test.java) content
with one of the existing old versions of the CVS repository like test.java 1.2
then
Right click on Test.javaàreplace with history àselect version number (1.5) àreplace.\
v If you click on save button
of My Eclipse IDE window then the content will be saved to local repository like
E:\Rameshwork (or) E:\Raja work.
v If you use teamàcommit option then the
modification will be saved to the CVS repository.
v After replacing current file
with old version file if you want to get current file back from local repository
then right click on Test.java àreplace with previous from local history.
Procedure to Configure CVS NT repository in net beans IDE:-
Procedure to convert java
application into .exe file:-
Step:1 develop java application having
main method.
FrameTest.java
import java.awt.*;
import
javax.swing.*;
public class
FrameTest extends JFrame
{
………………
………………
FrameTest()
{
……………
…………… logic into instantiate components
……………
}
public static void main(String args[])
{
new FrameTest();
}
}
Step:2 compile the above .java file
cmd> javac
FrameTest.java
Step:3 create manifest file specifying the class that contains main() method.
MyManifest.mf
Manifest_version
: 1.0
Main_class :
{space} FrameTest ß(enter key)
note: this
manifest file helps the JRE to find out the class that contains main() method
based on which the application execution has to be started in jar file based
application execution.
MyManifest.mf
(save along with FrameTest.java)
any<file name>.<exe> can be taken as manifest
file
step:4 prepare jar file representing the above
application by also including manifest file .
cmd>jar cmf MyManifest.mf test.jar * (*
or. Should be used)
gives Test.jar representing the application.
·
Download J2E software as
J2E_FREE.ZIP file from the following url.
http://www.regexlab.com/download/noc/jar2exe
·
Extract the above zip file to get the setup file of the above J2E
software.
·
Install J2E software.
·
Launch the J2E software supplied jar2exe wizard from desktop to convert
the above test.jar file to exe file.
Launch
wizard ànext àbrowse and select test.jar àselect jre1.6 version
[minimum] ànext àΘ windows GUI application ànext àmain java class to start
running à[Frameset] àselect image for background
space window[C:\....]{browse} ànext à{tick mark}hide class file ànext àselect dependent jar files
if there [like ojdbc14.jar] ànext àchoose the name and location
of the exe file [c:\desktop\text.exe{browse}] ànext finish.
Ø We can convert any stand
alone & desktop applications as exe file to make java application as
end-users friendly to run the application.
ANT [Another Neat Tool]
Ø Batch file is used to
automate set of operations that are required to complete the task by using DOS
commands.
Problem:-The remembering command names & their sequence of execution is
complex process.
Ex: task1 à (1st time)
>date
>time
>dir
task1à(2nd time)
> date
>time
>dir
Solution: - prepare batch file having above command & run that batch file for
multiple times.
run.bat
date
time
dir
task1 (1st
time) >run.bat
task1 (2nd time)
>run
task (task time) > run
Ø Building application is
nothing but performing set of operations which keep application ready for
execution. These operations are like compiling resources creating deployment
directory structure, adding jar files to class path, preparing war and jar file
deleting temporary files and etc…
Ø In module integration or
project integration we need to perform the above said build operations for
multiple times until the integration takes place perfectly.
Ø Instead of performing above
said build operations manually is a sequence for multiple times, it is
recommended to automate them by using build tool and generate single command to
perform all build operations .so that we can use that single command for
multiple times for repetitive build operations In project.
Build tool:
ant |for java environment.
maven |
MS-build } for
.net environment
ANT is a java based build
tool that can automate complicated & repetitive operations of application build process (or) module/project integration process.
ANT
Type : build tool for java environment
Vendor : apache foundation.
Version : 1.x (compatible with any JDK
software)
Open
source.
Ø To install and setup ant
tool separately and explicitly refer page number-1 of ant tool document/booklet.
Ø To understand the need of ant
tool refer the page number-1 of the booklet.
Ø While working with ant tool
,we need to write instructions in build file automating our application build
process or project/module integration process.
Ø The build file is an xml
file ,if no file is explicitly specified the ant tool looks to take build.xml
as default build file
CMD> ant ànow ant tool looks to take
the build.xml file as default build file.
CMD>ant build file abc.xml ànow ant tool takes abc.xml
as build file àany <filename>.xml can act as build file.
Ø
To write instructions in build file we need to use set of predefined
xml tags which are self descriptive.
Ø
Each task related instructions of build file should be specified as
target having logical name and we can specify dependency between these targets.
Ø
Set of targets in build file together is called as project.
Ant tool can
be used to perform the following operations:-
·
For compiling java resources.
·
For creating deployment directory structure.
·
For adding jar files to class path.
·
For preparing jar, war, ear & etc… files.
·
For deployment of applications/components in servers.
·
For starting and stopping servers.
·
For executing standalone applications.
·
For performing copy, move and delete operations on resources and etc..
Flow of execution in build file:-
Build file (xml file) (abc.xml)
|
<project
name =”p1” default =”t3”>
|
Ø Most of the java environment
based IDE software’s internally uses ant built tool to build and execute
applications.
Ø Before we write build script
in build file we must understand the existing resources and their locations.
In build file
<property
name=”a” value=”hello”> àdeclares “a” variable with
value “hello”
${a} àgives “hello” value.
Ø Before writing build script
in build.xml we must have complete idea on existing directories and their
resources.
E:\APPs
|------->ant-web
|--------->lib
| |---->ojdbc14.jar
|--->DBApp.war (93 line no)
|--->web.xml
|--->DBServlet.java
|--->query.html
|--->build.xml
|--->build
|---->WEB-INF
| |----->classes
| | |------>DBServlet.class
| |--->lib
| | |---->ojdbc14.jar
| |--->web.xml
|---->Query.html
E:\APPs
|------->StrutsApp
|--------->jars
| |---->*.jar (10+1)
|--->src
| |----->RegisterAction.java
| |----->RegisterForm.java
|--->failure.jsp
|--->register.jsp
|--->success.jsp
|--->web.xml
|--->struts-config.xml
|--->struts-html.tld
|--->build.xml
|--->build
|---------------->WEB-INF
| |----->classes
|--->success.jsp | |------>RegisterAction.class
|--->failure.jsp
| |------>RegisterForm.class
|--->success.jsp |--->lib
| |------------>*.jar(11)
|--->struts-html.tld
|--->web.xml
|--->struts-config.xml