Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

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

The Nexus Suite is uniquely architected for a DevOps native world and creates value early in the development pipeline, provides precise contextual controls at every phase, and accelerates DevOps innovation with automation you can trust. Read how in this ebook.

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"?>
<web-app>
    <display-name>Mule Maven Tomcat Example</display-name>
    <session-config>
        <session-timeout>120</session-timeout>
    </session-config>

    <!-- 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 -->
    <context-param>
        <param-name>org.mule.config</param-name>
        <param-value>WEB-INF\mule-flows\flow.xml</param-value>
    </context-param>

    <!-- Declare the Mule Servlet to receive calls and listen on /mule/* incoming requests and redirect to requested mule servlet components -->
    <servlet>
        <servlet-name>muleServlet</servlet-name>
        <servlet-class>org.mule.transport.servlet.MuleReceiverServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>muleServlet</servlet-name>
        <url-pattern>/mule/*</url-pattern>
    </servlet-mapping>

    <!-- Declare the listeners to initialize/load mule context -->
    <listener>
        <listener-class>org.mule.config.builders.MuleXmlBuilderContextListener</listener-class>
    </listener>
</web-app>

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

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>maven-mule-project</artifactId>

    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>maven-mule-tomcat Maven Webapp</name>

    <properties>
        <java.version>1.7</java.version>
        <mule.version>3.6.0</mule.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.mule.modules</groupId>
            <artifactId>mule-module-xml</artifactId>
            <version>${mule.version}</version>
        </dependency>
        <dependency>
            <groupId>org.mule.transports</groupId>
            <artifactId>mule-transport-servlet</artifactId>
            <version>${mule.version}</version>
        </dependency>
        <dependency>
            <groupId>org.mule.modules</groupId>
            <artifactId>mule-module-spring-config</artifactId>
            <version>${mule.version}</version>
        </dependency>
        <dependency>
            <groupId>org.mule.modules</groupId>
            <artifactId>mule-module-builders</artifactId>
            <version>${mule.version}</version>
        </dependency>
        <dependency>
            <groupId>commons-cli</groupId>
            <artifactId>commons-cli</artifactId>
            <version>1.1</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>maven-mule-tomcat</finalName>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

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"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 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"/>
    </flow>

</mule>

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.

The DevOps Zone is brought to you in partnership with Sonatype Nexus.  See how the Nexus platform infuses precise open source component intelligence into the DevOps pipeline early, everywhere, and at scale. Read how in this ebook

Topics:
tomcat ,deployment ,mulesoft ,mule ,maven

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 }}