<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>JavaBeat &#187; ANT</title>
	<atom:link href="http://www.javabeat.net/category/ant/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.javabeat.net</link>
	<description>Java Technology News</description>
	<lastBuildDate>Mon, 13 May 2013 20:10:23 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>Apache ANT Listeners and Loggers</title>
		<link>http://www.javabeat.net/2013/02/apache-ant-listeners-and-loggers/</link>
		<comments>http://www.javabeat.net/2013/02/apache-ant-listeners-and-loggers/#comments</comments>
		<pubDate>Mon, 04 Feb 2013 04:41:33 +0000</pubDate>
		<dc:creator>Manisha Patil</dc:creator>
				<category><![CDATA[ANT]]></category>
		<category><![CDATA[Maven]]></category>

		<guid isPermaLink="false">http://www.javabeat.net/?p=5740</guid>
		<description><![CDATA[<p>Connect to us ( <a href="https://twitter.com/javabeat">@twitter</a> | <a href="https://www.facebook.com/javabeat.net">@facebook )</p><p>This article explains in brief about two features of ANT which are used to monitor the build process: Listeners and Loggers. In the previous articles on ANT we saw that ANT has logging feature(logging using external log4j). If the logger is not specified then ANT uses the DefaultLogger. Along with logger, Ant also has a [...]</p>]]></description>
				<content:encoded><![CDATA[<p>Connect to us ( <a href="https://twitter.com/javabeat">@twitter</a> | <a href="https://www.facebook.com/javabeat.net">@facebook )</p><div class="wpInsert wpInsertInPostAd wpInsertLeft" style="float: left; margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* Article-Rect */
google_ad_slot = "9976259118";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div><a id="dd_start"></a><p>This article explains in brief about two features of ANT which are used to monitor the build process: <strong>Listeners and Loggers</strong>. In the <a href="http://www.javabeat.net/2013/01/tutorial-of-java-programming-with-apache-ant/">previous articles</a> on ANT we saw that ANT has logging feature(logging using external log4j). If the logger is not specified then ANT uses the DefaultLogger. Along with logger, Ant also has a log message handling category known as a listener. At a low level a logger is in fact a listener with a little difference.</p>
<ul>
<li>Logger has the ability to write to log output device, such as a console or a file supplied by the &#8220;-logfile&#8221; command line argument.</li>
<li>Another difference is ANT build file can have multiple listeners but only one logger.</li>
<li>The logger is something which ANT connects to the System.out and System.err streams</li>
</ul>
<p>The article briefly explains about loggers, listeners and built-in loggers/listeners.</p>
<p><strong><span style="text-decoration: underline;">Books:</span></strong></p>
<ul>
<li><a href="http://www.flipkart.com/ant-definitive-guide-2nd/p/itmczzj3dfpzhp9d?pid=9788184040807&amp;affid=suthukrish" target="_blank">Ant: The Definitive Guide</a></li>
<li><a href="http://www.flipkart.com/apache-maven-2-effective-implementation/p/itmdyusqwng3phvg?pid=9788184049152&amp;affid=suthukrish" target="_blank">Apache Maven 2 Effective Implementation</a></li>
</ul>
<p style="text-align: center;"><a href="http://www.javabeat.net/wp-content/uploads/2013/02/Apache-Ant-Logo-java.png"><img class="aligncenter  wp-image-5798" alt="Apache-Ant-Logo-java" src="http://www.javabeat.net/wp-content/uploads/2013/02/Apache-Ant-Logo-java.png" width="388" height="240" /></a></p>
<h2><b>Ant Listeners</b></h2>
<p>A build life has events, which alerts the listener.These events are:</p>
<ul>
<li>Build started</li>
<li>Build finished</li>
<li>Target started</li>
<li>Target finished</li>
<li>Task started</li>
<li>Task finished</li>
<li>Message logged</li>
</ul>
<p>As these events trigger some action in listener, they are used for various recording and maintenance operation. New listeners may be registered on the command line through the <b>-listener </b> argument.</p>
<h2><b>Ant Loggers</b></h2>
<p>A logger is built on top of a listener, extends the capabilities of listeners and logs information about the build. You use a logger whenever you run ANT, whether or not you specify one. <span style="color: #000080;"><strong>If a logger is not specified in ANT, then you are using the DefaultLogger</strong></span>. Loggers have following features:</p>
<ol>
<li>Loggers receive a handle to the standard output and error print streams and hence log information to the console or to a specified file(command for specifying file <b>-logfile</b>).</li>
<li>Loggers are aware of logging level (-quiet, -verbose, -debug)</li>
<li>Loggers are aware of Emacs-mode</li>
</ol>
<h2><b>Built-in Listeners/Loggers</b></h2>
<p>In addition to the DefaultLogger, Ant comes with following standard loggers and listeners:</p>
<ul>
<li><strong>DefaultLogger</strong></li>
<li><strong>NoBannerLogger</strong></li>
<li><strong>MailLogger</strong></li>
<li><strong>AnsiColorLogger</strong></li>
<li><strong>XmlLogger</strong></li>
<li><strong>Log4jListener</strong></li>
<li><strong>TimestampedLogger</strong></li>
<li><strong>BigProjectLogger</strong></li>
<li><strong>ProfileLogger</strong></li>
</ul>
<p>Here&#8217;s a brief overview of what each one does:<br />
(Note:The source file and build file used in all the examples below shown are from previous article(<a href="http://www.javabeat.net/2013/01/tutorial-of-java-programming-with-apache-ant/">Tutorial of java programming with Apache Ant</a>) )</p>
<h2><b>DefaultLogger</b></h2>
<p>DefaultLogger is used implicitly by ANT unless overridden to use some other logger.Though the logging is not so impressive, but it does its job well. Use the following command-line option to override this logger:<br />
<span style="color: #0000ff;">ant -logger org.apache.tools.ant.DefaultLogger</span></p>
<p><b>Output</b></p>
<pre class="brush: java; title: ; notranslate">
C:\&gt;ant -logger org.apache.tools.ant.DefaultLogger
Buildfile: build.xml
clean:
   [delete] Deleting directory C:\build
compile:
    [mkdir] Created dir: C:\build\classes
    [javac] Compiling 1 source file to C:\build\classes
jar:
    [mkdir] Created dir: C:\build\jar
      [jar] Building jar: C:\build\jar\WelcomeToJavaBeat.jar
run:
     1 0 [main] INFO net.javaBeat.WelcomeToJavaBeat  - Welcome To JavaBeat
main:
BUILD SUCCESSFUL
</pre>
<h2><b>NoBannerLogger</b></h2>
<p>The NoBannerLogger is very similar to the DefaultLogger, except it omits output from targets that don&#8217;t log anything. When you run Ant, the console is printed as follows:<br />
Each target&#8217;s name prints at the left-most column, followed by a colon, and then the name of corresponding task is printed in brackets. This occurs even if there is no useful output from any of the tasks.Here at such scenarios NoBannerLogger can be used to suppress output of empty target output.</p>
<p>Use the following command-line switch to use NoBannerLogger:<br />
<span style="color: #0000ff;">ant -logger org.apache.tools.ant.NoBannerLogger</span></p>
<p><b>Output</b></p>
<pre class="brush: java; title: ; notranslate">
C:\&gt;ant -logger org.apache.tools.ant.NoBannerLogger
Buildfile: build.xml
clean:
   [delete] Deleting directory C:\build
compile:
    [mkdir] Created dir: C:\build\classes
    [javac] Compiling 1 source file to C:\build\classes
jar:
    [mkdir] Created dir: C:\build\jar
      [jar] Building jar: C:\build\jar\WelcomeToJavaBeat.jar
run:
     1 0 [main] INFO net.javaBeat.WelcomeToJavaBeat  - Welcome To JavaBeat
BUILD SUCCESSFUL
</pre>
<p><span style="color: #0000ff;">Here you see that the logger has omitted the <b>main</b> target.</span></p>
<h2><b>MailLogger</b></h2>
<p>This logger extends the DefaultLogger, such that it captures all the output logged through DefaultLogger,then send a success or failure messages to unique email lists. A group of properties must be set for MailLogger to do its job. These properties can be passed into Ant from the command line using the standard Java -D syntax or statements in your init target or,by specifying a properties file.<br />
The properties are:</p>
<ul>
<li>MailLogger.mailhost-Mail server to use.Defaults to &#8220;localhost&#8221;.</li>
<li>MailLogger.port -SMTP Port for the Mail server.Defaults to &#8220;25&#8243;.</li>
<li>MailLogger.user-user name for SMTP auth.This field is required, if SMTP auth is required on your SMTP server the email message will be then sent using Mime and requires JavaMail</li>
<li>MailLogger.password-password for SMTP auth.This field is required, if SMTP auth is required on your SMTP server the email message will be then sent using Mime and requires JavaMail.</li>
<li>MailLogger.ssl-on or true if ssl is needed.This feature requires JavaMail.</li>
<li>MailLogger.from-You have to set this to a good &#8220;from&#8221; address</li>
<li>MailLogger.replyto-You have to set this to a &#8220;replyto&#8221; address(es),comma-seperated.</li>
<li>MailLogger.failure.notify-Whether to send an e-mail on build failure; defaults to true</li>
<li>MailLogger.success.notify-Whether to send an e-mail on build success; defaults to true</li>
<li>MailLogger.failure.to-A comma-separated list of e-mail addresses to send to on build failure</li>
<li>MailLogger.success.to-A comma-separated list of e-mail addresses to send to on build success</li>
<li>MailLogger.failure.subject-The subject of the e-mail on build failure; defaults to &#8220;Build Failure&#8221;</li>
<li>MailLogger.success.subject-The subject of the e-mail on build success; defaults to &#8220;Build Success&#8221;</li>
<li>MailLogger.failure.body-The fixed body of the e-mail on build failure;default is to send full log output</li>
<li>MailLogger.success.body-The fixed body of the e-mail on build success;default is to send full log output</li>
<li>MailLogger.mimeType-MIME-type of the message being sent;defaults to text/plain</li>
<li>MailLogger.charset-character set of message being sent.</li>
<li>MailLogger.starttls.enable-on or true if STARTTLS should be supported (requires JavaMail);deafults to &#8220;false&#8221;</li>
<li>MailLogger.properties.file-The name of a properties file that contains properties for the logger and which overrides other values.</li>
</ul>
<p>Use the following command-line option to use this logger:<br />
<span style="color: #0000ff;">ant -logger org.apache.tools.ant.listener.MailLogger</span></p>
<h2><b>AnsiColorLogger</b></h2>
<p>AnsiColorLogger can produce color output(standard ANT output) by prefixing and suffixing ANSI color code escape sequences to it.It extends the DefaultLogger features.You can specify certain ANSI color sequences for different types of messages based on severity. In other words, info messages are a different color than errors. There are lots of codes that you can use to specify which colors you want. Consult the Ant documentation for the full list.</p>
<p>AnsiColorLogger is designed to work on terminals that support ANSI color codes. It works on XTerm, ETerm, Win9x Console (with ANSI.SYS loaded.), etc.</p>
<p>NOTE: It doesn&#8217;t work on WinNT and successors, even when a COMMAND.COM console loaded with ANSI.SYS is used.<br />
The recognized keys and their default values are shown below:</p>
<pre class="brush: java; title: ; notranslate">
 AnsiColorLogger.ERROR_COLOR=2;31
 AnsiColorLogger.WARNING_COLOR=2;35
 AnsiColorLogger.INFO_COLOR=2;36
 AnsiColorLogger.VERBOSE_COLOR=2;32
 AnsiColorLogger.DEBUG_COLOR=2;34
</pre>
<p>Each key takes as value a color combination defined as <b>Attribute;Foreground;Background</b>.In the above example, background value has not been used.</p><div class="wpInsert wpInsertInPostAd wpInsertMiddle" style="margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* Article-Middle-Med-Rect */
google_ad_slot = "7805667846";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>
<p>Command line option to use this logger:<br />
<span style="color: #0000ff;">ant -logger org.apache.tools.ant.listener.AnsiColorLogger</span></p>
<h2><b>Log4jListener</b></h2>
<p>Log4jListener passes events to Log4j for highly customizable logging. Log4j JAR need to be installed and a &#8216;log4j.xml&#8217; configuration file configured for this listener to work.Each build event is passed to Log4j, using the full classname of the generator of each build event as the Log4j category:</p>
<ul>
<li>build started / build finished &#8211; org.apache.tools.ant.Project</li>
<li>target started / target finished &#8211; org.apache.tools.ant.Target</li>
<li>task started / task finished &#8211; the fully qualified classname of the task</li>
<li>message logged &#8211; the classname of one of the above, so if a task logs a message, its classname is the Log4j category used, and so on.</li>
</ul>
<p>All the start events are logged as INFO-level messages; finish events are sent as INFO if they succeed, and ERROR otherwise. Message events are logged according to their Ant logging level, mapping directly to a corresponding Log4j level.</p>
<p>Example:<br />
<b>log4j.xml file:</b></p>
<pre class="brush: java; title: ; notranslate">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; ?&gt;
&lt;!DOCTYPE log4j:configuration SYSTEM &quot;log4j.dtd&quot;&gt;
&lt;log4j:configuration xmlns:log4j=&quot;http://jakarta.apache.org/log4j/&quot;&gt;
   &lt;appender name=&quot;fileAppender&quot; class=&quot;org.apache.log4j.FileAppender&quot;&gt;
   	&lt;param name=&quot;Threshold&quot; value=&quot;INFO&quot; /&gt;
          &lt;param name=&quot;File&quot; value=&quot;build.log&quot; /&gt;
          &lt;param name=&quot;Append&quot; value=&quot;true&quot; /&gt;
          &lt;layout class=&quot;org.apache.log4j.PatternLayout&quot;&gt;
            &lt;param name=&quot;ConversionPattern&quot; value=&quot;%t %-5p %c{2} - %m%n&quot;/&gt;
          &lt;/layout&gt;
     &lt;/appender&gt;

  &lt;root&gt;
    &lt;priority value =&quot;debug&quot; /&gt;
    &lt;appender-ref ref=&quot;fileAppender&quot; /&gt;
  &lt;/root&gt;
&lt;/log4j:configuration&gt;
</pre>
<p>Place Log4j JAR in your Ant classpath.Set the following attributes:<br />
<span style="color: #0000ff;">set ANT_OPTS=-Dlog4j.debug -Dlog4j.configuration=file:///c:/lib/log4j.xml</span><br />
To use this listener, use the command:<br />
<span style="color: #0000ff;">ant -listener org.apache.tools.ant.listener.Log4jListener</span></p>
<p><b>Output in build.log file:</b></p>
<pre class="brush: java; title: ; notranslate">
  INFO Project : Build started.
  INFO Target : Target &quot;clean&quot; started.
  INFO Delete : Deleting directory C:\build
  INFO Target : Target &quot;clean&quot; finished.
  INFO Target : Target &quot;compile&quot; started.
  INFO Mkdir : Created dir: C:\build\classes
  INFO Javac : C:\build.xml:21: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds
  INFO Javac : Compiling 1 source file to C:\build\classes
  INFO Target : Target &quot;compile&quot; finished.
  INFO Target : Target &quot;jar&quot; started.
  INFO Mkdir : Created dir: C:\build\jar
  INFO Jar : Building jar: C:\build\jar\WelcomeToJavaBeat.jar
  INFO Target : Target &quot;jar&quot; finished.
  INFO Target : Target &quot;run&quot; started.
  INFO Java : 0 [main] INFO net.javaBeat.WelcomeToJavaBeat  - Welcome To JavaBeat
  INFO Target : Target &quot;run&quot; finished.
  INFO Target : Target &quot;main&quot; started.
  INFO Target : Target &quot;main&quot; finished.
  INFO Project : Build finished.
</pre>
<h2><b>XmlLogger</b></h2>
<p><span style="color: #000080;"><strong>XmlLogger logs all the build information to an XML file.</strong> <strong>This logger behaves differently depending on whether it is invoked as a listener or a logger</strong></span>.This logger writes to XML file named log.xml, or the value of the XmlLogger.file (set in your init target, at the command line with -D, or in a properties file) property if present, when used as a listener. In logger mode, all output goes to the console or to the file specified in -logfile of command-line option.<br />
You can set a property called ant.XmlLogger.stylesheet to point to the XSLT stylesheet. If you don&#8217;t specify a style sheet, the XML file will point to one called log.xsl by default in the current directory(ANT_HOME/etc). If you don&#8217;t want stylesheet support built into your XML file, set this property to an empty string &#8220;&#8221;.<br />
Whether XmlLogger is used as a listener or logger, the output is generated only on build completion, as it buffers the information in order to provide timing information for task, targets, and the project.</p>
<p>Command lines for starting Ant with the XmlLogger in both listener and logger modes are:<br />
<span style="color: #0000ff;">ant -listener org.apache.tools.ant.XmlLogger</span><br />
<b>The output is logged in log.xml</b></p>
<p><span style="color: #0000ff;">ant -logger org.apache.tools.ant.XmlLogger -logfile output.xml</span><br />
<b>The output is logged in output.xml</b></p>
<p><b>TimestampedLogger</b><br />
TimestampedLogger includes the build completion time at the final success/failure.It extends the fetaures of DefaultLogger.</p>
<p>To use this listener, use the command:<br />
<span style="color: #0000ff;"> ant -logger org.apache.tools.ant.listener.TimestampedLogger </span><br />
<b>Output:</b></p>
<pre class="brush: java; title: ; notranslate">
C:\&gt;ant -logger org.apache.tools.ant.listener.TimestampedLogger
Buildfile: C:\build.xml
clean:
    [delete] Deleting directory C:\build
compile:
     [mkdir] Created dir: C:\build\classes
     [javac] C:\build.xml:21: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds
     [javac] Compiling 1 source file to C:\build\classes
 jar:
     [mkdir] Created dir: C:\build\jar
       [jar] Building jar: C:\build\jar\WelcomeToJavaBeat.jar
 run:
      1 0 [main] INFO net.javaBeat.WelcomeToJavaBeat  - Welcome To JavaBeat

 main:

BUILD SUCCESSFUL - at 1/30/13 12:53 PM
 </pre>
<p><b>BigProjectLogger</b><br />
BigProjectLogger is used in big projects which build complex systems,especialy those run with continuous integration tools and use subant.</p>
<ul>
<li>When entering a child project, prints its name and directory</li>
<li>When exiting a child project, prints its name</li>
<li>Includes the name of the project when printing a target</li>
<li>Omits logging the names of all targets that have no direct task output</li>
<li>Includes the build finished timestamp of the TimeStamp logger</li>
</ul>
<p>This is useful when using to build a large project from many smaller projects;the output shows which particular project is building.</p>
<p>To use this listener, use the command:<br />
<span style="color: #0000ff;">ant -logger org.apache.tools.ant.listener.BigProjectLogger </span><br />
<b>Output:</b></p>
<pre class="brush: java; title: ; notranslate">
C:\&gt;ant -logger org.apache.tools.ant.listener.BigProjectLogger
Buildfile: C:\build.xml

======================================================================
Entering project
In C:\
======================================================================

WelcomeToJavaBeat.clean:
   [delete] Deleting directory C:\build

WelcomeToJavaBeat.compile:
    [mkdir] Created dir: C:\build\classes
    [javac] C:\build.xml:21: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds
    [javac] Compiling 1 source file to C:\build\classes

WelcomeToJavaBeat.jar:
    [mkdir] Created dir: C:\build\jar
      [jar] Building jar: C:\build\jar\WelcomeToJavaBeat.jar

WelcomeToJavaBeat.run:
     1 0 [main] INFO net.javaBeat.WelcomeToJavaBeat  - Welcome To JavaBeat

======================================================================
Exiting project &quot;WelcomeToJavaBeat&quot;
======================================================================

BUILD SUCCESSFUL - at 1/30/13 12:54 PM
Total time: 2 seconds
</pre>
<p><b>ProfileLogger</b><br />
The ProfileLogger is a default logger which adds start time,end time and durations for each task and target.The output contains a timestamp when entering the build, target or task and a timestamp and the needed time when exiting.</p>
<p>To use this listener, use the command:<br />
<span style="color: #0000ff;">ant -logger org.apache.tools.ant.listener.ProfileLogger</span></p>
<p><b>Output</b></p>
<pre class="brush: java; title: ; notranslate">
C:\&gt;ant -logger org.apache.tools.ant.listener.ProfileLogger
Buildfile: C:\build.xml
property: started Wed Jan 30 12:55:11 IST 2013
property: finished Wed Jan 30 12:55:12 IST 2013 (360ms)
property: started Wed Jan 30 12:55:12 IST 2013
property: finished Wed Jan 30 12:55:12 IST 2013 (0ms)
property: started Wed Jan 30 12:55:12 IST 2013
property: finished Wed Jan 30 12:55:12 IST 2013 (0ms)
property: started Wed Jan 30 12:55:12 IST 2013
property: finished Wed Jan 30 12:55:12 IST 2013 (0ms)
property: started Wed Jan 30 12:55:12 IST 2013
property: finished Wed Jan 30 12:55:12 IST 2013 (0ms)
property: started Wed Jan 30 12:55:12 IST 2013
property: finished Wed Jan 30 12:55:12 IST 2013 (0ms)
path: started Wed Jan 30 12:55:12 IST 2013
path: finished Wed Jan 30 12:55:12 IST 2013 (62ms)
Target clean: started Wed Jan 30 12:55:12 IST 2013
delete: started Wed Jan 30 12:55:12 IST 2013
  [delete] Deleting directory C:\build
delete: finished Wed Jan 30 12:55:12 IST 2013 (31ms)
Target clean: finished Wed Jan 30 12:55:12 IST 2013 (31ms)
Target compile: started Wed Jan 30 12:55:12 IST 2013
mkdir: started Wed Jan 30 12:55:12 IST 2013
    [mkdir] Created dir: C:\build\classes
mkdir: finished Wed Jan 30 12:55:12 IST 2013 (47ms)
javac: started Wed Jan 30 12:55:12 IST 2013
    [javac] C:\build.xml:21: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds
    [javac] Compiling 1 source file to C:\build\classes
javac: finished Wed Jan 30 12:55:13 IST 2013 (782ms)
Target compile: finished Wed Jan 30 12:55:13 IST 2013 (829ms)
Target jar: started Wed Jan 30 12:55:13 IST 2013
mkdir: started Wed Jan 30 12:55:13 IST 2013
    [mkdir] Created dir: C:\build\jar
mkdir: finished Wed Jan 30 12:55:13 IST 2013 (0ms)
jar: started Wed Jan 30 12:55:13 IST 2013
      [jar] Building jar: C:\build\jar\WelcomeToJavaBeat.jar
jar: finished Wed Jan 30 12:55:13 IST 2013 (78ms)
Target jar: finished Wed Jan 30 12:55:13 IST 2013 (93ms)
Target run: started Wed Jan 30 12:55:13 IST 2013
java: started Wed Jan 30 12:55:13 IST 2013
     1 0 [main] INFO net.javaBeat.WelcomeToJavaBeat  - Welcome To JavaBeat
java: finished Wed Jan 30 12:55:14 IST 2013 (844ms)
Target run: finished Wed Jan 30 12:55:14 IST 2013 (859ms)
Target main: started Wed Jan 30 12:55:14 IST 2013
Target main: finished Wed Jan 30 12:55:14 IST 2013 (0ms)
BUILD SUCCESSFUL
</pre>
<h3><b>Summary</b></h3>
<p><span style="text-decoration: underline;"><strong>Books:</strong></span></p>
<ul>
<li><a href="http://www.flipkart.com/ant-definitive-guide-2nd/p/itmczzj3dfpzhp9d?pid=9788184040807&amp;affid=suthukrish" target="_blank">Ant: The Definitive Guide</a></li>
<li><a href="http://www.flipkart.com/apache-maven-2-effective-implementation/p/itmdyusqwng3phvg?pid=9788184049152&amp;affid=suthukrish" target="_blank">Apache Maven 2 Effective Implementation</a></li>
</ul>
<p>To summarize,we saw the difference between loggers and listerners.Each built-in logger and listerner is expalined in brief with a simple example.For any further queries please leave your comments.</p>
<div class='dd_outer'><div class='dd_inner'><div id='dd_ajax_float'><div class='dd_button_v'><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><fb:like href="http%3A%2F%2Fwww.javabeat.net%2Fcategory%2Fant%2Ffeed%2F" send="false" show_faces="false"  layout="box_count" width="50"  ></fb:like></div><div style='clear:left'></div><div class='dd_button_v'><script type='text/javascript' src='https://apis.google.com/js/plusone.js'></script><g:plusone size='tall' href='http://www.javabeat.net/category/ant/feed/'></g:plusone></div><div style='clear:left'></div><div class='dd_button_v'><a href="http://twitter.com/share" class="twitter-share-button" data-url="http://www.javabeat.net/category/ant/feed/" data-count="vertical" data-text="ANT" data-via="javabeat" ></a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></div><div style='clear:left'></div><div class='dd_button_extra_v'><script type="text/javascript">jQuery(document).load(function(){ stLight.options({publisher:'bab47279-62c9-46af-addc-79fd1fe8fee0'}); });</script><div class="st_email_custom"><span id='dd_email_text'>email</span></div></div><div style='clear:left'></div><div class='dd_button_extra_v'><div id='dd_print_button'><span id='dd_print_text'><a href='javascript:window:print()'>print</a></span></div></div><div style='clear:left'></div></div></div></div><script type="text/javascript">var dd_offset_from_content = 44; var dd_top_offset_from_content = 0;</script><script type="text/javascript" src="http://www.javabeat.net/wp-content/plugins/digg-digg//js/diggdigg-floating-bar.js?ver=5.3.0"></script><div class="wpInsert wpInsertInPostAd wpInsertBelow" style="margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* JB-Footer-LU 468x15 */
google_ad_slot = "8789107210";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>]]></content:encoded>
			<wfw:commentRss>http://www.javabeat.net/2013/02/apache-ant-listeners-and-loggers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Tutorial of java programming with Apache Ant</title>
		<link>http://www.javabeat.net/2013/01/tutorial-of-java-programming-with-apache-ant/</link>
		<comments>http://www.javabeat.net/2013/01/tutorial-of-java-programming-with-apache-ant/#comments</comments>
		<pubDate>Mon, 28 Jan 2013 00:37:10 +0000</pubDate>
		<dc:creator>Manisha Patil</dc:creator>
				<category><![CDATA[ANT]]></category>

		<guid isPermaLink="false">http://www.javabeat.net/?p=5621</guid>
		<description><![CDATA[<p>Connect to us ( <a href="https://twitter.com/javabeat">@twitter</a> | <a href="https://www.facebook.com/javabeat.net">@facebook )</p><p>This article guides you through java programming with Apache Ant with the steps listed as below. The following are the steps used in the articles to explain the Ant script creation. If you have any questions, please write it in the comments section. If you are interested in receiving the future articles on Java, please [...]</p>]]></description>
				<content:encoded><![CDATA[<p>Connect to us ( <a href="https://twitter.com/javabeat">@twitter</a> | <a href="https://www.facebook.com/javabeat.net">@facebook )</p><div class="wpInsert wpInsertInPostAd wpInsertLeft" style="float: left; margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* Article-Rect */
google_ad_slot = "9976259118";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div><div>
<p>This article guides you through java programming with Apache Ant with the steps listed as below. The following are the steps used in the articles to explain the Ant script creation. If you have any questions, please write it in the comments section. If you are interested in receiving the future articles on Java, please subscribe <a href="http://www.javabeat.net/subscribe/">here</a>.</p>
<ol>
<li>Create a simple java program(WelcomeToJavaBeat) and run it from command prompt.</li>
<li>Build the JAR and use the JAR to run the WelcomeToJavaBeat application.</li>
<li>Use Apache Ant to create a buildfile for WelcomeToJavaBeat application.</li>
<li>Build WelcomeToJavaBeat application to include external library(log4j) and hence create Apache Ant buildfile for this application</li>
</ol>
<p>We have taken some references from the Apache Ant web site for the above tasks. We created this post as there was chance for better clarification.</p>
</div>
<h3><b>Task 1: Create a simple java program(WelcomeToJavaBeat) and run it from command prompt</b></h3>
<ol>
<li>Create the following directory structure ( here <b>[basedir]</b> can be any directory):[basedir]\src\net\javaBeat<br />
[basedir]\build\classes<br />
[basedir]\build\jarFolder <b>build</b> contains all the generated files.Folder <b>[basedir]\build\classes</b> shall include all compiled files and <b>[basedir]\build\jar</b> folder shall contain the JAR-file.</li>
<li>Create a simple java program(WelcomeToJavaBeat.java) in <b>[basedir]\src\net\javaBeat</b> and run it from command prompt.
<pre class="brush: java; title: ; notranslate">
package net.javaBeat;
public class WelcomeToJavaBeat {
    public static void main(String[] args) {
        System.out.println(&quot;Welcome To JavaBeat&quot;);
    }
}</pre>
</li>
<li>Compile and run the code as follows:<br />
<span style="color: #0000ff;">javac -sourcepath src -d build\classes src\net\javaBeat\WelcomeToJavaBeat.java<br />
java -cp build\classes net.javaBeat.WelcomeToJavaBeat</span><br />
The second command runs the application by calling the class file<b>Output:</b></p>
<p class="wp_syntax">Welcome To JavaBeat</p>
</li>
</ol>
<h3><b>Task2: Build the JAR and use the JAR to run the WelcomeToJavaBeat application</b></h3>
<p>Build a JAR file for the java application and use the JAR command to run the application</p>
<ol>
<li>Create a manifest file.
<ul>
<li><span style="color: #0000ff;">echo Main-Class: net.javaBeat.WelcomeToJavaBeat&gt;myManifest: </span></li>
</ul>
<ul>
<li>Do not have blanks around the &#8220;&gt;&#8221;sign in the echo Main-Class instruction because it would falsify it.</li>
</ul>
<ul>
<li>This command creates a myManifest file in the <b>[basedir]</b></li>
</ul>
</li>
<li>Create the JAR. (Don’t miss the &#8220;dot&#8221; at the end of the command.)
<ul>
<li><span style="color: #0000ff;">jar cfm build\jar\WelcomeToJavaBeat.jar myManifest -C build\classes .</span></li>
</ul>
</li>
<li>Run the application.
<ul>
<li><span style="color: #0000ff;">java -jar build\jar\WelcomeToJavaBeat.jar </span></li>
</ul>
</li>
</ol>
<h3><b>Task3: Use Apache Ant to create a buildfile for WelcomeToJavaBeat application</b></h3>
<p>Ant uses build.xml as the name for a build file by default, so our <b>[basedir]\build.xml</b> would be as follows. It&#8217;s a good practice to have a &#8220;clean&#8221; target, which deletes all the existing stuff from the directories. The clean target must be the first task to be running after starting the Ant script.</p>
<pre class="brush: java; title: ; notranslate">
&lt;project&gt;

    &lt;target name=&quot;clean&quot;&gt;
        &lt;delete dir=&quot;build&quot;/&gt;
    &lt;/target&gt;

    &lt;target name=&quot;compile&quot;&gt;
        &lt;mkdir dir=&quot;build/classes&quot;/&gt;
        &lt;javac srcdir=&quot;src&quot; destdir=&quot;build/classes&quot;/&gt;
    &lt;/target&gt;

    &lt;target name=&quot;jar&quot;&gt;
        &lt;mkdir dir=&quot;build/jar&quot;/&gt;
        &lt;jar destfile=&quot;build/jar/WelcomeToJavaBeat.jar&quot; basedir=&quot;build/classes&quot;&gt;
            &lt;manifest&gt;
                &lt;attribute name=&quot;Main-Class&quot; value=&quot;net.javaBeat.WelcomeToJavaBeat&quot;/&gt;
            &lt;/manifest&gt;
        &lt;/jar&gt;
    &lt;/target&gt;

    &lt;target name=&quot;run&quot;&gt;
        &lt;java jar=&quot;build/jar/WelcomeToJavaBeat.jar&quot; fork=&quot;true&quot;/&gt;
    &lt;/target&gt;

&lt;/project&gt;</pre>
<p>Use the following command to compile,package and run the application:<br />
<span style="color: #0000ff;">ant compile jar run</span></p>
<p>The above build file can be enhanced as follows:</p><div class="wpInsert wpInsertInPostAd wpInsertMiddle" style="margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* Article-Middle-Med-Rect */
google_ad_slot = "7805667846";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>
<ul>
<li>Many a times we reference the same directories and main-class.We can address this problem using <b><i>properties.</i></b></li>
<li>jar-name are hard coded,which can be taken care of by using a special property &#8211; an attribute of the &lt;project&gt;-tag</li>
<li>While invocation we need to remember right order of build steps.This can be dealt with by using dependencies.</li>
</ul>
<pre class="brush: java; title: ; notranslate">
&lt;project name=&quot;WelcomeToJavaBeat&quot; basedir=&quot;.&quot; default=&quot;main&quot;&gt;

    &lt;property name=&quot;src.dir&quot;     value=&quot;src&quot;/&gt;
    &lt;property name=&quot;build.dir&quot;   value=&quot;build&quot;/&gt;
    &lt;property name=&quot;classes.dir&quot; value=&quot;${build.dir}/classes&quot;/&gt;
    &lt;property name=&quot;jar.dir&quot;     value=&quot;${build.dir}/jar&quot;/&gt;
    &lt;property name=&quot;main-class&quot;  value=&quot;net.javaBeat.WelcomeToJavaBeat&quot;/&gt;

   &lt;target name=&quot;clean&quot;&gt;
        &lt;delete dir=&quot;${build.dir}&quot;/&gt;
    &lt;/target&gt;

    &lt;target name=&quot;compile&quot;&gt;
        &lt;mkdir dir=&quot;${classes.dir}&quot;/&gt;
        &lt;javac srcdir=&quot;${src.dir}&quot; destdir=&quot;${classes.dir}&quot;/&gt;
    &lt;/target&gt;

    &lt;target name=&quot;jar&quot; depends=&quot;compile&quot;&gt;
        &lt;mkdir dir=&quot;${jar.dir}&quot;/&gt;
        &lt;jar destfile=&quot;${jar.dir}/${ant.project.name}.jar&quot; basedir=&quot;${classes.dir}&quot;&gt;
            &lt;manifest&gt;
                &lt;attribute name=&quot;Main-Class&quot; value=&quot;${main-class}&quot;/&gt;
            &lt;/manifest&gt;
        &lt;/jar&gt;
    &lt;/target&gt;

    &lt;target name=&quot;run&quot; depends=&quot;jar&quot;&gt;
        &lt;java jar=&quot;${jar.dir}/${ant.project.name}.jar&quot; fork=&quot;true&quot;/&gt;
    &lt;/target&gt;

    &lt;target name=&quot;clean-build&quot; depends=&quot;clean,jar&quot;/&gt;

    &lt;target name=&quot;main&quot; depends=&quot;clean,run&quot;/&gt;

&lt;/project&gt;
</pre>
<p>To invoke the buildfile, type <b>ant</b> at the command line. This runs the <b>main</b> target in the buildfile.Note that the project tag defines the default task as <b>main</b>.</p>
<p><b>Output:</b></p>
<pre class="brush: java; title: ; notranslate">C:\&gt;ant
Buildfile: build.xml

clean:

compile:
    [mkdir] Created dir: C:\build\classes
[javac] Compiling 1 source file to C:\build\classes

jar:
    [mkdir] Created dir: C:\build\jar
      [jar] Building jar: C:\build\jar\WelcomeToJavaBeat.jar

run:&lt;/pre&gt;
&lt;code&gt;Welcome To JavaBeat&lt;/code&gt;

&lt;code&gt;main:&lt;/code&gt;

&lt;code&gt;BUILD SUCCESSFUL&lt;/code&gt;

</pre>
<p>Task4: Build WelcomeToJavaBeat application to include external library(log4j) and hence create Apache Ant buildfile for this application&lt;/b&gt;&lt;/h3&gt;Store external libraries in a new directory lib. Log4J can be downloaded from the <a href="http://logging.apache.org/log4j/1.2/download.html" target="_blank">Apache site</a>. Create the lib directory and extract the log4j-1.2.17.jar into that lib-directory. Modify java source to use this library and also modify the buildfile so that this library could be accessed during compilation and run.evised Java Source:</p>
<pre class="brush: java; title: ; notranslate">
 package net.javaBeat;

 import org.apache.log4j.BasicConfigurator;
 import org.apache.log4j.Logger;

 public class WelcomeToJavaBeat {

 static Logger logger = Logger.getLogger(WelcomeToJavaBeat.class);
 public static void main(String[] args) {
 BasicConfigurator.configure();
 logger.info(&quot;Welcome To JavaBeat&quot;);// the old System Out-statement
 }
}

</pre>
<p><b>Revised buildfile:</b></p>
<pre class="brush: java; title: ; notranslate">
&lt;project name=&quot;WelcomeToJavaBeat&quot; basedir=&quot;.&quot; default=&quot;main&quot;&gt;

    &lt;property name=&quot;src.dir&quot;     value=&quot;src&quot;/&gt;
    &lt;property name=&quot;build.dir&quot;   value=&quot;build&quot;/&gt;
    &lt;property name=&quot;classes.dir&quot; value=&quot;${build.dir}/classes&quot;/&gt;
    &lt;property name=&quot;jar.dir&quot;     value=&quot;${build.dir}/jar&quot;/&gt;
    &lt;property name=&quot;lib.dir&quot;     value=&quot;lib&quot;/&gt;
    &lt;property name=&quot;main-class&quot;  value=&quot;net.javaBeat.WelcomeToJavaBeat&quot;/&gt;

    &lt;path id=&quot;classpath&quot;&gt;
            &lt;fileset dir=&quot;${lib.dir}&quot; includes=&quot;**/*.jar&quot;/&gt;
    &lt;/path&gt;

   &lt;target name=&quot;clean&quot;&gt;
        &lt;delete dir=&quot;${build.dir}&quot;/&gt;
    &lt;/target&gt;

    &lt;target name=&quot;compile&quot;&gt;
        &lt;mkdir dir=&quot;${classes.dir}&quot;/&gt;
        &lt;javac srcdir=&quot;${src.dir}&quot; destdir=&quot;${classes.dir}&quot; classpathref=&quot;classpath&quot;/&gt;
    &lt;/target&gt;

    &lt;target name=&quot;jar&quot; depends=&quot;compile&quot;&gt;
        &lt;mkdir dir=&quot;${jar.dir}&quot;/&gt;
        &lt;jar destfile=&quot;${jar.dir}/${ant.project.name}.jar&quot; basedir=&quot;${classes.dir}&quot;&gt;
            &lt;manifest&gt;
                &lt;attribute name=&quot;Main-Class&quot; value=&quot;${main-class}&quot;/&gt;
            &lt;/manifest&gt;
        &lt;/jar&gt;
    &lt;/target&gt;

    &lt;target name=&quot;run&quot; depends=&quot;jar&quot;&gt;
    	&lt;java fork=&quot;true&quot; classname=&quot;${main-class}&quot;&gt;
                &lt;classpath&gt;
                    &lt;path refid=&quot;classpath&quot;/&gt;
                    &lt;path location=&quot;${jar.dir}/${ant.project.name}.jar&quot;/&gt;
                &lt;/classpath&gt;
        &lt;/java&gt;

    &lt;/target&gt;
    &lt;target name=&quot;clean-build&quot; depends=&quot;clean,jar&quot;/&gt;
    &lt;target name=&quot;main&quot; depends=&quot;clean,run&quot;/&gt;
&lt;/project&gt;
</pre>
<p>Following are the steps to modify the buildfile:</p>
<ul>
<li>add property name lib.dir</li>
<li>add path element for classpath to include JARs in lib.dir</li>
<li>in the compile target, add classpathref to use the classpath which points to the external library JAR.</li>
<li>modify run task, such that it refers to the main-class,finds the project JAR and uses the classpath with the JARs in the library.</li>
</ul>
<pre class="wp_syntax"><b>Run ant and the following output would be displayed:</b></pre>
<div>
<pre class="wp_syntax">C:\&gt;ant
Buildfile: build.xml

clean:
   [delete] Deleting directory C:\build

compile:
    [mkdir] Created dir: C:\build\classes
[javac] Compiling 1 source file to C:\build\classes

jar:
    [mkdir] Created dir: C:\build\jar
      [jar] Building jar: C:\build\jar\WelcomeToJavaBeat.jar

run:
     


<pre class="wp_syntax"></pre>
<pre class="wp_syntax"></pre>
<pre class="wp_syntax">[main] INFO net.javaBeat.WelcomeToJavaBeat - Welcome To JavaBeat main: BUILD SUCCESSFUL</pre>
</div>
<h4><b>Summary</b></h4>
<p>We saw stepwise to build a simple java application through command prompt.Then created buildfile to run this simple java application.Further we enhanced the application to include logging using external jar(log4j).We also modified the buildfile to include the external jars.On similar lines we can enhance this appliction to include Junit(testing tool).</p>
<p>If you have any questions, please write it in the comments section. If you are interested in receiving the future articles on Java, please subscribe <a href="http://www.javabeat.net/subscribe/">here</a>.</p>
<div class="wpInsert wpInsertInPostAd wpInsertBelow" style="margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* JB-Footer-LU 468x15 */
google_ad_slot = "8789107210";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>]]></content:encoded>
			<wfw:commentRss>http://www.javabeat.net/2013/01/tutorial-of-java-programming-with-apache-ant/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Execute batch file or shell script using Ant</title>
		<link>http://www.javabeat.net/2013/01/execute-batch-file-or-shell-script-using-ant/</link>
		<comments>http://www.javabeat.net/2013/01/execute-batch-file-or-shell-script-using-ant/#comments</comments>
		<pubDate>Fri, 25 Jan 2013 00:52:23 +0000</pubDate>
		<dc:creator>Manisha Patil</dc:creator>
				<category><![CDATA[ANT]]></category>

		<guid isPermaLink="false">http://www.javabeat.net/?p=5544</guid>
		<description><![CDATA[<p>Connect to us ( <a href="https://twitter.com/javabeat">@twitter</a> | <a href="https://www.facebook.com/javabeat.net">@facebook )</p><p>This article discusses on how to execute a batch file or shell script using Ant.The article details about using exec command with an example code.We will write an Ant build script, which identifies the current operating system(OS) and based on the OS detected either a batch file or a shell script is executed.We assume that [...]</p>]]></description>
				<content:encoded><![CDATA[<p>Connect to us ( <a href="https://twitter.com/javabeat">@twitter</a> | <a href="https://www.facebook.com/javabeat.net">@facebook )</p><div class="wpInsert wpInsertInPostAd wpInsertLeft" style="float: left; margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* Article-Rect */
google_ad_slot = "9976259118";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div><div>This article discusses on how to execute a batch file or shell script using Ant.The article details about using <b>exec</b> command with an example code.We will write an Ant build script, which identifies the current operating system(OS) and based on the OS detected either a batch file or a shell script is executed.We assume that the reader of this article has basic knowledge about Apache Ant tool.</div>
<div>
<h2>Example Code</h2>
<p>Let&#8217;s assume we have two scripts, one (.bat file) which executes on windows operating system family, and other (.sh) that runs on all non-windows platforms like UNIX, Linux, etc. Now we need an ant task in build file, to call the appropriate scripts depending on the operating system.</p>
<p>Let&#8217;s write a target with named <b>checkOS</b>. This target depends on targets <b>ifOSWindows and ifOSNotWindows</b>.</p>
</div>
<pre class="brush: java; title: ; notranslate">
&lt;target name=&quot;checkOS&quot; depends=&quot;ifOSWindows, ifOSNotWindows&quot;/&gt;
</pre>
<p>We then define these targets as:</p>
<pre class="brush: java; title: ; notranslate">
&lt;target name=&quot;ifOSWindows&quot; if=&quot;isOSWindows&quot;&gt;
	&lt;antcall target=&quot;runscript.windows&quot;/&gt;
&lt;/target&gt;

&lt;target name=&quot;ifOSNotWindows&quot; if=&quot;isOSUnix&quot;&gt;
	&lt;antcall target=&quot;runscript.unix&quot;/&gt;
&lt;/target&gt;
</pre>
<p>We can see that, <b>ifOSWindows and ifOSNotWindows</b> are based on a condition, which if true calls the specific target.</p>
<p>If <b>isOSWindows</b> property,defined as -</p>
<pre class="brush: java; title: ; notranslate">
&lt;condition property=&quot;isOSWindows&quot;&gt;
    &lt;os family=&quot;windows&quot;/&gt;
&lt;/condition&gt;
</pre>
<p>returns <b>true</b>, then ant target <b>runscript.windows</b> is executed.</p>
<p>Else, <b>ifOSNotWindows</b> is tested using <b>isOSUnix</b> property:</p>
<pre class="brush: java; title: ; notranslate">
&lt;condition property=&quot;isOSUnix&quot;&gt;
   &lt;os family=&quot;unix&quot; /&gt;
&lt;/condition&gt;
</pre>
<p>If this condition returns true, then <b>runscript.unix</b> will be called.</p><div class="wpInsert wpInsertInPostAd wpInsertMiddle" style="margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* Article-Middle-Med-Rect */
google_ad_slot = "7805667846";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>
<p>Following is how you can define an ant task to execute a batch script on windows:<br />
To execute a script , <b>exec</b> command is used wrapped inside your target definition. &lt;exec&gt; executes a system command. When the os attribute is specified, then the command is only executed when Apache Ant is run on one of the specified operating systems :</p>
<pre class="brush: java; title: ; notranslate">
&lt;target name=&quot;runscript.windows&quot;&gt;
   &lt;echo&gt;This is a Windows machine.&lt;/echo&gt;
       &lt;exec dir=&quot;.&quot; executable=&quot;cmd&quot; &gt;
	      &lt;arg line=&quot;/c test.bat 'm4 foo.m4 &gt; foo'&quot;/&gt;
   	&lt;/exec&gt;
&lt;/target&gt;
</pre>
<p>Following is to run a shell script on UNIX using Ant task:</p>
<pre class="brush: java; title: ; notranslate">
&lt;target name=&quot;runscript.unix&quot;&gt;
   &lt;echo&gt;This is an Unix machine.&lt;/echo&gt;
	   &lt;exec dir=&quot;.&quot; executable=&quot;/bin/sh&quot;&gt;
	     &lt;arg path=&quot;-c test.bat 'm4 foo.m4 &gt; foo'&quot;/&gt;
	&lt;/exec&gt;
&lt;/target&gt;
</pre>
<p>To try out this,</p>
<ol>
<li>Copy the build.xml and test.bat (attached below), to your C: directory.Now open the command prompt,cursor pointing to C:\&gt;</li>
<li>Execute the <b>ant</b> command and the output should be:</li>
</ol>
<pre class="brush: java; title: ; notranslate">
C:\&gt;ant
Buildfile: build.xml

ifOSWindows:
     [echo] is windows........

runscript.windows:
     [echo] This is a Windows machine.
     [exec] **************************************
     [exec] Hello world!...Printed from batch file. &quot;m4 foo.m4 &gt; foo&quot;
     [exec] **************************************

ifOSNotWindows:

checkOS:

BUILD SUCCESSFUL
Total time: 0 seconds
</pre>
<p><b>build.xml</b></p>
<pre class="brush: java; title: ; notranslate">
&lt;?xml version=&quot;1.0&quot;?&gt;
&lt;project name=&quot;checkOperatingSystem&quot; default=&quot;checkOS&quot;&gt;

	&lt;!--checking if for windows and non windows OS--&gt;
	&lt;target depends=&quot;ifOSWindows, ifOSNotWindows&quot; name=&quot;checkOS&quot;/&gt;

		&lt;condition property=&quot;isOSUnix&quot;&gt;
			&lt;os family=&quot;unix&quot; /&gt;
		&lt;/condition&gt;

		&lt;condition property=&quot;isOSWindows&quot;&gt;
			&lt;os family=&quot;windows&quot; /&gt;
		&lt;/condition&gt;

	&lt;!-- if the OS is windows call the target run.script.windows --&gt;
	&lt;target name=&quot;ifOSWindows&quot; if=&quot;isOSWindows&quot;&gt;
		&lt;echo&gt;is windows........&lt;/echo&gt;
		&lt;antcall target=&quot;runscript.windows&quot;/&gt;
	&lt;/target&gt;

	&lt;!-- if the OS is not windows call the target run.script.unix--&gt;
	&lt;target name=&quot;ifOSNotWindows&quot; if=&quot;isOSUnix&quot;&gt;
		&lt;echo&gt;is unix........&lt;/echo&gt;
		&lt;antcall target=&quot;runscript.unix&quot;/&gt;
	&lt;/target&gt;

	&lt;target name=&quot;runscript.windows&quot;&gt;
		&lt;echo&gt;This is a Windows machine.&lt;/echo&gt;
		&lt;exec dir=&quot;.&quot; executable=&quot;cmd&quot; &gt;
			&lt;arg line=&quot;/c /test.bat 'm4 foo.m4 &gt; foo'&quot;/&gt;
		&lt;/exec&gt;

	&lt;/target&gt;

	&lt;target name=&quot;runscript.unix&quot;&gt;
		&lt;echo&gt;This is an Unix machine.&lt;/echo&gt;
	   	&lt;exec dir=&quot;.&quot; executable=&quot;/bin/sh&quot;&gt;
	     		&lt;arg line=&quot;-c /test.bat 'm4 foo.m4 &gt; foo'&quot;/&gt;
		&lt;/exec&gt;
	&lt;/target&gt;
&lt;/project&gt;
</pre>
<p><b>test.bat</b></p>
<pre class="brush: java; title: ; notranslate">
@echo **************************************
@echo Hello world!...Printed from batch file. %1%
@echo **************************************
</pre>
<p>To summarize,on native UNIX systems, you should be able to run shell scripts directly. On systems running a Unix-type shell (for example, Cygwin on Windows) execute the (command) shell</p>
<ol>
<li><b>sh</b> for shell scripts. Pass the shell script (plus any arguments to the script) as a single command, using -c switch.</li>
<li><b>cmd</b> for batch files. Pass the batch files (plus any arguments to the script) as a single command, using /c switch.</li>
</ol>
<div class="wpInsert wpInsertInPostAd wpInsertBelow" style="margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* JB-Footer-LU 468x15 */
google_ad_slot = "8789107210";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>]]></content:encoded>
			<wfw:commentRss>http://www.javabeat.net/2013/01/execute-batch-file-or-shell-script-using-ant/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Error starting modern compiler in Ant and Eclipse</title>
		<link>http://www.javabeat.net/2011/04/error-starting-modern-compiler-in-ant-and-eclipse/</link>
		<comments>http://www.javabeat.net/2011/04/error-starting-modern-compiler-in-ant-and-eclipse/#comments</comments>
		<pubDate>Fri, 15 Apr 2011 13:43:38 +0000</pubDate>
		<dc:creator>JavaBeat</dc:creator>
				<category><![CDATA[ANT]]></category>
		<category><![CDATA[Eclipse]]></category>

		<guid isPermaLink="false">http://www.javabeat.net/examples/?p=293</guid>
		<description><![CDATA[<p>Connect to us ( <a href="https://twitter.com/javabeat">@twitter</a> | <a href="https://www.facebook.com/javabeat.net">@facebook )</p><p>Eclipse and ANT Build Tool Error You would have come across this compiler error (error starting modern compiler) in many times during the developement. This error occirs when the actual Java runtime loaded by the environment and another tool is pointing to the differenet Java run time. This tips explains with example if this error [...]</p>]]></description>
				<content:encoded><![CDATA[<p>Connect to us ( <a href="https://twitter.com/javabeat">@twitter</a> | <a href="https://www.facebook.com/javabeat.net">@facebook )</p><div class="wpInsert wpInsertInPostAd wpInsertLeft" style="float: left; margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* Article-Rect */
google_ad_slot = "9976259118";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div><h1>Eclipse and ANT Build Tool Error</h1>
<p>You would have come across this compiler error (<b>error starting modern compiler</b>) in many times during the developement. This error occirs when the actual Java runtime loaded by the environment and another tool is pointing to the differenet Java run time. This tips explains with example if this error occirs when you are trying to run the <b>Ant build tool</b> inside eclipse. The following are the screenshots which explains where you need to check and change it.</p>
<p><img src="images/2011/04/ant/ant-build.jpg" /></p>
<p><img src="images/2011/04/ant/ant-build-1.jpg" width="670" /></p><div class="wpInsert wpInsertInPostAd wpInsertMiddle" style="margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* Article-Middle-Med-Rect */
google_ad_slot = "7805667846";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>
<p><img src="images/2011/04/ant/ant-build-2.jpg" width="670" /></p>
<p><font color="red">Here the jre selection must be the same as your eclipse is used for running the application. If its is different, you will get <b>error starting modern compiler</b></font></p>
<div class="wpInsert wpInsertInPostAd wpInsertBelow" style="margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* JB-Footer-LU 468x15 */
google_ad_slot = "8789107210";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>]]></content:encoded>
			<wfw:commentRss>http://www.javabeat.net/2011/04/error-starting-modern-compiler-in-ant-and-eclipse/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Writing simple ANT build script</title>
		<link>http://www.javabeat.net/2008/07/writing-simple-ant-build-script/</link>
		<comments>http://www.javabeat.net/2008/07/writing-simple-ant-build-script/#comments</comments>
		<pubDate>Thu, 24 Jul 2008 10:54:33 +0000</pubDate>
		<dc:creator>JavaBeat</dc:creator>
				<category><![CDATA[ANT]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://www.javabeat.net/examples/?p=188</guid>
		<description><![CDATA[<p>Connect to us ( <a href="https://twitter.com/javabeat">@twitter</a> | <a href="https://www.facebook.com/javabeat.net">@facebook )</p><p>Apache Ant Example This article explains how to write a very basic ANT build script. This does not explain indepth knowledge on building your project using ANT script. But, this writeup will be more useful for the beginners who haben&#8217;t writen any ANT script before. Before start writing the script, lets look into how to [...]</p>]]></description>
				<content:encoded><![CDATA[<p>Connect to us ( <a href="https://twitter.com/javabeat">@twitter</a> | <a href="https://www.facebook.com/javabeat.net">@facebook )</p><div class="wpInsert wpInsertInPostAd wpInsertLeft" style="float: left; margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* Article-Rect */
google_ad_slot = "9976259118";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div><h1>Apache Ant Example</h1>
<p>This article explains how to write a very basic <b><i>ANT</i></b> build script. This does not explain indepth knowledge on building your project using <b><i>ANT script</i></b>. But, this writeup will be more useful for the beginners who haben&#8217;t writen any  <b><i>ANT</i></b> script before. Before start writing the script, lets look into how to install the <b><i>ANT</i></b> in your machine.</p>
<h1>Install Apache Ant</h1>
<p>Download the <b><i>Apache Ant</i></b> from the link : <a href="http://ant.apache.org/bindownload.cgi">Download Apache Ant</a></p>
<ul>
<li>Unzip the files into your machine.</li>
<li>Set your system path variable to ANT&#8217;s bin directory. For example d:/projects/ant/bin&#8221;</li>
<li>Set the <b><i>ANT_HOME</i></b> in your system variables. <b><i>ANT_HOME</i></b> should point to your Ant installation directory. For example &#8220;d:/projects/ant&#8221;.</li>
<li>Set <b><i>JAVA_HOME</i></b> in your system environment variables.</li>
<li>Copy <b><i>tools.jar</i></b> file into the &#8220;JRE/lib&#8221; folder.</li>
</ul>
<p>	To test the setup open your command prompt and type as &#8220;ant&#8221;. You will see a message saying</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">Buildfile: build.xml does not exist!
Build failed</pre></td></tr></table></div>

<p>Set up is done. Let&#8217;s look into our first simple example program using <b><i>ANT</i></b> script.
</p>
<p>This example program shows how to create the <b><i>JAR</i></b> file after building all the java files.</p><div class="wpInsert wpInsertInPostAd wpInsertMiddle" style="margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* Article-Middle-Med-Rect */
google_ad_slot = "7805667846";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">&lt;project name=&quot;sampleProject&quot; basedir=&quot;.&quot; default=&quot;jar&quot;&gt;</pre></td></tr></table></div>

<p>In the above code &#8220;sampleProject&#8221; is the project name for this prticular build. We are setting base directory as the current directory by just giving .(dot). We are telling the script to start from &#8220;jar&#8221; target name.	The <b><i>default</i></b> attribute is used for setting the starting point for the ANT script.</p>
<p>We call a seperate task as <b><i>target</i></b>. We can define as many number of targets inside a build file. If we closely look into the build file, we have many number of targets based its operations. So, our build starts with &#8220;jar&#8221; <b><i>target</i></b>.</p>
<p>Target has <b><i>depends</i></b> attribute to indicate, that particular task can be executed only after the certain number of dependencies. In our case, before building the jar file, we have to compile the files. So, jar target calls compile target. That has some dependencies that is executed. Once the build is successful, test.jar will be created in the same directory. </p>
<h4>build.xml</h4>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">&lt;?xml version=&quot;1.0&quot;?&gt;
&lt;project name=&quot;sampleProject&quot; basedir=&quot;.&quot; default=&quot;jar&quot;&gt;
    &lt;property name=&quot;src&quot; value=&quot;ant-source&quot;/&gt;
    &lt;property name=&quot;output&quot; value=&quot;bin&quot;/&gt;
&nbsp;
    &lt;target name=&quot;compile&quot; depends=&quot;create&quot;&gt;
        &lt;javac destdir=&quot;bin&quot;&gt;
            &lt;src path=&quot;${src}&quot;/&gt;
            &lt;classpath refid=&quot;java&quot;/&gt;
        &lt;/javac&gt;
    &lt;/target&gt;
&nbsp;
    &lt;target name=&quot;jar&quot; depends=&quot;compile&quot;&gt;
        &lt;jar destfile=&quot;test.jar&quot;&gt;
            &lt;fileset dir=&quot;bin&quot;/&gt;
        &lt;/jar&gt;
    &lt;/target&gt;
&nbsp;
&nbsp;
    &lt;target name=&quot;clean&quot;&gt;
        &lt;delete dir=&quot;${output}&quot;/&gt;
    &lt;/target&gt;
&nbsp;
    &lt;target name=&quot;create&quot; depends=&quot;clean&quot;&gt;
        &lt;mkdir dir=&quot;${output}&quot;/&gt;
    &lt;/target&gt;
&nbsp;
    &lt;path id=&quot;java&quot;&gt;
        &lt;fileset dir=&quot;D:JarsHibernate&quot;&gt;
            &lt;include name=&quot;*.jar&quot;/&gt;
        &lt;/fileset&gt;
    &lt;/path&gt;
&lt;/project&gt;</pre></td></tr></table></div>

<div class="wpInsert wpInsertInPostAd wpInsertBelow" style="margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* JB-Footer-LU 468x15 */
google_ad_slot = "8789107210";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>]]></content:encoded>
			<wfw:commentRss>http://www.javabeat.net/2008/07/writing-simple-ant-build-script/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
