
Apache ANT Basics
- ANT - Build Files
- ANT - Property Task
- ANT - Property Files
- ANT - Data Types
- ANT - Building Projects
- ANT - Build Documentation
- ANT - Creating JAR files
- ANT - Create WAR Files
- ANT - Packaging Applications
- ANT - Deploying Applications
Apache ANT Advanced
Apache ANT Useful Examples
- ANT - Using Token
- ANT - Using Command Line Arguments
- ANT - Using If Else arguments
- ANT - Custom Components
- ANT - Listeners and Loggers
Apache ANT Useful Resources
Ant - Executing Java code
You can use Ant to execute the Java code. In the following example, the java class takes in an argument (administrator's email address) and send out an email.
NotifyAdministrator.java
public class NotifyAdministrator { public static void main(String[] args) { String email = args[0]; notifyAdministratorviaEmail(email); System.out.println("Administrator "+email+" has been notified"); } public static void notifyAdministratorviaEmail(String email){ //...... } }
Here is a simple build that executes this java class.
<?xml version="1.0"?> <project name="sample" default="run"> <property name="src.dir" value="."/> <property name="build.dir" value="build"/> <property name="main.class" value="NotifyAdministrator"/> <target name="init"> <mkdir dir="${build.dir}"/> </target> <target name="compile" depends="init"> <javac srcdir="${src.dir}" destdir="${build.dir}" includeantruntime="false"/> </target> <target name="run" depends="compile"> <java classname="${main.class}" classpath="${build.dir}" fork="true"> <arg line="admin@test.com"/> </java> </target> <target name="clean"> <delete dir="${build.dir}"/> </target> </project>
When the build is executed, it produces the following outcome −
C:\>ant Buildfile: C:\build.xml init: compile: [javac] Compiling 1 source file to D:\ant\build run: [java] Administrator admin@test.com has been notified BUILD SUCCESSFUL Total time: 0 seconds
In this example, the java code does a simple thing which is, to send an email. We could have used the built in the Ant task to do that.
However, now that you have got the idea, you can extend your build file to call the java code that performs complicated things. For example: encrypts your source code.
Advertisements