DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. Deploying your Mule Application to CloudHub using Apache Ant

Deploying your Mule Application to CloudHub using Apache Ant

Nial Darbey user avatar by
Nial Darbey
·
Mar. 11, 13 · Interview
Like (0)
Save
Tweet
Share
4.35K Views

Join the DZone community and get the full member experience.

Join For Free

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

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



application Apache Ant Continuous Integration/Deployment

Published at DZone with permission of Nial Darbey, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How To Generate Code Coverage Report Using JaCoCo-Maven Plugin
  • NEXT.JS 13: Be Dynamic Without Limits
  • When AI Strengthens Good Old Chatbots: A Brief History of Conversational AI
  • Revolutionizing Supply Chain Management With AI: Improving Demand Predictions and Optimizing Operations

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: