Saturday, July 4, 2015

Invoking Java class through Ant

Ant is a powerful tool which can run Java programs as well as part of your build process. This might be helpful in running a utility class during the build. It can also be handy to do smoke testing by running some of the test classes during the build. Literary you can perform any operation during the build if you are able to call a Java class.

This post, I will discuss creating a dedicated target to run a Java class.

Create Java Project

Let's create a sample Java project with a single class file with the main method so that it can be invoked from an external tool (i.e. Ant).




/**
 * Sample Java class
 * @author Siddheshwar
 *
 */
public class RunMe{
 public static void main(String[] args){
     System.out.println("Inside main method of RunMe");
            if(args.length !=0 ){
              System.out.println("you passed argument="+args[0]);
            }  
       }
} 

build.xml

<?xml version="1.0"?>
<project name="AntSample" default="compile">
 <!--Declare common properties  -->
 <property name="src.dir"     value="src"/>
 <property name="build.dir"   value="build"/>
 <property name="RunMe-class"  value="RunMe"/>
 <property name="classes.dir" value="${build.dir}/classes"/>

 <!-- init target to clean up existing build directory -->
 <target name="clean">
  <delete dir="${build.dir}"/>
 </target>

 <!--Before running a Java class; that class should be compiled   -->
 <target name="compile" depends="clean">
  <echo>compilation started -</echo>
  <mkdir dir="${classes.dir}"/>
  <javac srcdir="src" destdir="build/classes" />
  <echo>compilation complete!</echo>
 </target>

 <!--Execute the java class   -->
 <target name="execute" depends="compile">
  <java classname="RunMe" classpath="build/classes">
   <arg value="hello" />
   <arg file="." />
  </java>
 </target>

 <!-- abstracts clean and execute target -->
 <target name="main" depends="clean,execute"/>
</project>


Executing Java class

In build.xml file execute target is declared to run the Java class. Before running the class it should have been compiled and that's the reason for adding dependency on compile target. This way when we run execute target, it will run compile first. Target, execute provides all information required for running a program. It provides name of the class (i.e. className) as well as path of class through classpath. It also passes an argument to the main method having value hello. 
The last argument file, tells Ant to resolve that attribute to an absolute file location. 

That's it; you are done!

Now let's run the class: 
I have given below multiple options for running the Java program (Ant Cheat Sheet), you can call any of them:

$ant execute
or
$ant main
or
$ant compile execute
or
$ant clean compile execute

Response

$ant main
Buildfile: /Users/siddheshwar/Documents/gitRepo/AntSample/build.xml

clean:
   [delete] Deleting directory /Users/siddheshwar/Documents/gitRepo/AntSample/build

compile:
     [echo] compilation started -
    [mkdir] Created dir: /Users/siddheshwar/Documents/gitRepo/AntSample/build/classes
    [javac] /Users/siddheshwar/Documents/gitRepo/AntSample/build.xml:18: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds
    [javac] Compiling 1 source file to /Users/siddheshwar/Documents/gitRepo/AntSample/build/classes
     [echo] compilation complete!

execute:
     [java] Inside main method of RunMe
     [java] you passed argument=hello

main:

BUILD SUCCESSFUL
Total time: 0 seconds

--

Keep coding!

No comments:

Post a Comment