blob: d2ab4c2314733024d254cd1a142ebacb0baa4408 [file] [log] [blame]
---
layout: post
title: Use Apache Ant to post to Apache ESME
date: '2011-01-18T00:00:00+00:00'
categories: esme
---
<p style="line-height: 13pt; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="font-weight: normal; ">Apache ESME has an integration with&nbsp;<a href="http://ant.apache.org/" class="external-link" rel="nofollow">Apache Ant</a>. It is now possible to send messages to Apache ESME that describe the progress of your build steps. This is especially useful when Apache ESME is being used in development projects where </span><b>individual </b>developers can be made aware of the status of various-build related actions. For example, a development team could be informed that a build is broken or if it is successful. &nbsp;</p>
<h2>Setup</h2>
<h4 style="line-height: normal; font-weight: bold; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 25px; margin-right: 0px; margin-bottom: 4px; margin-left: 0px; "><a name="AntClient-Setup"></a></h4>
<ol style="line-height: 13pt; list-style-type: decimal; ">
<li style="line-height: 13pt; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">Install Apache Ant</li>
<li style="line-height: 13pt; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">Install Apache Maven</li>
<li style="line-height: 13pt; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">Download the Apache Commons packages&nbsp;<a href="http://commons.apache.org/codec/" class="external-link" rel="nofollow">&quot;Codec&quot;</a>&nbsp;and&nbsp;<a href="http://commons.apache.org/logging/download_logging.cgi" class="external-link" rel="nofollow">Logger</a>.&nbsp;We also need&nbsp;<a href="http://hc.apache.org/" class="external-link" rel="nofollow">Apache &quot;Httpclient</a>&quot;&nbsp;. Please note that we are currently using the legacy 3.1&nbsp;version of the HTTP Client.&nbsp;</li>
<li style="line-height: 13pt; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">Check-out the ESME java client:&nbsp;<a href="https://svn.apache.org/repos/asf/esme/trunk/esme-java-client_" class="external-link" rel="nofollow">https://svn.apache.org/repos/asf/esme/trunk/esme-java-client</a></li>
<li style="line-height: 13pt; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">Perform a maven build in this directory with the following command &quot;<em>mvn compile package -Dmaven.test.skip=true</em>&quot; . This will create a jar file &quot;esme-rest-api-0.1.jar&quot; in the target directory.&nbsp;</li>
<li style="line-height: 13pt; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">Create <i>lib </i>directory and copy &quot;rest-api-0.1-SNAPSHOT.jar&quot;, &quot;commons-codec-1.2.jar&quot;, &quot;commons-httpclient-3.1.jar&quot; and &quot;commons-logging-1.1.1.jar&quot; into this directory.&nbsp;</li>
<li style="line-height: 13pt; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">Create&nbsp;<em>src</em>&nbsp;directory and copy the &quot;EsmeAntTask.java&quot; file to this directory</li>
<li style="line-height: 13pt; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">Change the build.xml file to point to the appropriate directories</li>
<li style="line-height: 13pt; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">Change the token in the &quot;esme&quot; target in the build.xml file to a correct token.</li>
<li style="line-height: 13pt; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">Change the proxy in the &quot;esme&quot; target in the build.xml file to a correct proxy.</li>
<li style="line-height: 13pt; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">call&nbsp;<em>ant dist esme</em>&nbsp;to test the application</li>
</ol>
<div>
<p><span style="line-height: 17px; ">Here is what the result looks like:</span></p>
<p><a href="https://blogs.apache.org/esme/mediaresource/d4d6de99-366b-49f3-ad5b-4bbbb35949c3"><img src="https://blogs.apache.org/esme/mediaresource/d4d6de99-366b-49f3-ad5b-4bbbb35949c3?t=true" alt="ant blog.jpg" /></a></p><a href="https://blogs.apache.org/esme/mediaresource/d4d6de99-366b-49f3-ad5b-4bbbb35949c3">
<p> </p>
<p><b>Note</b>: We wrote a version of this blog two years ago on our old blog. I've updated it and added an image plus more details.&nbsp;</p></a>
<p> </p>
</div>
<h2><span style="line-height: 17px; ">Build.xml</span></h2>
<div>
<pre class="code-xml" style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; text-align: left; overflow-x: auto; overflow-y: auto; direction: ltr; "><span class="code-tag" style="background-color: inherit; line-height: 1.3; ">&lt;project name=<span class="code-quote" style=" background-color: inherit; ">"EsmeViaAnt"</span> default=<span class="code-quote" style=" background-color: inherit; ">"dist"</span> basedir=<span class="code-quote" style=" background-color: inherit; ">"."</span>&gt;</span><span class="Apple-style-span" style="line-height: 1.3;">
</span><span class="code-tag" style="background-color: inherit; line-height: 1.3; "><span class="code-comment" style=" background-color: inherit; ">&lt;!-- set global properties for this build --&gt;</span></span><span class="Apple-style-span" style="line-height: 1.3;">
</span><span class="code-tag" style="background-color: inherit; line-height: 1.3; ">&lt;property name=<span class="code-quote" style=" background-color: inherit; ">"src"</span> location=<span class="code-quote" style=" background-color: inherit; ">"src"</span>/&gt;</span><span class="Apple-style-span" style="line-height: 1.3;">
</span><span class="code-tag" style="background-color: inherit; line-height: 1.3; ">&lt;property name=<span class="code-quote" style=" background-color: inherit; ">"build"</span> location=<span class="code-quote" style=" background-color: inherit; ">"build"</span>/&gt;</span><span class="Apple-style-span" style="line-height: 1.3;">
</span><span class="code-tag" style="background-color: inherit; line-height: 1.3; ">&lt;property name=<span class="code-quote" style=" background-color: inherit; ">"dist"</span> location=<span class="code-quote" style=" background-color: inherit; ">"dist"</span>/&gt;</span><span class="Apple-style-span" style="line-height: 1.3;">
</span><span class="code-tag" style="background-color: inherit; line-height: 1.3; ">&lt;path id=<span class="code-quote" style=" background-color: inherit; ">"project.class.path"</span>&gt;</span><span class="Apple-style-span" style="line-height: 1.3;">
</span><span class="code-tag" style="background-color: inherit; line-height: 1.3; ">&lt;pathelement path=<span class="code-quote" style=" background-color: inherit; ">"./dist/lib/EsmeViaAnt.jar:./lib/esme-rest-api-0.1.jar:./lib/commons-codec-1.4.jar:./lib/commons-logging-1.1.1.jar:./lib/commons-httpclient-3.1.jar"</span>/&gt;</span><span class="Apple-style-span" style="line-height: 1.3;">
</span><span class="code-tag" style="background-color: inherit; line-height: 1.3; ">&lt;/path&gt;</span><span class="Apple-style-span" style="line-height: 1.3;">
</span><span class="code-tag" style="background-color: inherit; line-height: 1.3; ">&lt;target name=<span class="code-quote" style=" background-color: inherit; ">"init"</span>&gt;</span><span class="Apple-style-span" style="line-height: 1.3;">
</span><span class="code-tag" style="background-color: inherit; line-height: 1.3; "><span class="code-comment" style=" background-color: inherit; ">&lt;!-- Create the time stamp --&gt;</span></span><span class="Apple-style-span" style="line-height: 1.3;">
</span><span class="code-tag" style="background-color: inherit; line-height: 1.3; ">&lt;tstamp/&gt;</span><span class="Apple-style-span" style="line-height: 1.3;">
</span><span class="code-tag" style="background-color: inherit; line-height: 1.3; "><span class="code-comment" style=" background-color: inherit; ">&lt;!-- Create the build directory structure used by compile --&gt;</span></span><span class="Apple-style-span" style="line-height: 1.3;">
</span><span class="code-tag" style="background-color: inherit; line-height: 1.3; ">&lt;mkdir dir=<span class="code-quote" style=" background-color: inherit; ">"${build}"</span>/&gt;</span><span class="Apple-style-span" style="line-height: 1.3;">
</span><span class="code-tag" style="background-color: inherit; line-height: 1.3; ">&lt;/target&gt;</span><span class="Apple-style-span" style="line-height: 1.3;">
&lt;target name=</span><span class="code-quote" style="background-color: inherit; line-height: 1.3; ">"compile"</span><span class="Apple-style-span" style="line-height: 1.3;"> depends=</span><span class="code-quote" style="background-color: inherit; line-height: 1.3; ">"init"</span><span class="Apple-style-span" style="line-height: 1.3;">
description=</span><span class="code-quote" style="background-color: inherit; line-height: 1.3; ">"compile the source "</span><span class="Apple-style-span" style="line-height: 1.3;"> &gt;
</span><span class="code-tag" style="background-color: inherit; line-height: 1.3; "><span class="code-comment" style=" background-color: inherit; ">&lt;!-- Compile the java code from ${src} into ${build} --&gt;</span></span><span class="Apple-style-span" style="line-height: 1.3;">
</span><span class="code-tag" style="background-color: inherit; line-height: 1.3; ">&lt;javac srcdir=<span class="code-quote" style=" background-color: inherit; ">"${src}"</span> destdir=<span class="code-quote" style=" background-color: inherit; ">"${build}"</span>&gt;</span><span class="Apple-style-span" style="line-height: 1.3;">
</span><span class="code-tag" style="background-color: inherit; line-height: 1.3; ">&lt;classpath refid=<span class="code-quote" style=" background-color: inherit; ">"project.class.path"</span>/&gt;</span><span class="Apple-style-span" style="line-height: 1.3;">
</span><span class="code-tag" style="background-color: inherit; line-height: 1.3; ">&lt;/javac&gt;</span><span class="Apple-style-span" style="line-height: 1.3;">
</span><span class="code-tag" style="background-color: inherit; line-height: 1.3; ">&lt;/target&gt;</span><span class="Apple-style-span" style="line-height: 1.3;">
&lt;target name=</span><span class="code-quote" style="background-color: inherit; line-height: 1.3; ">"dist"</span><span class="Apple-style-span" style="line-height: 1.3;"> depends=</span><span class="code-quote" style="background-color: inherit; line-height: 1.3; ">"compile"</span><span class="Apple-style-span" style="line-height: 1.3;">
description=</span><span class="code-quote" style="background-color: inherit; line-height: 1.3; ">"generate the distribution"</span><span class="Apple-style-span" style="line-height: 1.3;"> &gt;
</span><span class="code-tag" style="background-color: inherit; line-height: 1.3; "><span class="code-comment" style=" background-color: inherit; ">&lt;!-- Create the distribution directory --&gt;</span></span><span class="Apple-style-span" style="line-height: 1.3;">
</span><span class="code-tag" style="background-color: inherit; line-height: 1.3; ">&lt;mkdir dir=<span class="code-quote" style=" background-color: inherit; ">"${dist}/lib"</span>/&gt;</span><span class="Apple-style-span" style="line-height: 1.3;">
</span><span class="code-tag" style="background-color: inherit; line-height: 1.3; "><span class="code-comment" style=" background-color: inherit; ">&lt;!-- Put everything in ${build} into the jar file --&gt;</span></span><span class="Apple-style-span" style="line-height: 1.3;">
</span><span class="code-tag" style="background-color: inherit; line-height: 1.3; ">&lt;jar jarfile=<span class="code-quote" style=" background-color: inherit; ">"${dist}/lib/EsmeViaAnt.jar"</span> basedir=<span class="code-quote" style=" background-color: inherit; ">"${build}"</span>/&gt;</span><span class="Apple-style-span" style="line-height: 1.3;">
</span><span class="code-tag" style="background-color: inherit; line-height: 1.3; ">&lt;/target&gt;</span><span class="Apple-style-span" style="line-height: 1.3;">
&lt;target name=</span><span class="code-quote" style="background-color: inherit; line-height: 1.3; ">"esme"</span><span class="Apple-style-span" style="line-height: 1.3;">
description=</span><span class="code-quote" style="background-color: inherit; line-height: 1.3; ">"Send Esme"</span><span class="Apple-style-span" style="line-height: 1.3;"> &gt;
&lt;java fork=</span><span class="code-quote" style="background-color: inherit; line-height: 1.3; ">"true"</span><span class="Apple-style-span" style="line-height: 1.3;">
classname=</span><span class="code-quote" style="background-color: inherit; line-height: 1.3; ">"EsmeAntTask"</span><span class="Apple-style-span" style="line-height: 1.3;">&gt;
</span><span class="code-tag" style="background-color: inherit; line-height: 1.3; ">&lt;arg value=<span class="code-quote" style=" background-color: inherit; ">"http://esmecloudserverapache.dickhirsch.staxapps.net/api"</span>/&gt;</span><span class="Apple-style-span" style="line-height: 1.3;">
</span><span class="code-tag" style="background-color: inherit; line-height: 1.3; ">&lt;arg value=<span class="code-quote" style=" background-color: inherit; ">"31EL0R0M15NTD2LSOS0BKC5Y0P5JOVAZ6"</span>/&gt;</span><span class="Apple-style-span" style="line-height: 1.3;">
</span><span class="code-tag" style="background-color: inherit; line-height: 1.3; ">&lt;arg value=<span class="code-quote" style=" background-color: inherit; ">"myproxy:81"</span>/&gt;</span><span class="Apple-style-span" style="line-height: 1.3;">
</span><span class="code-tag" style="background-color: inherit; line-height: 1.3; ">&lt;arg value=<span class="code-quote" style=" background-color: inherit; ">"A message from the ant build process"</span>/&gt;</span><span class="Apple-style-span" style="line-height: 1.3;">
</span><span class="code-tag" style="background-color: inherit; line-height: 1.3; ">&lt;classpath&gt;</span><span class="Apple-style-span" style="line-height: 1.3;">
</span><span class="code-tag" style="background-color: inherit; line-height: 1.3; ">&lt;pathelement path=<span class="code-quote" style=" background-color: inherit; ">"./dist/lib/EsmeViaAnt.jar:./lib/esme-rest-api-0.1.jar:./lib/commons-codec-1.4.jar:./lib/commons-logging-1.1.1.jar:./lib/commons-httpclient-3.1.jar"</span>/&gt;</span><span class="Apple-style-span" style="line-height: 1.3;">
</span><span class="code-tag" style="background-color: inherit; line-height: 1.3; ">&lt;/classpath&gt;</span><span class="Apple-style-span" style="line-height: 1.3;">
</span><span class="code-tag" style="background-color: inherit; line-height: 1.3; ">&lt;/java&gt;</span><span class="Apple-style-span" style="line-height: 1.3;">
</span><span class="code-tag" style="background-color: inherit; line-height: 1.3; ">&lt;/target&gt;</span><span class="Apple-style-span" style="line-height: 1.3;">
</span><span class="code-tag" style="background-color: inherit; line-height: 1.3; ">&lt;/project&gt;</span><span class="Apple-style-span" style="line-height: 1.3;">
</span><h2><span class="Apple-style-span" style="line-height: 17px; white-space: normal;">EsmeAntTask java class</span></h2><span class="Apple-style-span" style="line-height: 17px; white-space: normal; "><pre class="code-java" style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; text-align: left; overflow-x: auto; overflow-y: auto; 'Courier New', Courier, monospace; line-height: 1.3; "><span class="code-keyword" style=" background-color: inherit; ">import</span> org.apache.esme.api.EsmeRestApi;
<span class="code-keyword" style=" background-color: inherit; ">import</span> org.apache.esme.model.Message;
<span class="code-keyword" style=" background-color: inherit; ">public</span> class EsmeAntTask
{
<span class="code-keyword" style=" background-color: inherit; ">public</span> <span class="code-keyword" style=" background-color: inherit; ">static</span> void main (<span class="code-object" style=" background-color: inherit; ">String</span>[] args) {
<span class="code-object" style=" background-color: inherit; ">String</span> apiUrl;
<span class="code-object" style=" background-color: inherit; ">String</span> authToken;
<span class="code-object" style=" background-color: inherit; ">String</span> localProxy;
<span class="code-object" style=" background-color: inherit; ">String</span> message;
EsmeRestApi esme;
apiUrl = args[0];
authToken = args[1];
localProxy = args[2];
message = args[3];
<span class="code-keyword" style=" background-color: inherit; ">try</span> {
esme = <span class="code-keyword" style=" background-color: inherit; ">new</span> EsmeRestApi(apiUrl);
<span class="code-keyword" style=" background-color: inherit; ">if</span> ((localProxy != <span class="code-keyword" style=" background-color: inherit; ">null</span>) &amp;&amp; !("".equals(localProxy)))
{
<span class="code-comment" style=" background-color: inherit; ">// Split proxyHost:port into two parts
</span> <span class="code-object" style=" background-color: inherit; ">String</span>[] proxy = localProxy.split(<span class="code-quote" style=" background-color: inherit; ">":"</span>);
esme.setProxy(proxy[0], <span class="code-object" style=" background-color: inherit; ">Integer</span>.parseInt(proxy[1]));
}
esme.login(authToken);
Message esmeMsg = <span class="code-keyword" style=" background-color: inherit; ">new</span> Message();
esmeMsg.setBody(message);
<span class="code-object" style=" background-color: inherit; ">String</span>[] tags = <span class="code-keyword" style=" background-color: inherit; ">new</span> <span class="code-object" style=" background-color: inherit; ">String</span>[1];
tags[0] = <span class="code-quote" style=" background-color: inherit; ">"Ant Task"</span>;
esmeMsg.setTags(tags);
esmeMsg.setSource(<span class="code-quote" style=" background-color: inherit; ">"Ant"</span>);
esme.sendMsg(esmeMsg);
}
<span class="code-keyword" style=" background-color: inherit; ">catch</span> (Exception e) {}
}
</pre></span><p style="line-height: 1.3; "><span class="Apple-style-span" style="'Courier New', Courier, monospace; ">}</span>&nbsp;</p><p>&nbsp;</p><p><b>
</b></p></pre>
</div>