This chapter is about building a library as a plug-in for Ant. In this case, we’ll add extra user interactivity.
Sometimes you want to augment your ant scripts to provide user feedback or even get parameters from the user. For example, you could be asking for a version control label name.
We’re going to produce a Clojure project that extends an Ant class, package it up as a jar file, and then call our Clojure program from Ant. This will give us a graphical result that we can use to change the control flow of the Ant script.
In this chapter we assume the following:
You work in an environment with Java projects that use Ant to build and install projects.
You are familiar with Ant tasks and how they look in XML.
The benefits of this chapter include understanding and applying a use-case that even in the workplace with the strictest coding language constraints can be used in a way that doesn’t impact the production system. Modifying the build process with Clojure can be an ideal place to do that.
Let’s get started.
1. Create a new Leiningen project ant-lib
in your projects directory, and change to that directory:
lein new ant-lib
cd ant-lib
2. Ensure that the following is in project.clj
:
(defproject ant-lib "0.1.0-SNAPSHOT"
:dependencies [[org.clojure/clojure "1.7.0-beta2"]
[org.apache.ant/ant "1.9.1"]]
:main ant-lib.ant-message-box)
3. Rename the file src/ant_lib/core.clj to ant_message_box.clj
:
mv src/ant_lib/core.clj src/ant_lib/ant_message_box.clj
4. Ensure that ant_message_box.clj
has the following content:
(ns ant-lib.ant-message-box
(:import (javax.swing JFrame JOptionPane)
(org.apache.tools.ant Task))
(:gen-class
:name clojurerecipes.AntMessageBox
:extends org.apache.tools.ant.Task
:exposes-methods {execute executeSuper getProject getProjectSuper}))
(defn get-JFrame[]
(let [jFrame (proxy [JFrame] [])]
(doto jFrame
(.setVisible true)
(.setDefaultCloseOperation JFrame/EXIT_ON_CLOSE))
jFrame))
(defn run-JFrame [this jFrame]
(let [result (JOptionPane/showConfirmDialog jFrame "Close this
application?")]
(cond
(= result JOptionPane/YES_OPTION) (System/exit 0)
(= result JOptionPane/NO_OPTION) (.setNewProperty (.getProjectSuper
this) "msgBoxResult" "No")
(= result JOptionPane/CANCEL_OPTION) (.setNewProperty (.getProjectSuper
this) "msgBoxResult" "Cancel"))))
(defn -execute [this]
(run-JFrame this (get-JFrame)))
(defn -main[& args])
5. Run lein uberjar
to package the plug-in into a jar.
6. Change down to the projects directory, create a new directory called ant-test
, and change to that directory:
cd..
mkdir ant-test
cd ant-test
7. Open a new command prompt in the ant-test
directory. Test your Ant installation by running Ant:
ant
You should see the following result:
Buildfile: build.xml does not exist!
Build failed
8. Copy the jar file from your ant-lib
directory ant-lib/target/provided/ant-lib-0.1.0-SNAPSHOT.jar
to your ant-test
directory:
cp ../ant-lib/target/ant-lib-0.1.0-SNAPSHOT-standalone.jar
9. Create a file called build.xml
and put the following contents into it:
<project name="Message Box Test" default="run-message-box" basedir=".">
<target name="run-message-box" description="Use the Ant Task">
<taskdef name="ExitMessageBox" classname="clojurerecipes.AntMessageBox"
classpath="ant-lib-0.1.0-SNAPSHOT-standalone.jar"/>
<ExitMessageBox />
<echo message="Program continuing..."/>
<echo message="Result: ${msgBoxResult}"/>
</target>
</project>
Follow these steps for the test.
1. Run Ant with your new jar:
ant -lib ant-lib-0.1.0-SNAPSHOT-standalone.jar
You should get the result shown in Figure 25.1.
2. Click Yes. The command prompt should return from the running application.
3. Run Step 1 again and click No. You should see the following at the command prompt:
[echo] Program continuing...
[echo] Result: No
Then the program should terminate. You’ll notice we retrieved the value of the property from the No option and printed it to stdout.
Looking at AntMessageBox.clj
, the function get-JFrame
returns an instantiated and configured Java Swing JFrame
object. The function run-JFrame
displays the JFrame
and gets a result from the user and then actions that. The function –execute
is called by Ant and runs this plug-in.
In addition, when the Cancel or No buttons are clicked, we record the result in the Ant task property key list. We’ll retrieve this later.
We have built a simple Ant plug-in that can be used to alter the control-flow of your build script based on user input. This will be a great way to get started with Clojure in your workplace. The next steps would be to modify this to get user input via a field when you want to provide a label name.
This can then be used in your program flow to make decisions on the build via graphical, rather than command line, input from the user.
If you haven’t already installed Ant on a Mac, you can do so by following these steps:
1. Download Ant from the following locations as needed:
http://ant.apache.org/bindownload.cgi
This should give you a path somewhat similar to
http://apache.mirror.serversaustralia.com.au//ant/binaries/apache-ant-1.9.1-
bin.tar.gz (for Mac)
http://apache.mirror.serversaustralia.com.au//ant/binaries/apache-ant-1.9.1-
bin.zip (for Windows)
2. Expand your download apache-ant-1.9.1
.
3. On a Mac, inside the directory apache-ant-1.9.1
, copy the directory bin
to /users/<username>
and name the new bin
directory to ant-bin
. Now you should have a directory /users/<username>/ant-bin
.
4. Modify your .profile
file at /users/<username>/.profile
to add the following:
#Ant
export ANT_HOME=/Users/Julian/bin/apache-ant-1.9.1
export PATH=$ANT_HOME/bin:$PATH
5. Now reload the .profile
on your command line by running the following:
3.145.78.136