Getting started with (Mule ESB) Anypoint Studio and Maven

mule-esbRecently I started to work with Mule ESB, in my opinion one of the best Enterprise Service Bus on the market. It’s easy, fresh, modern and does not give you the impression of a Big Monster that will eat you like products from Oracle or IBM :-).

It is really a good product but is not immune to some little defects that could let you waste some time, especially at the beginning when you are not confident with it.

In the following tutorial I will describe how to configure Anypoint Studio with Mule ESB runtime embedded on a local machine and the initial setup to work with Maven.

I assume that you already have a Anypoint Studio with an Enterprise/Community run-time installed, if not you can always download AnyPoint Studio from MuleSoft official website.

Another prerequisite is a local Maven installation and some confidence with the tool.

Check the JRE

Anypoint Studio is based on Eclipse and by default will take one of the JRE installed on your pc, if you are a developer like me you will probably have more than one and could be that Anypoint will pick up the one you don’t want to use.

Go to Anypoint Studio -> Preferences or Window ->Preferences if you are on Windows and  than on left menu select Java-> Installed JREs.

jre

If the selected JRE is not the one you want to use add a new one using the button on the right or let the editor search all the available one on the system using the Search button.

Maven configuration

Let’s come back to the Preferences screen and on the left menu go to Anypoint Studio -> Maven settings .

On this screen tick the check-box “Enable Maven support…” has our main target is to use maven with Anypoint Studio.

The second check-box “Enable automatic maintenance of pom.xml” should be checked, especially if you are  a beginner. This will let Anypoint studio update the pom.xml dependencies automatically when using new components in your apps.

Something that I personally found useful is to add the option -DskipTests=true to the MAVEN_OPTS field, this will tell maven to skip the tests when running application on the embedded run-time of the editor.maven

Before closing the screen make sure the maven installation directory is the correct one.

My darling M2_REPO variable

Mules prepend to all dependencies in the build path an environment variable that contains the path to the local maven repository, by default this variable contains the canonical value of a maven repository (userhome/.m2/) but could be that our maven installation is customised and that our local repository is located somewhere else on the disk.

Let’s check if the path is correct by going to the Preferences screen and selecting from the left menu Java->Build Path -> Class path variables.

In the central panel you should see the M2_REPO variable, if the path is not correct just click edit and change it. In case the variable does not exist at all you can create it using the Add button on the right.variable

Only for Enterprise customers

If you are an enterprise customer MuleSoft should have provided you access to the Maven Enterprise Repos with specific credential.

To let maven access and download dependencies from this repository you should open your general maven settings.xml and add the following:

To the server node:

<server>
    <id>MuleRepository</id>
    <username>Your username</username>
    <password>Your Password</password>
</server>

To your profiles

<profile>
  <id>Mule</id>
  <activation>
    <activeByDefault>true</activeByDefault>
  </activation>
  <repositories>
    <repository>
      <id>MuleRepository</id>
      <name>MuleRepository</name>
      <url>https://repository.mulesoft.org/nexus-ee/content/repositories/releases-ee/</url>
      <layout>default</layout>
      <releases>
        <enabled>true</enabled>
      </releases>
      <snapshots>
        <enabled>true</enabled>
      </snapshots>
    </repository>
  </repositories>
</profile>

Choosing the right runtime

Now you are ready to create your first project with Maven support in Anypoint Studio by clicking on on File->New->Mule Project and in the popup choose a name for your project, enable the maven support and choose the right runtime. By default Anypoint Studio ships with only the Mule Server EE runtime and if you but if you want to use only community feature that you should select a CE runtime.

If Mule Server CE is not displayed you can install it by going to Help->Install New software and choose from available sites dropdown the item “Mule ESB Runtimes for Anypoint Studio” on the central window you can see a node that contains different CE runtimes, choose the one you like and install it.

Remember to never start to work with a project on EE if your target is later on to run on CE, this because some component can not run on CE and furthermore the pom automatic management will assume you are an Enterprise customer and will add dependencies pointing to packages available only on the enterprise Maven repository of Mulesoft.

A note on Mavenizing existing projects

As you may know mule offer the possibility to “Mavenize” a normal Mule project for enable the Maven support by simply right clicking on the project and select the relative option under the menu “Maven Support in Studio”. When doing that Mule will read the configuration file of your app and create a POM out of it.

This functionality I assume is not yet working perfectly, one example is that the java version that he will put will be always the 6, despite your editor is using the 7 or 8.

What I strongly suggest is to check the POM manually making sure that also all the dependencies are there.

For example if your project is using a Quartz component in your pom you should find the dependency

    <dependency>
            <groupId>org.mule.transports</groupId>
            <artifactId>mule-transport-quartz</artifactId>
            <version>${mule.version}</version>
            <scope>provided</scope>
    </dependency>

but also the inclusion node under the maven-mule plugin configurations

<inclusion>
    <groupId>org.mule.transports</groupId>
    <artifactId>mule-transport-quartz</artifactId>
</inclusion>

It happened to me that when Mavenizing a project some dependencies was not added to the POM and I realised it only when running tests on Maven because in the embedded runtime of the editor everything was still working fine.

Most of you may think that this stuff are easy and don’t really need a tutorial, however I found myself loosing some time on this little stupid configuration issues and that’s way I decided to share this small experience that I hope it will help you too.

  • Yuvaraj

    I observed that Mule Embeded Server doesn’t care scope attribute of maven dependencies. I see application lib is also including dependencies with scope ‘provided’

    I am referring location under workspace: .muleappsfxaip-cas-webservices-legacylib

  • http://www.toforge.com/ Mauro Rocco

    Probably because Mule Creates a server container for each app that is completely independent from other apps. But this is just a guessing.

  • tek slate

    Tekslate.com is the Industry leader in providing MULE ESB Training across the globe. Our online training methodology focus on hands on experience of MULE ESB.

    To Attend Free Demo (Or) For any Queries Write to us at:

    Email: info@tekslate.com

    Call us on

    USA :- +1 415-830-3823, India :- 91 954-262-2288

    Website: http://tekslate.com/

    URL: http://tekslate.com/mule-esb-training/

  • Rajesh

    Nice explanation and usefull information on Mule with Maven. Thanks and its useful for me.