_posts/2011-01-18-use_apache_ant_to_post.html (120 lines of code) (raw):

--- layout: post status: PUBLISHED published: true title: Use Apache Ant to post to Apache ESME id: aaf2d338-5b89-45d9-bade-9fa4ba9cb8d1 date: '2011-01-18 11:28:43 -0500' categories: esme tags: - esme - microblogging permalink: esme/entry/use_apache_ant_to_post --- <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">"Codec"</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 "Httpclient</a>"&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 "<em>mvn compile package -Dmaven.test.skip=true</em>" . This will create a jar file "esme-rest-api-0.1.jar" 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 "rest-api-0.1-SNAPSHOT.jar", "commons-codec-1.2.jar", "commons-httpclient-3.1.jar" and "commons-logging-1.1.1.jar" 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 "EsmeAntTask.java" 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 "esme" 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 "esme" 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> <p><a href="https://blogs.apache.org/esme/mediaresource/d4d6de99-366b-49f3-ad5b-4bbbb35949c3"> </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> <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; "><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>></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; "><!-- set global properties for this build --></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; "><property name=<span class="code-quote" style=" background-color: inherit; ">"src"</span> location=<span class="code-quote" style=" background-color: inherit; ">"src"</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; "><property name=<span class="code-quote" style=" background-color: inherit; ">"build"</span> location=<span class="code-quote" style=" background-color: inherit; ">"build"</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; "><property name=<span class="code-quote" style=" background-color: inherit; ">"dist"</span> location=<span class="code-quote" style=" background-color: inherit; ">"dist"</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; "><path id=<span class="code-quote" style=" background-color: inherit; ">"project.class.path"</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; "><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>/></span><span class="Apple-style-span" style="line-height: 1.3;"> </span><span class="code-tag" style="background-color: inherit; line-height: 1.3; "></path></span><span class="Apple-style-span" style="line-height: 1.3;"> </span><span class="code-tag" style="background-color: inherit; line-height: 1.3; "><target name=<span class="code-quote" style=" background-color: inherit; ">"init"</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; "><span class="code-comment" style=" background-color: inherit; "><!-- Create the time stamp --></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; "><tstamp/></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; "><!-- Create the build directory structure used by compile --></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; "><mkdir dir=<span class="code-quote" style=" background-color: inherit; ">"${build}"</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; "></target></span><span class="Apple-style-span" style="line-height: 1.3;"> <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;"> > </span><span class="code-tag" style="background-color: inherit; line-height: 1.3; "><span class="code-comment" style=" background-color: inherit; "><!-- Compile the java code from ${src} into ${build} --></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; "><javac srcdir=<span class="code-quote" style=" background-color: inherit; ">"${src}"</span> destdir=<span class="code-quote" style=" background-color: inherit; ">"${build}"</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; "><classpath refid=<span class="code-quote" style=" background-color: inherit; ">"project.class.path"</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; "></javac></span><span class="Apple-style-span" style="line-height: 1.3;"> </span><span class="code-tag" style="background-color: inherit; line-height: 1.3; "></target></span><span class="Apple-style-span" style="line-height: 1.3;"> <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;"> > </span><span class="code-tag" style="background-color: inherit; line-height: 1.3; "><span class="code-comment" style=" background-color: inherit; "><!-- Create the distribution directory --></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; "><mkdir dir=<span class="code-quote" style=" background-color: inherit; ">"${dist}/lib"</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; "><span class="code-comment" style=" background-color: inherit; "><!-- Put everything in ${build} into the jar file --></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; "><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>/></span><span class="Apple-style-span" style="line-height: 1.3;"> </span><span class="code-tag" style="background-color: inherit; line-height: 1.3; "></target></span><span class="Apple-style-span" style="line-height: 1.3;"> <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;"> > <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;">> </span><span class="code-tag" style="background-color: inherit; line-height: 1.3; "><arg value=<span class="code-quote" style=" background-color: inherit; ">"http://esmecloudserverapache.dickhirsch.staxapps.net/api"</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; "><arg value=<span class="code-quote" style=" background-color: inherit; ">"31EL0R0M15NTD2LSOS0BKC5Y0P5JOVAZ6"</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; "><arg value=<span class="code-quote" style=" background-color: inherit; ">"myproxy:81"</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; "><arg value=<span class="code-quote" style=" background-color: inherit; ">"A message from the ant build process"</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; "><classpath></span><span class="Apple-style-span" style="line-height: 1.3;"> </span><span class="code-tag" style="background-color: inherit; line-height: 1.3; "><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>/></span><span class="Apple-style-span" style="line-height: 1.3;"> </span><span class="code-tag" style="background-color: inherit; line-height: 1.3; "></classpath></span><span class="Apple-style-span" style="line-height: 1.3;"> </span><span class="code-tag" style="background-color: inherit; line-height: 1.3; "></java></span><span class="Apple-style-span" style="line-height: 1.3;"> </span><span class="code-tag" style="background-color: inherit; line-height: 1.3; "></target></span><span class="Apple-style-span" style="line-height: 1.3;"> </span><span class="code-tag" style="background-color: inherit; line-height: 1.3; "></project></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> <p></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><br /> </b></p> </p></div>