Interview Questions On Java,Java EE


Enter your email address:



Add to Technorati Favorites
Google PageRank Checker
Showing posts with label ANT. Show all posts
Showing posts with label ANT. Show all posts

Thursday, October 8, 2009

How is ANT Property Task used?

In order to to set the Ant properties, the <property> task is used. Once property value is set, it cannot be changed. It means they are immutable.In order to set a property to a specific value, the name and value pair is used.

Example

<property name="project.name" value="IQJava"/>

If you want to set a property to a location then name/location assignment is used.
To use the properties surround them with ${}.

Example

<property location="destination" name="dest.dir"/>
<property name="war.dir" location="${dest.dir}/${project.name}"/>

A build.xml as shown in following code box displays how effectively property tasks can be used within a build.xml:
<project basedir="." default="all"> <property name="gensrc.dir" value="gen_src"/> <property name="release.dir" value="release"/> <property name="src.dir" value="../src"/> <property name="lib.dir" value="../externals/lib"/> <property name="class.dir" value="classes"/> <property name="class.dir.lib" value="${class.dir}/lib"/> <property name="class.dir.src" value="${class.dir}/src"/> <import file="one-jar-ant-task.xml"/> <!-- Deletes the directories and files....--> <target name="clean"> <delete dir="${gensrc.dir}"/> <delete dir="${class.dir}"/> <delete> <fileset dir="${lib.dir}" > <include name="**/xyz.jar"/> </fileset> <fileset dir="${release.dir}" > <include name="**/abc.jar"/> </fileset> </delete> </target> <!-- Prepare the Directories needed for code generation --> <target name="prepare"> <mkdir dir="${class.dir}"/> <mkdir dir="${gensrc.dir}"/> <mkdir dir="${class.dir.lib}"/> <mkdir dir="${class.dir.src}"/> </target> <!-- Compile the source code available in src folder --> <target name="compilesrc"> <javac srcdir="${src.dir}" destdir="${class.dir.src}" target="1.4" source="1.4" debug="off" deprecation="on" optimize="off" encoding="iso-8859-1"> <classpath > <fileset dir="${lib.dir}" includes="**/*.jar"/> </classpath> </javac> <copy todir="${class.dir.src}"> <fileset dir="${src.dir}" includes="*.properties"/> </copy> </target> <!-- Generate the lib jar file --> <target name="lib"> <jar manifest="manifest.mf" destfile="${lib.dir}/xyz.jar" includes="**/*.class" basedir="${class.dir.lib}" /> </target> </project>
Continue reading...

How to integrate ANT tool with Eclipse IDE?

ANT Eclipse IDE(latest edition of Eclipse IDE is Galileo) integration is a very simple process as summarized in following steps:

1. As a first step, create a Java Project in Eclipse IDE.

2. Associate a build.xml with this project and ensure you put this XML at the same location as shown in package structure in Eclipse IDE :
(Click images to enlarge)


The code of build.xml is as given below:
<?xml version="1.0" ?> <project name="IQJava" default="execute"> <target name="init" depends="clean"> <mkdir dir="build/classes" /> </target> <target name="compile" depends="init"> <javac srcdir="src" destdir="build/classes" /> </target> <target name="execute" depends="compile"> <java classname="com.iqjava.ant.AntExample" classpath="build/classes" /> </target> <target name="clean"> <delete dir="build" /> </target> </project>


3. Run this build XML



4. The output of this execution will look like as given below:


Continue reading...

Wednesday, October 7, 2009

What is ANT and how can one configure it?

ANT is a Java based build tool and helps in automating whole of build process of a Java project.
ANT uses an XML build file and by default it looks for a build file named build.xml from the directory it has been executed.

A build.xml contains several targets e.g. creating or deleting directory structure, post compilation creation of jar or ear files, extracting/unzipping jar/ear files etc.One target can depend upon one or more targets and it can contain one or more tasks.

One can download Apace Ant 1.7.1 from here.Once that is done, extract it any of your local drive. Set the system variables as follows:

ANT_HOME =D:\apache-ant-1.7.1 (Here I have extracted the ANT ZIP at D drive)
PATH = %ANT_HOME%\bin

To ensure proper installation of ANT, go to command prompt and execute the command "ant - version", you will see the installed ant version.

The execution of ANT file is done from the directory where build.xml resides and simply typing ant command on console and hit enter will start the execution of build file.

The build.xml looks like as given below:
<?xml version="1.0" ?>
<project name="IQJava" default="compress"> <target name="compile"> <javac srcdir="com/iqjava/log4j"/> <echo> Compilation Complete! </echo> </target> <target name="compress" depends="compile"> <jar destfile="IQJava.jar" basedir="." includes="*.class" /> <echo> Building .jar file Complete! </echo> </target> </project>
In the build.xml,<project> element is the root element, name attribute of this represents the name of the project.

<target> element can exist multiple times within project element and each one of them represents a single stage. In the above build.xml, we have compile target element which contains two tasks, one - <echo> and the other <javac> which builds java files.

The other target element 'compress' is dependent upon compile target element. It creates a jar file which includes all the .class files in current directory.

The echo tasks just display messages on console.
Continue reading...
 

Disclaimer
Interview Questions On Java,Java EE Copyright © 2017. Reads: best tracker