Interview Questions

ANT

Maven Plugin - war

Use the following maven war plugin snippet to be used in pom file

 <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <escapeString>\</escapeString>
                    <webResources>
                        <resource>
                            <directory>src/main/resources/packaged</directory>
                            <targetPath>WEB-INF/classes</targetPath>
                            <filtering>true</filtering>

Maven Plugin - Clean

Use the following maven snippet to clean existing build files

<plugin>
                <artifactId>maven-clean-plugin</artifactId>
                <version>2.5</version>
                <configuration>
                    <filesets>
                        <fileset>
                            <directory>${user.home}/${project.build.finalName}/index</directory>
                           
                        </fileset>
                        <fileset>
                                        <directory>${tomcat.path}\webapps\<war-folder-name></directory>

Maven Plugin - Tomcat Start

Use the following maven plugin snippet to start tomcat

  <plugin>
  <groupId>org.apache.tomcat.maven</groupId>
  <artifactId>tomcat7-maven-plugin</artifactId>
  <version>2.1</version>
  <configuration>
    <path>C:\Program Files\Apache Software Foundation\Tomcat 7.0\</path>
  </configuration>
</plugin>

ANT Build Execution from Java

Build Engineers are usually faced with requirements to notify the development team whenever a build is successfully completed or if there is an exception in build. ANT framework provides a simple API to do this. Have a look at the following code.

AntBuild.java

/**
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS

ANT Script to execute JUnit

To execute JUnit classes using ANT build, use the following ANT build script.

build.xml

<?xml version="1.0" encoding="UTF-8"?>
<project basedir="." default="all" name="common">

<path id="test.classpath">
        <pathelement location="${basedir}/src/main/webapp/WEB-INF/classes"/>
       
        <fileset dir="${basedir}/src/main/webapp/WEB-INF/lib">
                <include name="**/*.jar"/>
        </fileset>
</path>

<target name="test">
        <junit haltonfailure="yes">
                        <formatter usefile="true" type="plain"/>
                        <classpath refid="test.classpath"/>

ANT Tutorial

Software Build :
Software build is the process of converting the source files in to executables or software artifacts which can be deployed in different machine.

A simple software build comprises the below steps.
1. Fetch the Source files from the versioning system
2. Compile the Source code to make executable programs
3. Package the executables and the supported configuration files
4. Test case execution
5. Application deployment

ANT – Usage of <Property> task in Ant Build

Properties

In any project, there will be set of properties which can be set pre-configured so that the project can be customized. This is just like assigning a value to the key so that the key can be used every where in the project.

In ANT, this is achieved using property task. The property task takes "name" and "value" as attributes.

We can define a name in the property and assign the value so that it can be used anywhere in the Ant build. The value once set cannot be changed. It’s immutable.

ANT - Introduction to Ant build Targets

In this article, let us discuss about target in ANT build.

Target

Target is a container which performs set of tasks.

Let us take an example of the below Java program.

AntBuildTest Java Class

public class AntBuildTest
{
        public static void main(String[] args)
        {
                System.out.println("My First Java Program deployed using ANT build target...");
        }
}

In order to deploy the Java application, the following are the set of activities need to be performed.

ANT - Simple Ant Build script

In this example, let us see how to create a simple ANT Build XML.

Perform the following steps,
1. Create a Java Project
2. Create a source directory named “src”.
3. Create a “build.xml” file as given below and place inside the Java Project.
4. Right click the build.xml and run as Ant Build. This will compile the Java class and places the class file inside a directory named “classes” directory. Then it executes the Java program.

AntBuildTest Java Class

public class AntBuildTest
{
        public static void main(String[] args)
        {

ANT - Load Properties file

In the previous article we have seen how to use Property in ANT build xml for compiling and deploying the Java application.

Now, let us see how to group all these properties in a file and load them in the ANT build xml in order deploy the Java application (which is discussed earlier in the previous article).

Create a build.properties file as given below. This properties file has all the property object needed for the ANT build script.

build.properties
sourceDir=src