Enterprise Integration Zone is brought to you in partnership with:

Nial is an engineer at Mulesoft. He writes blogs about use cases and technologies catered for by Mule. Nial is a DZone MVB and is not an employee of DZone and has posted 8 posts at DZone. You can read more from them at their website. View Full User Profile

Deploying your Mule Application to CloudHub using Apache Ant

03.11.2013
| 2526 views |
  • submit to reddit

Introduction

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 to 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";

	@Override
	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>

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

	<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>
			<fileset dir="${dir.mule.home}/lib/mule">
				<include name="**/*.jar" />
			</fileset>
			<fileset dir="${dir.mule.home}/lib/opt">
				<include name="**/*.jar" />
			</fileset>
			<fileset dir="${dir.mule.home}/lib">
				<include name="**/*.jar" />
			</fileset>
		</path>
	</target>

	<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" />
		</javac>

	</target>

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

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


	<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}" />
		</mulePackage>
	</target>

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

Result

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 !

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 Nial Darbey, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)