wabson

Refreshing Web Scripts from Ant

Blog Post created by wabson Employee on May 4, 2011
I've always been a big fan of using Ant to automate some of the more mundane tasks such as building packages, when developing Share add-ons like the 20 now available on Share Extras, and the Sample Dashlet project now hosted on there now provides a template for others to use in their own projects.



That project's build script provides a target to hot-copy files into a running Tomcat instance for local testing, but whenever I've updated web script components (either on the repo side or in Share) I've had to remember to hit the Refresh Web Scripts button on the scripts index page. This calls the Web Scripts Maintenance page, which is actually a web script itself.



Since I was doing a small bit of work this week to provide a couple of new custom document actions, I figured it was worth looking again for a more automated solution to this.



Ant comes complete with it's own Get Task which can make outgoing HTTP calls, but as the name implies it's limited to GETs only and not the POSTs required to hit the Web Scripts Maintenance page. So I started looking for alternatives.



Ant's Sandbox does provide a HTTP module which can apparently make other requests, but the lack of any updates since 2007 did not bode well, and the lack of any documentation was too much of a barrier anyway. Lastly I revisited the Ant-Contrib tasks which I'd experimented with a while back, but again there were no recent updates and the library still did not support the authentication necessary to call the Maintenance web script.



Finally via a thread on Stack Overflow, I came across this project by Alex Sherwin, which is actively being maintained and supports POST requests with authentication and request parameters. Exactly what I needed.



So I added the following definitions to my build script, which seem to work well on 3.4.



<!-- Tomcat properties to reload web scripts or the manager webapp -->

<property name='tomcat.url' value='http://localhost:8080' />

<property name='tomcat.repo.url' value='${tomcat.url}' />

<property name='tomcat.share.url' value='${tomcat.url}' />

<!-- Tomcat properties to reload web scripts -->

<property name='webapp.alfresco.path' value='/alfresco' />

<property name='webapp.share.path' value='/share' />

<property name='post.verbose' value='false' />

<property name='repo.admin.username' value='admin' />

<property name='repo.admin.password' value='admin' />

<property name='repo.scripts.index' value='${tomcat.repo.url}${webapp.alfresco.path}/service/index' />

<property name='share.scripts.index' value='${tomcat.share.url}${webapp.share.path}/page/index' />




<path id='ml-ant-http.classpath'>

    <fileset dir='lib'>

        <include name='ml-ant-http-1.1.1.jar' />

    </fileset>

</path>




<taskdef name='http' classname='org.missinglink.ant.task.http.HttpClientTask'>

    <classpath>

        <path refid='ml-ant-http.classpath' />

    </classpath>

</taskdef>




<!--

Web script reloading from Ant. These tasks use the HTTP task from

http://code.google.com/p/missing-link/.

-->

<target name='reload-webscripts-repo' depends='' description='Reload repository webscripts'>

    <http url='${repo.scripts.index}'

        method='POST'

        printrequest='false'

        printrequestheaders='false'

        printresponse='false'

        printresponseheaders='false'

        expected='200'

        failonunexpected='true'>

        <credentials username='${repo.admin.username}' password='${repo.admin.password}' />

        <query>

            <parameter name='reset' value='on' />

        </query>

    </http>

</target>




<target name='reload-webscripts-share' depends='' description='Reload Share webscripts'>

    <http url='${share.scripts.index}'

        method='POST'

        printrequest='false'

        printrequestheaders='false'

        printresponse='false'

        printresponseheaders='false'

        expected='200'

        failonunexpected='true'>

        <credentials username='${repo.admin.username}' password='${repo.admin.password}' />

        <query>

            <parameter name='reset' value='on' />

        </query>

    </http>

</target>




I've tried to structure the properties to cater for most peoples' development settings, for example if you have separate Tomcats running the repo and Share on different ports, then you can specify different values for -Dtomcat.repo.url and -Dtomcat.share.url when you call the script. You should see that the username and password used to authenticate to the web script can be easily overridden if you have changed the default values.



The scripts haven't yet been tested against v3.3, but I suspect that the share.scripts.index property will need overriding in this version in order to use the 'service' servlet name instead of 'page' used in 3.4, e.g. -Dshare.scripts.index=http://localhost:8080/share/service/index .



There's just a couple of negatives. I couldn't figure out how to make the http task a little less verbose with it's output, but I'm sure that will be addressed in time. Also it would be good if the Maintenance web script came with a plain text output template rather than just HTML, as although it would be useful to output the result from the script (which shows the number of web scripts currently/previously loaded, plus any problems), the HTML markup isn't really readable enough to echo to the terminal.



Since the changes seem pretty stable, I've added these to the Sample Project in Subversion. You can try it out yourself by grabbing a copy of the build.xml file as well as the JAR file ml-ant-http-1.1.1.jar from the lib directory. I'll put a new release of the project ZIP file out in the coming days.



Lastly, here's a screenshot of the Execute Script action that I was able to test using the new build script.



Execute Script action

Outcomes