Deploying a Mavenized Mule Web App on Tomcat

DZone 's Guide to

Deploying a Mavenized Mule Web App on Tomcat

This post is all about helping the fellow Mule Devs out there who are facing exceptions in setting up a web app with Maven support and having Mule support for flows and connectors.

· DevOps Zone ·
Free Resource

Deploying a web app with Maven support and having Mule support for flows and connectors is a tedious task and very painful indeed. We still remember the time when we were very new to Mule and our project demanded us to have this kind of architecture. Then began a never-ending, painful search to get the job done. Going through official Mulesoft post to deploy Mule on Tomcat as a service led us nowhere, and we were in the middle of exceptions that were not to be fixed.

Then, one fine day, we resorted to the elixir “Stack Overflow” and asked a question to which there was no reply for a few days. But then one even finer day when the sun was about to set, we got a ray of hope and out came David Dossot with his rich expertise and know-how. It took some exchanging of comments and clarifications to clear the matter up, but when the dust settled, we were successful in deploying the THING that we were desperately seeking help for.

So, this post is all about helping the fellow Mule Devs out there who are facing exceptions in setting up the same. What we’re going to achieve after this is a webapp having Mule support and pom.xml taking care of all dependencies + a flow with a servlet inbound-endpoint hitting which would return us a payload. Easy-peasy, lemon-squeezy!

Step 1

Create a new maven project in Anypoint (or Eclipse, whichever you use) and select the org.apache.maven.archetypes as shown below for the Archetype.

Image title

Click Next >
On the next screen, enter the values in the fields like below

Image title

Click Finish and we’re done with creating the project. Hooooaahh!!

[Upon building, the project might give an error... just go ahead and delete the index.jsp file from webapp folder. We won’t be needing it anyway!]

Step 2

Now, let’s do some configuration in the web.xml file. It will form the heart of our application.
This is what the web.xml should look like for the minimal requirement that we have.

<?xml version="1.0" encoding="UTF-8"?>
    <display-name>Mule Maven Tomcat Example</display-name>

    <!-- Initialize all mule flows/configurations. You can change the location of the flows to be anywhere you wish, just change the path here to point to that config xml -->

    <!-- Declare the Mule Servlet to receive calls and listen on /mule/* incoming requests and redirect to requested mule servlet components -->

    <!-- Declare the listeners to initialize/load mule context -->

Step 3

Now, let’s add some dependencies in pom.xml that would add support for Mule and provide us with a playground for us to play (the brain of the application). For the minimal configuration, we would only need these to begin with and our pom.xml should look like this.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">


    <name>maven-mule-tomcat Maven Webapp</name>




Step 4

Now, since the web.xml and pom.xml are ready to rock-and-roll, we can add our flow in the set location, i.e. WEB-INF\mule-flows\flow.xml. Just, create a folder in the location and right-click > create new Mule config xml file. We’ll have this simple flow.

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:servlet="http://www.mulesoft.org/schema/mule/servlet" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
 xmlns:spring="http://www.springframework.org/schema/beans" version="EE-3.6.1"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/servlet http://www.mulesoft.org/schema/mule/servlet/current/mule-servlet.xsd">

    <servlet:connector name="Servlet" validateConnections="true" doc:name="Servlet"/>

    <flow name="myServletFlow">
        <servlet:inbound-endpoint connector-ref="Servlet" path="/myServlet" responseTimeout="10000" doc:name="Servlet"/>
        <set-payload value="Hello, I'm your payload" doc:name="Set Payload"/>


Image title

Step 5

Now, all we have to do is right-click the project and do a Run As—select Tomcat from the servers and hit Go. Our project should successfully be deployed.

[If any error or exception occurs, just once try doing a right-click on project Maven > Update project > Force update all snapshots and do a project dependency update.]

Step 6 (?)

This actually is not a step—we’ve already had too many. We’re done with everything, and all we have to do is pull up a web browser and hit the localhost URL on which the tomcat runs and see if we get the payload.

Image title

Voilà!! So, that’s all folks! We now have a Mavenized Mule web app deployed on Tomcat.

deployment ,maven ,mule ,mulesoft ,tomcat

Published at DZone with permission of Shanky Gupta . See the original article here.

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}