Over a million developers have joined DZone.

Deploying your Mule Application to CloudHub using Apache Ant

DZone 's Guide to

Deploying your Mule Application to CloudHub using Apache Ant

· Integration Zone ·
Free Resource


We wrote a blog over a year ago showing you how to use Ant to package and deploy your Mule Application to your Mule server. With this blog I’d like to show you how we have extended his work so that you can also deploy to CloudHub. Why would you want to do that from Ant? Why Continuous Integration of course! Wouldn’t it be nice for it go from development to CI to CloudHub automatically just as you do in-house? Of course it would!

Java Code

So, in the typical Ant way of doing things, we have to write a Java class per Ant Task. We’ll call the class CloudHubDeploy. In the execute() method we exploit CloudHub’s REST API and post the zip archive to CloudHub. We use Jersey Client to communicate with the server.

private String cloudHubUrl = "https://cloudhub.io";

	public void execute() throws BuildException {
		FileChecker checker = new FileChecker(getLocation());
		checker.checkFile(applicationFile, "application file", false, false);
		try {
			Client client = Client.create();
			client.addFilter(new HTTPBasicAuthFilter(username, password));
			WebResource webResource = client.resource(cloudHubUrl);
			String domainCreationPath = "/api/applications/" + domain;
			String appDeploymentPath = domainCreationPath + "/deploy"; 
                        webResource.path(appDeploymentPath).post(String.class, applicationFile);
		} catch (Exception exception) {
			throw new BuildException(MessageFormat.format("Problem deploying Mule application file {0} to {1}. Exception: {2}", applicationFile, cloudHubUrl, exception.getMessage()), exception, getLocation());

We must remember to map the class in the mulePackagingTasks.properties file in src/main/resources:

mulePackage: org.mule.tools.anttasks.MulePackage

muleDeploy: org.mule.tools.anttasks.MuleDeploy

cloudHubDeploy: org.mule.tools.anttasks.CloudHubDeploy

Ant Script

So, to communicate with CloudHub we should provide a username and password, the domain for the Application and of course the Application zip file itself.

<cloudHubDeploy username="myUsername" password="myPassword" domain="${app.name}" applicationFile="${app.file}" />

And now, making use of all the good work Mike put in, let’s modify his example script and replace the deployment bit!

<project name="hello" default="deploy">

	<taskdef resource="org/mule/mulePackagingTasks.properties" />
	<property name="dir.java" value="src" />
	<property name="dir.conf" value="conf" />
	<property name="dir.build" value="target" />
	<property name="dir.classes" value="${dir.build}/classes" />

	<property name="app.name" value="nialdarbey" />
	<property name="app.jar" value="${dir.build}/${app.name}.jar" />
	<property name="app.file" value="${app.name}.zip" />
	<property environment="env" />

	<target name="clean" description="Clean the output directory (start from scratch).">
		<delete dir="${dir.build}" />

	<target name="set-paths">
		<property name="dir.mule.home" value="/Users/nialdarbey/Downloads/mule3.3/dev01" />

	<target name="init" depends="set-paths">
		<!-- Create the build directory if it doesn't exist -->
		<mkdir dir="${dir.classes}" />

		<!-- Configure Mule classpath (mirrors wrapper.conf settings) -->
		<path id="classpath.mule">
			<pathelement location="${dir.mule.home}/conf" />
			<fileset dir="${dir.mule.home}/lib/user">
				<include name="**/*.jar" />
			<fileset dir="${dir.mule.home}/lib/mule">
				<include name="**/*.jar" />
			<fileset dir="${dir.mule.home}/lib/opt">
				<include name="**/*.jar" />
			<fileset dir="${dir.mule.home}/lib">
				<include name="**/*.jar" />

	<target name="compile" depends="init" description="Compile the application.">

		<javac srcdir="${dir.java}" destdir="${dir.classes}" debug="true" source="1.5" target="1.5">
			<classpath refid="classpath.mule" />


	<target name="jar" depends="compile" description="build the application jar">

		<jar jarfile="${app.jar}" basedir="${dir.classes}" />

	<target name="package" depends="jar" description="Package the application">

		<mulePackage applicationFile="${app.file}">
			<config dir="src/main/app" />
			<lib dir="${dir.build}" includes="${app.jar}" />
			<classes dir="${dir.classes}" />

	<target name="deploy" depends="package">
		<cloudHubDeploy username="myUsername" password="myPassword" domain="${app.name}" applicationFile="${app.file}" />


The proof is in the pudding! After executing the Ant build script shown above, we can login to CloudHub’s console and watch our Mule Application, called holamundo, deploy:

After, just a couple of seconds you’ll see how it’s running on the Cloud!

You can get the souce code and the Task jar itself at the same location at https://github.com/mulesoft/mule-packaging-tasks.git.


Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}